0
\$\begingroup\$

I'm using an STM32F103C8T6. I connected an LED to the pin PA5 and a tactile button switch to the pin PA0. I wrote the program as follows:

If(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
}
else
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
}

The program works as expected but the LED is lit up about 50% bright when the switch isn't pressed and 100% when it is. And to be sure it wasn't the microcontroller, I wrote another LED blinking program which worked as expected. I'm inclined to believe that it has something to do with the code. I used the STMCubeMX to initialize the pins so I don't think I've done anything wrong there. Where am I going wrong?enter image description here

\$\endgroup\$
  • \$\begingroup\$ Welcome to the site. The problem may be in the schematic but you show no schematic. Please edit your question and add a schematic, the schematic tool here is a breeze to use. Make sure that the schematic is of what you have, not what you think you might have made. The better the quality of your question, the better the quality of the answers you will attract. \$\endgroup\$ – TonyM Oct 4 at 10:16
  • 3
    \$\begingroup\$ Presumably that code is in a loop and somewhere else in the loop there is code that is also setting the LED GPIO pin, so that the LED is turning on and off very quickly when the button is not pressed. Can you show the rest of the code? \$\endgroup\$ – HandyHowie Oct 4 at 10:23
  • \$\begingroup\$ @HandyHowie that's all that's in the loop. Everything else is initialization code. I checked the output initialization code for pin PA5 and it was set to 0. \$\endgroup\$ – Joshua John Oct 4 at 10:26
  • \$\begingroup\$ Haven't you got your LED upside down in the schematic? \$\endgroup\$ – HandyHowie Oct 4 at 10:33
  • \$\begingroup\$ You should also have a pull up/down on you switch input. It is probably floating on and off causing your 50% illumination. \$\endgroup\$ – HandyHowie Oct 4 at 10:34
3
\$\begingroup\$

You need a pull down resistor in you switch GPIO pin.

Connect a 10K resistor from the pin to ground.

The GPIO is probably floating causing the input to switch on/off very quickly when the button is not pressed. This will cause your LED to glow slightly or give random behavior.

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
  • \$\begingroup\$ I also wrote another program where I toggled the pin off, on, and also had it blink. The LED turned on and off correctly then. \$\endgroup\$ – Joshua John Oct 4 at 11:57
  • \$\begingroup\$ @JoshuaJohn Not sure what you are saying. Is it working correctly now with a pull down resistor? \$\endgroup\$ – HandyHowie Oct 4 at 13:15
  • \$\begingroup\$ I just got to trying it out, and yes, it works. Thanks! \$\endgroup\$ – Joshua John Oct 5 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.