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.

Materials needed

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.

Image for post
Image for post
Fig 1. GPIO board with the pins explained.

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.

Image for post
Image for post
Fig 2. Breadboard

It has three different sections:

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.

Image for post
Image for post
Fig 3. Source: Instructables.com (link)

Single LED

After connecting the LEDs, our circuit looks something like this for a single LED setup.

Image for post
Image for post
Fig 4. Breadboard circuit for a single LED

Note

Multiple LEDs

And this is how the circuit looks for Multiple LED setup.

Image for post
Image for post
Fig 5. Breadboard circuit for multiple LEDs

Note:

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.

Note that we are passing HIGH and LOW voltage to the GPIO pin number 17 to control our LED light. Run the code to see if it works.

python single_led.py

Code for Multiple LEDs

The following code snippet can be used to control multiple LEDs.

Note that we are passing HIGH and LOW voltage to the GPIO pin number 17, 18, and 22 to control the 3 LEDs respectively. You can run the code and see if it works.

python multiple_led.py

Written by

Grad Student at ASU | Student Researcher at The Luminosity Lab | Ex Senior Software Engineer, Zeta | Volunteer, Wikimedia Foundation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store