1
\$\begingroup\$

I want to trigger a LED with both an Arduino and a hardware switch without coding any button logic in the Arduino.

schematic

simulate this circuit – Schematic created using CircuitLab

When the button is pressed there is a direct connection between the 5V power supply and Arduino pin 17. Can this do any harm? Is there a better solution?

\$\endgroup\$
3
\$\begingroup\$

What you have drawn is a bad idea. If the Arduino output is low when the button is pressed, the 5V bus will be shorted. That's really bad. Instead, do something like this.

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
  • \$\begingroup\$ You use the 2N3904 to avoid current flowing back to the Arduino output? \$\endgroup\$ – Bas van Dijk May 19 '14 at 14:28
  • \$\begingroup\$ You could run the output open drain, but I prefer to switch LEDs with an external transistor. There is no reason for the micro to have to sink that current. \$\endgroup\$ – Matt Young May 19 '14 at 14:39
  • \$\begingroup\$ OK, if I replace the LED with a relay, do I still need R1? \$\endgroup\$ – Bas van Dijk May 19 '14 at 14:47
  • \$\begingroup\$ Yes, R1 is here to limit the current into the LED. \$\endgroup\$ – Blup1980 May 19 '14 at 14:49
  • \$\begingroup\$ You do not need R1 if you replace the LED with a relay. But you will need a catch diode in parallel with the relay (reverse biased). \$\endgroup\$ – akellyirl May 19 '14 at 14:52
2
\$\begingroup\$

Yes. There is a much better solution.

First off you need a resistor in series with your LED. Connect the resistor from the +5V and then to anode of the LED.

The switch would connect from the cathode of the LED to GND. When pressed the LED will go on.

The Arduino output needs to be operated in a mode where it changes from Input to Output@0 for its switching activity. Connect the output to the cathode of the LED. Under no circumstances should your software ever set the output pin to a '1' high level. You want to avoid the situation where a high output level can get shorted to GND by pressing the switch. To turn the LED on the pin is set low '0' as an output. To turn off the LED the pin is set to input mode.

\$\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.