cancel
Showing results for 
Search instead for 
Did you mean: 

GPIO Problem with STM32F070RB

JAgui.1
Associate II

Hello everyone,

I am developing a small program with a relay which has a data, power and ground input. This is powered at 5v and I have it connected to the GPIO PA0 output.

My code is as simple as creating a task with FreeRTOS as follows:

void StartDefaultTask(void const * argument)

{

/* USER CODE BEGIN 5 */

/* Infinite loop */

for(;;)

{

HAL_GPIO_WritePin(GPIOA, EV_Pin, 1);

HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);

HAL_GPIO_WritePin(GPIOA, EV_Pin, 0);

osDelay(100);

}

/* USER CODE END 5 */

}

The question is that when I run it, the relay turns on but does not change state. It is as if the GPIO output is not working. However the led 2 does flash with the TogglePin function.
Inside my .ioc file I have left everything by default, just changing the pin label to identify it more easily and trying the GPIO Mode option to Output Open Drain but nothing works.

Does anyone know what can happen?

Best regards, thanks.

4 REPLIES 4
KnarfB
Principal III

Your code sets the EV_Pin to high only for a very short time, the duration of the HAL_GPIO_TogglePin call, and then immediately back to low, and it stays low for 100ms. If you want to toggle the relais, you may add another osDelay after setting the pin high.

hth

KnarfB

Thanks for the idea, but it does not solve the bug. The problem is that even with a single HAL_GPIO_Write instruction it doesn't change.

I have modified the delay value, added an osDelay after the first HAL_GPIO_Write and none of this works.

Karl Yamashita
Lead III

You haven't shown any code that initializes the GPIO.

The LD2 seems to be generated by CubeMX but for the EV_Pin you're not using a GPIO Port define that would be normally be defined as EV_GPIO_Port but instead you're using GPIOA. So it comes down to what GPIO port is EV_pin on?

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
KnarfB
Principal III

> a relay which has a data, power and ground input. This is powered at 5v and I have it connected to the GPIO PA0 output.

This sounds special. More info on the type of relais, schematics, etc.. could be helpful. Not every relais can be switched by a 3.3V GPIO.