I have a toggle button with 3 pins. I want to use it to turn 2 leds, when it's in one position turn on one led (red) and when it's in the other position turn on the other led (green).

I found a lot of information about push buttons, but no about toggle buttons. I tried it using the 5V GPIO like in the schematic below.


simulate this circuit – Schematic created using CircuitLab

Now I want to use 2 GPIOs as inputs to know what led is receiving the current. I'm sure it's possible, i'm sure it's easy but i'm a noob.

My idea is do this:

GPIO.setup(20, GPIO.IN)
GPIO.setup(21, GPIO.IN)

green_led_state = GPIO.input(20)
red_led_state = GPIO.input(21)
if green_led_state == True and red_led_state == False:
    print('Green led active')
if red_led_state == True and green_led_state == True:
    print('Red led active')

3 Answers 3


You must not connect a Pi GPIO to a voltage outside the range 0 to 3.3V. If you do so you will eventually destroy the GPIO and then the Pi.

Assuming you are using 3.3V.

Connect a GPIO to the green LED at the switch.

If the GPIO is high the green LED is on, otherwise the red LED is on.

  • Thank you very much, you think I need to put some resistors?
    – Lleims
    Commented May 18, 2019 at 14:49
  • Yes, you need a resistor in series with each LED. Something like 330 ohms should be fine. If you search on-line you will probably find info about resistors for each colour of LED if you want to match apparent brightness.
    – joan
    Commented May 18, 2019 at 16:09

You can connect any LED that has a forward voltage of > 3 volts directly with a GPIO pin of a Raspberry.

To calculate the resistor:

Normally 10 mA is reasonably bright for a LED. However, if you want the maximum, check the Raspberry documentation for the recommended maximum current per output pin.

The steps are:

  • Find the forward voltage of the LED (Vf), let's say 2 V.
  • Calculate the resistor: V = I * R <=> ( 3 - Vf) = 0.01 * R <=> R = (3 - 1) / 0.01 = R <=> R = 100 ohm

For the other LED, you perform the same steps (probably the red and green LED have different forward voltages). Also it is high likely that 10 mA for a LED red looks more or less bright than 10 mA for a green LED.

About the program:

It is common not to write == True or == False in boolean expressions, you can leave the == True away, and instead of == False you can write not in front of the expression, thus:

if green_led_state and not red_led_state:
    print('Green led active')
if red_led_state and green_led_state:
    print('Red led active')

The last condition from your program is strange: you check for both LEDs to be green and write Red led active. I think you probably mean:

if green_led_state and not red_led_state:
    print('Green led active')
if not red_led_state and green_led_state:
    print('Red led active')

try the code below(untested)

import time 
import RPi.GPIO as GPIO

G = 17
R = 27

GPIO.setup(G, GPIO.IN)
GPIO.setup(R, GPIO.IN)

while (i<10):
        print ('RED led is active')
        print ('RED led is active')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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