5059+ reviews
Order by 16:00 for same day shipping
14 days return
GB
EN
Individual
Business
In this project we use the GPIO pins of the Raspberry Pi . The pinout of these GPIO pins can be found by typing 'pinout' in the terminal. From these GPIO pins we let a few jumper wires come, which we connect to a breadboard. On the breadboard we place two push buttons and two LEDs.
In addition, we place a resistor at each LED, which ensures that not too much current flows through the LED. The goal of the project is that you can control the LEDs with the push buttons. This means that if you press the right push button, the right LED will light up. And if you press the left push button, the left LED will light up. If you press both push buttons at the same time, we ensure that both LEDs will flash.
The Raspberry Pi has a number of GPIO pins. Using jumper wires, we connect these pins to a breadboard. On the breadboard, we place two pushbuttons, two LEDs, and two resistors (see image). Then we connect the cathode (= the minus à the short leg of the LED) of both LEDs to the gnd (ground = 0V).
We also connect one side of both push buttons to the 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 to 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 chose to connect LED 1 to GPIO pin 8, and LED 2 to GPIO pin 7. See the image below.
The push buttons, unlike the LEDs, are connected directly to the GPIO pins. Here you do not need to place an extra resistor between the push button and the Raspberry Pi . In the example we connect push button 1 to GPIO pin 14, and push button 2 to GPIO pin 21. See the image below.
Once you have connected everything correctly, as in the example, you can move on to the code.
Now it's time to create some code to control the GPIO pins. We'll 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 in Raspberry Pi OS.
Once you have started Thonny Python IDE you can type the code. At the top of the code we import the library RPi.GPIO (we call it GPIO) and library time.
import RPi.GPIO as GPIO
import time
The GPIO pins can be numbered in different ways. In the RPi.GPIO library you can choose 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)
Now we need to make sure that the GPIO warnings are turned off. We do this with the following line:
GPIO.setwarnings (False)
Now it is time to indicate which GPIO pin each component is connected to. We do this by naming the pushbuttons and LEDs, and then indicating which GPIO pin it is connected to.
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 we will tell the code what the functions of the GPIO pins are. We tell the code that button1 and button2 (pin 14 and pin 21) are both an input. Behind that we indicate that it concerns push buttons. With 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. Then we put in the code that it is always checked whether a button has been pressed. If this is the case, the buttonState of the button in question becomes True.
while True:
buttonState1 = GPIO.input(button1)
buttonState2 = GPIO.input(button2)
Now we go to the control section of the LEDs. It is always checked which pushbutton is pressed. If no pushbuttons are pressed, both LEDs are off. If pushbutton 1 is pressed, LED 1 is on. If pushbutton 2 is pressed, LED 2 is on. If both pushbuttons are pressed, both LEDs will flash. This is done by turning the LEDs on for 0.1 seconds, after which the LEDs go off again for 0.1 seconds. This continues until the pushbuttons 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. In addition, you can think of adjustments or additions to the code yourself.
import RPi.GPIO as GPIO
import time
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)
Once you have successfully completed this project, you will know how and what to use the Raspberry Pi GPIO pins for.