# Semaphore using one microcontroller pin

I'm trying to make a semaphore using to LEDs green and red, controlled by one arduino pin. Idea is when pin is low red LED is on and green LED is off, when pin is up green LED is on and red LED is off.

So I've made the circuit using two transistors and in simulation software everything seems to be ok, but on practice red LED (right one on the picture) is still shining dimly when the green one (left one) is on.

On diagrams you can see there is still a small current through red LED, when the pin is in up state.

Is there any ideas how to deal with this current?

• Hmm. Is 33 uA too high to call the LED "off"? Commented Apr 14, 2015 at 13:08
• Yes, I real circuit the LED is still shining, but dimly Commented Apr 14, 2015 at 13:14
• Similar question: electronics.stackexchange.com/questions/106090/… Commented Apr 14, 2015 at 16:21

## 4 Answers

Two words - source, sink.

The output pin of an arduino is capable of sourcing (supplying an output current) as well as sinking (taking a current into the output and down to ground.) see http://arduino.cc/en/Tutorial/DigitalPins

You don't need any transistors to switch the LEDs.

In this circuit when the output is LOW (near 0V) the green LED will be ON, red is OFF. When the output is HIGH (near 5V) the red LED is ON , green is OFF.

The resistors limit the sink/source current to a safe value.

• Same as @MichaelKaras - both LEDs will be partially ON if the drive pin is hi-z. Commented Apr 14, 2015 at 13:56
• @Dzarda To quote from the reference given in the answer about configuring the pin as an OUTPUT "Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means that they can provide a substantial amount of current to other circuits. Atmega pins can source (provide positive current) or sink (provide negative current) up to 40 mA (milliamps) of current to other devices/circuits." - Not exactly High z! Commented Apr 14, 2015 at 14:19

For the sake of completeness: If you wanted to provide more current than your MCU can deliver, you can still use two transistors, but this time one NPN and one PNP. NPN in this case works as a non-inverting switch, PNP is inverting.

Note: This only works if your Drive voltage (MCU pin) goes all the way from 0 to 5V. Not a problem with the ATMega.

simulate this circuit – Schematic created using CircuitLab

Looks like you have voltage leaking from the 5V through the second transistor base to the emittor, which is giving you a faint glow (Similar to the problem I faced in this post). As the current from the Arduino is enough to power 1 LED at a time (with a ~300-470Ohm resistor), you could just use the circuit from my post to make this all work. Saves you a transistor :)

EDIT: The full circuit you should implement (your Arduino pin goes where IN1 is, and you should replace the 9V source with your 5V source or with another pin from the arduino) is this (from this post):

simulate this circuit – Schematic created using CircuitLab

NOTE that this assumes LED1 is Red and LED2 is green - so you would have to invert your pin logic on the Arduino.

• LED2 will always be on in this circuit (assuming same forward voltage) Commented Apr 14, 2015 at 13:16
• Thank you, I forget about difference of voltage of LEDs. This circuit is working fine, but another with source / sink work properly without any transistors! Commented Apr 14, 2015 at 14:37
• @geometrikal That's why my NOTE says "LED1 is Red and LED2 is green" - as per the linked post the reversed scenario indeed keeps LED2 always on Commented Apr 14, 2015 at 14:51
• @PhilB. Colour of the LED doesn't necessarily correspond to forward voltage. Commented Apr 15, 2015 at 5:53

A good way to deal with this is to stop putting the LEDs in to the emitter part of the circuit. Instead put the LED's in the collector part of the circuit in series with the current limit resistors.

Another thing to consider is that if the MCU pin has adequate drive capability and a 5V voltage swing you may consider simply connecting up the LEDs as follows:

• Note that if the MCU PIN is tri-stated, both LEDs will shine at lower intensity. Commented Apr 14, 2015 at 13:53
• Should not be a problem for the OP as they said that the MCU pin would be High or Low. The MCU pin in INPUT mode would result in the condition you specify and would exist from MCU power on and until the software sets the pin of output. The both on dim indicator can mean the software is not running properly. Commented Apr 14, 2015 at 14:01