Step 1: Supplies:
- Raspberry Pi 4B
- Raspberry Pi 4B power supply
- Micro SD card Raspberry Pi OS
- Micro-HDMI to HDMI cable
- Mouse & keyboard
- Monitor
- Breadboard
- LED 2X
- 2x Resistors 220Ω
- Push buttons 2x
- 2x Jumper wires (fm)
- Jumper wires (mm) 2x
Step 2: The project
In this project we use the GPIO pins of the Raspberry Pi. You can find the pinout of these GPIO pins by typing 'pinout' in the terminal. From these GPIO pins we run a few jumper wires, which we connect to a breadboard connect. On the breadboard we place two push buttons and two LEDs.
In addition, we add a resistor to each LED, which ensures that not too much current flows through the LED. The aim of the project is that you can control the LEDs with the push buttons. This means that when you press the right push button, the right LED turns on. And when you press the left push button, the left LED turns on. If you press both push buttons simultaneously, we ensure that both LEDs flash.
Step 3: Buildwen & wiredn
De Raspberry Pi has a number of GPIO pins. We connect these pins to a bread using jumper wiresboard. On the breadboard we place two push buttons, two LEDs and two resistors (see image). We then connect the cathode (= the negative on the short leg of the LED) of both LEDs to the gnd (ground = 0V).
We also connect one side of both push buttons to gnd. In the image below we have chosen black jumper wires for the gnd. Now we are going to connect the other side of the components.
We connect the anode (= the plus on the long leg of the LED) of the LEDs to a resistor. We connect the other side of the resistor to a GPIO pin. In this example we have chosen to connect LED 1 to GPIO pin 8, and LED 2 to GPIO pin 7. See the image below.
Unlike the LEDs, we connect the push buttons directly to the GPIO pins. Here you do not need any additional resistance between the push button and the Raspberry Pi to place. In the example we connect push button 1 to GPIO pin 14, and push button 2 to GPIO pin 21. See the image below.
Figure 1
When you have connected everything properly, as in the example, you can proceed to the code.
Step 4: programming
Now it's time to create code to control GPIO pins. We create this code in the Thonny Python IDE. With the RPi.GPIO Python library you can easily read and control the GPIO pins. This library is already installed by default Raspberry Pi BONE.
Once you have started Thonny Python IDE you can type the code. At the top of the code we import the library RPi.GPIO (which we call GPIO) and library time.
import RPi.GPIO as GPIO
import team
You can number the GPIO pins in different ways. In the RPi.GPIO library you can enter PIN codes (BOARD) or Broadcom GPIO numbers (BCM). You can't use both at the same time, so we have to make a choice. In our case we choose the BCM numbering.
GPIO.setmode(GPIO.BCM)
Nwe need to make sure that the GPIO warnings are turned off. We do this with the following line:
GPIO.setwarnings (False)
Now it's time to indicate which GPIO pin each component is connected to. We do this by naming the push buttons and LEDs, and then indicating to which GPIO pin it is connected.
button1 = 14
button2 = 21
led1 = 8
led2 = 7
If you have connected the components to a different GPIO pin, you can easily change this here by typing a different number.
Now let's tell the code what the functions of GPIO pins are. We tell the code that button1 and button2 (pin 14 and pin 21) are both an input. Behind this we indicate that these are push buttons. At the GPIO pins of the LEDs we only indicate that it is an output.
GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
Now we are going to create an infinite loop. We do this by making the while loop True. We then add to the code that we always check whether a push button has been pressed. When this is the case, the buttonState (push button status) of the relevant button becomes True.
while True:
buttonState1 = GPIO.input(button1)
buttonState2 = GPIO.input(button2)
Now we go to the control part of the LEDs. It is always checked which push button has been pressed. If no push buttons are pressed, both LEDs are off. When push button 1 is pressed, LED 1 is on. When push button 2 is pressed, LED 2 is on. When both push buttons are pressed, both LEDs will flash. This is done by turning the LEDs on for 0,1 second, after which the LEDs turn off again for 0,1 second. This happens until the push buttons are no longer pressed.
if buttonState1 == True and buttonState2 == True:
GPIO.output(led1,0)
GPIO.output(led2,0)
if buttonState1 == False and buttonState2 == True:
GPIO.output(led1,1)
GPIO.output(led2,0)
if buttonState1 == True and buttonState2 == False:
GPIO.output(led1,0)
GPIO.output(led2,1)
if buttonState1 == False and buttonState2 == False:
GPIO.output(led1,1)
GPIO.output(led2,1)
time.sleep (0.1)
GPIO.output(led1,0)
GPIO.output(led2,0)
time.sleep (0.1)
Below you will find the entire code in one piece. You can retype or copy the code. We recommend that you retype the code, because you will learn more from it. You can also come up with adjustments or additions to the code yourself.
import RPi.GPIO as GPIO
import team
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings (False)
button1 = 14
button2 = 21
led1 = 8
led2 = 7
GPIO.setup(button1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
while True:
buttonState1 = GPIO.input(button1)
buttonState2 = GPIO.input(button2)
if buttonState1 == True and buttonState2 == True:
GPIO.output(led1,0)
GPIO.output(led2,0)
if buttonState1 == False and buttonState2 == True:
GPIO.output(led1,1)
GPIO.output(led2,0)
if buttonState1 == True and buttonState2 == False:
GPIO.output(led1,0)
GPIO.output(led2,1)
if buttonState1 == False and buttonState2 == False:
GPIO.output(led1,1)
GPIO.output(led2,1)
time.sleep (0.1)
GPIO.output(led1,0)
GPIO.output(led2,0)
time.sleep (0.1)
If you have successfully completed this project, you will know how and for what purpose you can use the GPIO pins of the Raspberry Pi can use.