Raspberry Pi LESSON 30: Controlling LEDs from Push Buttons

In this lesson we will show how you can control LED’s from push buttons. In order to get started, you will want to expand the circuit we built in LESSON 29 to include two LEDs. The schematic below shows how you will want to hook things up (Also, remember you can see the Raspberry Pi pinout in LESSON 25). Also, as we have mentioned before, if you want to follow along with us in these lessons you can get a kit that has all the gear you need HERE.

Raspberry Pi LED Circuit
This Circuit Controls two LED from Push Buttons Using the Raspberry Pi

In the video lesson, we take you through the code step-by-step. We use the techniques learned in LESSON 29 to detect if a button has been pushed. We introduce two new variables, BS1 and BS2, so indicate the state of the LED’s. A BS1=False means the LED1 is off. A BS1=True means the LED is on. This concept allows us to determine whether we should turn the LED on or off when the button is pushed. Basically, we want to put it in the opposite state when a button is pushed. The code is below. The video shows how it works.


7 thoughts on “Raspberry Pi LESSON 30: Controlling LEDs from Push Buttons”

  1. Please help! This looks similar to a project i am trying to master but i am not having much luck. I want to use two buttons for my raspberry pi 3. One to completely and safely cut power the other to reset the pi. The wrinkle is i want to use existing buttons from an old gaming system. Any help you can provide is greatly appreciated

  2. Firstly I must say I like your lessons very much. However look at this small code which will replace your if statements.
    if GPIO.input(button1)==0:
    BS1 = not BS1
    This is simpler, and in programming simple is nice.

  3. as Mathew pointed out
    There is a type, an extra “,” at the end of OUT

    I am also getting an error about missing () on the print line.
    print(“Button 1 Was Pressed:”) #fixed error

    Small fixes but still a very informative video, keep up the good work.

Comments are closed.