0
\$\begingroup\$

I want to connect a LED and Push button on the GPIO input pin of a MCU, STM32F103 so that when the push button is pressed then the state of LED indicates the button is pressed. Is it possible to connect a LED and Push button on the input pin?

\$\endgroup\$
2
\$\begingroup\$

Sure.

schematic

simulate this circuit – Schematic created using CircuitLab

Figure 1. Two options.

R3 and R4 added as per Spehro's comment.

When the button is pressed it pulls the GPIO to a well defined voltage. When the button is released it needs to be pulled to the opposite logic level (other voltage rail). This is done by a pull-up (a) or a pull-down (b) resistor. I assumed that the LED and series resistor would do that for you. Spehro (who actually does this stuff for a living) cautions that a separate resistor may be required due to the LED being in circuit and making the pull very weak.

\$\endgroup\$
  • 1
    \$\begingroup\$ What will be the mode of GPIO pin? open-drain or tri-state or someother? \$\endgroup\$ – homecloud Aug 1 at 20:20
  • 2
    \$\begingroup\$ In both cases you should parallel the LED with a resistor to ensure a valid logic level for the input. \$\endgroup\$ – Spehro Pefhany Aug 1 at 20:21
  • 1
    \$\begingroup\$ @homecloud: Open drain and tri-state are for outputs. You need to configure the pin as an input. \$\endgroup\$ – Transistor Aug 1 at 20:22
  • 1
    \$\begingroup\$ Thanks, @Spehro. I've added them in. Have you had problems with this before? \$\endgroup\$ – Transistor Aug 1 at 20:25
  • 1
    \$\begingroup\$ Maybe ..... ;-) \$\endgroup\$ – Spehro Pefhany Aug 1 at 20:26
1
\$\begingroup\$

Actually, this is quite simple. Wire it up like this:

schematic

simulate this circuit – Schematic created using CircuitLab

You may even configure the GPIO as an open drain output and control the LED from the µC as well as the button. As you can't check the button while the GPIO is turned an output, you have to switch to input mode for a few µs every few ms. This is invisible.

What you can't have with this circuit is not turning on the LED when the button is pressed.

\$\endgroup\$

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.