Raspberry Pi: Push-button + Two(2) LEDs that Toggle


 2020-04-26 1 minute read 0 Comments #raspberry pi

Got another simple project here for you!

This project is about adding an additional LED light to the Raspberry Pi: Push-button + One(1) LED post, and having the push-button toggle the LED lights. I used one red and one blue LED.

The desired result, toggle on/off of the two LEDs with the push of a button; if the red light is on then the blue is off, and vice versa.

Let's see how this goes...

With this project, I learned I need to have a better understanding around detecting events with the GPIO. I found myself modifying “bouncetime” to achieve the desired result, but it is not optimal.

With the code below, there are times when the lights do not toggle or the light remains on.

Any ideas what I am doing wrong? Let me know in the comment section below.

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time

def on_callback(channel):
    GPIO.output(channel, GPIO.input(4))

def off_callback(channel):
    GPIO.output(channel, False)

GPIO.setmode(GPIO.BCM)
RED_LED = 25
BLUE_LED = 24
SWITCH_IN = 4
GPIO.setup(SWITCH_IN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(RED_LED, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(BLUE_LED, GPIO.OUT, initial=GPIO.LOW)
GPIO.add_event_detect(SWITCH_IN, GPIO.RISING, bouncetime=900)


led = BLUE_LED
nled = RED_LED
run = 1
while (run != 0):
    try:
        if GPIO.event_detected(4):
            print led
            led = BLUE_LED if (led == RED_LED) else RED_LED
            nled = RED_LED if (nled == BLUE_LED) else BLUE_LED

            on_callback(led)
            off_callback(nled)

        pass
    except KeyboardInterrupt:
        run = 0
        GPIO.cleanup()
        print 'KeyboardInterrupt caught'
        break
GPIO.cleanup()
print 'EXITED!'

Also available here, raspi-push-button-led-toggle.


 Categories: #raspberry pi


Ready for another?
Raspberry Pi: Push-button + One(1) LED

This was my first attempt at getting a push-button to turn on an LED light with my Raspberry Pi w/Pi Cobbler setup.

I had a lot of fun putting the bits and pieces together, and I hope you enjoy reading about it!

Hello, World

# Bash
WORLD='World'
echo "Hello, ${WORLD}"
Hello, World

This is officially the first post! How exciting!
Let me start things off by describing the idea behind the creation of Prompt Fu.

Tech tips and command line fu found within this dojo

 2023