Controlling multiple LEDs using Raspberry Pi’s GPIO ports
In this post, I will explain how you can control multiple LED lights using a Raspberry Pi. The whole thing will hardly take 15–20 minutes if you have all the pieces of equipment.
The easiest way to get all the additional materials(apart from Raspberry Pi) is to get a starter kit instead of buying everything individually. I bought all the items from Amazon but you can find it anywhere.
What is a GPIO Board?
According to Wikipedia, a GPIO board is defined as:
A general-purpose input/output (GPIO) is an uncommitted digital signal pin on an integrated circuit or electronic circuit board whose behavior — including whether it acts as input or output — is controllable by the user at run time.
The Raspberry Pi has 2 rows of GPIO pins which can be used to connect LEDs or can be used to send signals to other devices. These pins facilitate the communication of Raspberry Pi with the outside world and let it control electronics and other devices.
The Raspberry Pi 3 has 26 GPIO pins, the rest of the pins are power, ground, or “other”.
You don’t need to understand all the pins of the GPIO board as we would be using just a few of the pins.
What is Breadboard?
A breadboard is a solderless board that makes it easy to prototype the electronic circuits. Its a plastic board with a grid of holes that are internally connected in specific ways using metal strips. As the breadboard does not need soldering, it makes it very easy to experiment and try out different combinations.
It has three different sections:
- The red power bus is used to connect power to the breadboard. You can connect power to any of the tie-points in the column.
- The blue ground bus which is used to connect ground to the breadboard. You can connect ground to any of the tie-points in the column.
- Rows of tie points from a to j. The left side tie-points are connected (A-B-C-D-E), and the right side tie-points are connected (F-G-H-I-J).
In our example, we would be using 2 LED lights but you can easily extend it to use 3–4 LED lights. In the next section, we will go ahead and build our own circuit using the breadboard, GPIO pins, LEDs, resistors, and jumper wires.
Building the circuit
In our example, we would be using 3LED lights but you can easily extend it to use more LED lights. Build a circuit using the breadboard, GPIO pins, LEDs, resistors, and jumper wires as illustrated in the diagram below.
After connecting the LEDs, our circuit looks something like this for a single LED setup.
- The circuit shown in Fig 3. instructs to use RPi Pin 6 (GND) to connect the black jumper wire. But you can use any GND pin if your Pin 6 is already in use. In Fig. 4 the jumper wire is connected to some other GND as Pin 6 was not free.
- We are connecting the red jumper wire to GPIO Pin 17.
And this is how the circuit looks for Multiple LED setup.
- We are using GPIO Pin 17, 18, and 22 for connecting the jumper wires with the Raspberry Pi. These ports numbers would be used later in the python code.
Now, that we have the circuits ready, we will go ahead and write simple python programs to control these LEDs.
Install the package
You might need to install the following package on your Raspberry Pi if it is not already installed.
pip install RPi.GPIO
This package will make it easy to communicate with Raspberry Pi’s GPIO pins.
Code for Single LED
The following code snippet can be used to control a single LED.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(17,GPIO.OUT) print "LED on" GPIO.output(17,GPIO.HIGH) time.sleep(10) print "LED off" GPIO.output(17,GPIO.LOW)
Note that we are passing
LOW voltage to the GPIO pin number 17 to control our LED light. Run the code to see if it works.
Code for Multiple LEDs
The following code snippet can be used to control multiple LEDs.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.setup(18, GPIO.OUT) GPIO.setup(22, GPIO.OUT) GPIO.output(17, True) time.sleep(3) GPIO.output(17, False) time.sleep(1) GPIO.output(18, True) time.sleep(3) GPIO.output(18, False) time.sleep(1) GPIO.output(22, True) time.sleep(3) GPIO.output(22, False) time.sleep(1)
Note that we are passing
LOW voltage to the GPIO pin number 17, 18, and 22to control the 3 LEDs respectively. You can run the code and see if it works.
You can buy me a coffee if this post really helped you learn something or fix a nagging issue!
Written on May 17, 2020 by Vivek Maskara.
Originally published on Medium