Programming of Raspberry Pi To Control LEDs Attached To The GPIO Pins
Programming of Raspberry Pi To Control LEDs Attached To The GPIO Pins
Abstract- These days computer is the basic need for every people in every field across the world.
Raspberry pi is small size or credit card sized computer. The Raspberry Pi is a small computer that can
do lots of things. You plug it into a monitor and attach a keyboard and mouse. There is a Broadcom
BCM2835 SOC to integrate major functional element into single chip. We can control any hardware
equipment through Raspberry Pi by using GPIO pins. Python programming languages are preferred to
program the Raspberry Pi. It allows monitoring and controlling of hardware equipment. There are
three series of Raspberry Pi, and several generations of each have been released. Raspberry Pi SBCs
feature a Broadcom system on a chip (SoC) with an integrated ARM-compatible central processing unit
(CPU) And on-chip graphics processing unit (GPU), while Raspberry PI Pico has a RP2040 system on
chip with an integrated ARM-compatible central processing unit (CPU)). Raspberry pi is a series of
small single-board computers developed in the United Kingdom by the Raspberry Pi foundation in
association with Broadcom. The Raspberry pi project originally learned towards the promotion of
teaching basic computer in schools and in developing countries. This paper explains the Raspberry Pi
with the explanation of GPIO pins, and through it showing the working or blinking of LED light. The
main task of doing it is that through it we can make home automation project, by which home smart
gadgets can be controlled from anywhere with the help of relay. The paper mainly emphasis on the
home automation project with the help of LED light and the coding is used to accomplish the task. The
Raspberry Pi has two zones of GPIO pins, which are the connections between the Raspberry Pi, and the
real world. Output pins are like switches that the Raspberry Pi can turn on and off. It can also send a
signal to another device.
The Raspberry Pi is a tiny and affordable computer The Raspberry Pi has a Broadcom BCM2835 system
that you can use to learn programming through fun, on a chip (SoC), which includes an ARM1176JZF-S
practical project. The Raspberry Pi is a credit-card 700 MHz processor •It has Video Core IV GPU •
sized computer originally shipped with 256 megabytes of RAM, later
© 2022 Priyanka Dubey. This is an Open Access article distributed under the terms of the Creative Commons Attribution License
(http://creativecommons.org/licenses/by/4.0), which permits unrestricted use, distribution, and reproduction in any medium, provided the
original work is properly credited.
Priyanka Dubey. International Journal of Science, Engineering and Technology, 2022, 10:3 International Journal of Science,
Engineering and Technology
An Open Access Journal
upgraded to 512MB. • It does not include a built-in ensure every child has access to learning about
hard disk but uses an SD card for booting and long- computing. Similarly, Raspberry Jams are Raspberry
term storage. Pi-focused events for people of all ages to come
together to learn about Raspberry Pi and share ideas
It has 10/100 Base T Ethernet socket, HDMI socket, and projects and trains educators who can guide
USB 2.0 socket, RCA video socket, SD card socket, other people to learn.
Powered from microUSB socket • 3.5mm audio out
jack • Header footprint for camera connection is What Raspberry Pi models have been released?
there in Raspberry Pi There have been many generations of the Raspberry
Pi line: from Pi 1 to 4, and even a Pi 400. There has
generally been a Model A and a Model B of most
generations. Model A has been a less expensive
variant and tends to have reduced RAM and fewer
ports (such as USB and Ethernet). The Pi Zero is a
spinoff of the original (Pi 1) generation, made even
smaller and cheaper.
IV. APPLICATIONS:
V. GENERATIONS OF RASPBERRY PI
1. The Timeline:
• The Raspberry Pi 2 which added more RAM was
released in February 2015.
• Raspberry Pi 3 Model B released in February
2016, is bundled with on-board WiFi, Bluetooth
and USB boot capabilities.
Fig 4. Solar Raspberry Pi Power Pack.
Priyanka Dubey. International Journal of Science, Engineering and Technology, 2022, 10:3 International Journal of Science,
Engineering and Technology
An Open Access Journal
• As of January 2017, Raspberry Pi 3 Model B is the turn on or off (input) or that the Pi can turn on or off
newest mainline Raspberry Pi. (output).
• Raspberry Pi boards are priced between US$5–
35. One powerful feature of the Raspberry Pi is the row
• As of 28 February 2017, the Raspberry Pi Zero W of GPIO pins along the top edge of the board. GPIO
was launched, which is identical to the Raspberry stands for General-Purpose Input/Output. These pins
Pi Zero, but has the Wi-Fi and Bluetooth are a physical interface between the Raspberry Pi and
functionality of the Raspberry Pi 3 for US$10. the outside world. At the simplest level, you can
think of them as switches that you can turn on or off
2. Features: (input) or that the Pi can turn on or off (output).
• All models feature a Broadcom system on a
chip (SoC), which includes an ARM compatible The GPIO pins allow the Raspberry Pi to control and
central processing unit (CPU) and an on-chip monitor the outside world by being connected to
graphics processing unit (GPU, a Video Core IV). electronic circuits. The Pi is able to control LEDs,
• CPU speed ranges from 700 MHz to 1.2 GHz turning them on or off, run motors, and many other
for the Pi 3 and on board memory range from things. It’s also able to detect whether a switch has
256 MB to 1 GB RAM. been pressed, the temperature, and light. We refer to
• Secure Digital (SD) cards are used to store this as physical computing.
the operating system and program memory in
either the SDHC or Micro SDHC sizes. There are 40 pins on the Raspberry Pi (26 pins on
• Most boards have between one and four USB early models), and they provide various different
slots, HDMI, and composite video output, and a functions.
3.5 mm phono jack for audio.
• Lower-level output is provided by a number of If you have a RasPiO pin label, it can help to identify
GPIO pins which support common protocols like what each pin is used for. Make sure your pin label is
I²C. placed with the keyring hole facing the USB ports,
• The B-models have an 8P8C Ethernet port and pointed outwards.
the Pi 3 and Pi Zero W have on board Wi-Fi
802.11n and Bluetooth.
3. Operating System:
The Foundation provides Raspbian, a Debian based
Linux distribution for download, as well as third party
Ubuntu, Windows 10 IOT Core, RISC OS, and
specialized media centre distributions.
• It promotes Python and Scratch as the
main programming language, with support for
many other languages.
• The default firmware is closed source, while Fig 7. GPIO pin numbering.
an unofficial open source is available.
When programming the GPIO pins, there are two
different ways to refer to them: GPIO numbering and
VI. GPIO PINS IN RASPBERRY PI physical numbering. Throughout this course (and in
all our resources) we will refer to the pins using the
A standard interface for connecting a single-board GPIO numbering scheme. These are the GPIO pins as
computer or microprocessor to other components is the computer sees them.
through General-Purpose Input/Output (GPIO) pins.
GPIO stands for General-Purpose Input/Output. The numbering of the GPIO pins is not in numerical
order, instead relating to the numbering on the CPU
These pins are a physical interface between the of the Raspberry Pi, so there is no easy way to
Raspberry Pi and the outside world. At the simplest remember them. However, you can use a reference
level, you can think of them as switches that you can board that fits over the pins, a printed reference (like
Priyanka Dubey. International Journal of Science, Engineering and Technology, 2022, 10:3 International Journal of Science,
Engineering and Technology
An Open Access Journal
the image above), or a website guide to the GPIO speed in which the devices to transfer data to
pins to help you. each other.
• UART pins – UART stands for universal
1. Voltages: asynchronous receiver-transmitter, which is a
The voltage of a pin is labeled on the reference physical circuit designed to send and receive
guide. There are two 5V pins and two 3V3 pins, as data.
well as a number of ground pins (0V), which are • PWM pins – PWM means “pulse width
unconfigurable. The remaining pins are all general- modulation,” which is a communication protocol
purpose 3V3 pins, meaning that the outputs are set best used with stuff that moves and lights up:
to 3.3 volts and the inputs are tolerant of 3.3 volts. motors, LEDs, and so on.
• I2C pins – I2C is short for inter-integrated circuit
A GPIO pin designated as an output pin can be set to (two “inters” or Squared). It works similarly to SPI,
high (3.3V) or low (0V). Components are usually but it doesn’t force you to use nearly so many
attached so that setting the output to high will allow pins.
current to flow to them, while setting the output to
low won’t. GPIO are your standard pins that simply be used to
turn devices on and off. For example, a LED.
A GPIO pin that is designated as an input will allow a • I2C (Inter-Integrated Circuit) pins allow you to
signal to be received by the Raspberry Pi. The connect and talk to hardware modules that
threshold between a high and a low signal is around support this protocol (I2C Protocol). This will
1.8V. A voltage between 1.8V and 3.3V will be read typically take up 2 pins.
by the Raspberry Pi as high; anything lower than 1.8V SPI (Serial Peripheral Interface Bus) pins can be
will be read as low. Do not allow an input voltage used to connect and talk to SPI devices. Pretty
above 3.3V, or else you will fry your Pi! much the same as I2C but makes use of a
different protocol.
The GPIO header provides the following power and UART (Universal asynchronous receiver/
interface options: transmitter) are the serial pins used to
• 3.3V (on 2 pins) communicate with other devices.
• 5V (on 2 pins) DNC stands for do not connect, this is self-
• Ground (on 8 pins) explanatory.
• General purpose input and output The power pins pull power directly from the
• PWM (pulse width modulation) Raspberry Pi.
• I2C GPID are the pins you use to ground your
• I2S devices. It doesn’t matter which pin you use as
• SPI they are all connected to the same line.
Page 5 of 11
Priyanka Dubey. International Journal of Science, Engineering and Technology, 2022, 10:3 International Journal of Science,
Engineering and Technology
An Open Access Journal
graphics that could boot into the programming physical components: a new friendly Python API
environment and not break the bank. called GPIO Zero.
The Raspberry Pi is a credit card sized single-board The RPi. GPIO library is bare bones and provides all
computer with an open-source platform that has a the essential functionality to do simple things with
thriving community of its own, similar to that of the the Pi's GPIO pins—set up pins as inputs or outputs,
Arduino. It can be used in various types of projects read inputs, set outputs high or low, and so on. GPIO
from beginners learning how to code to hobbyists Zero is built on top of this and provides a collection
designing home automation systems. of simple interfaces to everyday components, so
rather than setting pin 2 high to turn on an LED, you
There are a few versions of the Raspberry Pi, but the have an LED object and you turn it on.
latest version, has improved upon its predecessor in
terms of both form and functionality. VIII. PROGRAMMING LED WITH GPIO
The Raspberry Pi Model B features:
GPIO are your standard pins that simply be used to
• More GPIO
turn devices on and off. For example, a LED.
• More USB
• Micro SD
• Lower power consumption
• Better audio
• Neater form factor
To get started you need a Raspberry Pi 3 Model B, a Fig 8. LED (Light Emitting doide).
5V USB power supply of at least 2 amps with a
micro-USB cable, any standard USB keyboard and I2C (Inter-Integrated Circuit) pins allow you to
mouse, an HDMI cable and monitor/TV for display, connect and talk to hardware modules that support
and a micro SD card with the operating system pre- this protocol (I2C Protocol). This will typically take up
installed. The NOOBS (New Out Of the Box Software) 2 pins.
OS is recommended for beginners, and you may
choose one of several from the download page. SPI (Serial Peripheral Interface Bus) pins can be used
to connect and talk to SPI devices. Pretty much the
This pin out diagram will help you get familiar with same as I2C but makes use of a different protocol.
the layout of the board and get started in immersing
yourself into your own passion projects. UART (Universal asynchronous receiver/transmitter)
are the serial pins used to communicate with other
One of the most exciting starter activities to do with devices.
a Raspberry Pi is something you can't do on your • DNC stands for do not connect, this is self-
regular PC or laptop—make something happen in explanatory.
the real world, such as flash an LED or control a • The power pins pull power directly from the
motor. If you've done anything like this before, you Raspberry Pi.
probably did it with Python using the RPi.GPIO • GND are the pins you use to ground your
library, which has been used in countless projects. devices. It doesn’t matter which pin you use as
There's now an even simpler way to interact with they are all connected to the same line.
Priyanka Dubey. International Journal of Science, Engineering and Technology, 2022, 10:3 International Journal of Science,
Engineering and Technology
An Open Access Journal
original Rev 1 Pi is slightly different), looking at the LEDs have many advantages over incandescent light
Raspberry Pi with the pins in the top right corner. sources, including lower power consumption, longer
The new 40 pin Raspberry Pi’s shares exactly the lifetime, improved physical robustness, smaller size,
same layout of pins for the top 13 rows of GPIO pins. and faster switching. In exchange for these generally
favorable attributes, disadvantages of LEDs include
IX. BUILDING THE CIRCUIT electrical limitations to low voltage and generally to
DC (not AC) power, inability to provide steady
illumination from a pulsing DC or an AC electrical
• The circuit consists of a power supply (the
supply source, and lesser maximum operating
Raspberry Pi), an LED that lights when the power
temperature and storage temperature.
is applied, and a resistor to limit the current that
can flow through the circuit.
In contrast to LEDs, incandescent lamps can be made
• You will be using one of the ‘ground’ (GND) pins
to intrinsically run at virtually any supply voltage, can
to act like the ‘negative’ or 0 volt ends of a
utilize either AC or DC current interchangeably, and
battery. The ‘positive’ end of the battery will be
will provide steady illumination when powered by AC
provided by a GPIO pin. Here we will be using
or pulsing DC even at a frequency as low as 50 Hz.
pin 18. When they are ‘taken high’, which means
LEDs usually need electronic support components to
it outputs 3.3 volts, the LED will light. Now take a
function, while an incandescent bulb can and usually
look at the circuit diagram below.
does operate directly from an unregulated DC or AC
power source.
1. A Single LED:
A light-emitting diode (LED) is a semiconductor light
LEDs are made in different packages for different
source that emits light when current flows through it.
applications. A single or a few LED junctions may be
Electrons in the semiconductor recombine with
packed in one miniature device for use as an
electron holes, releasing energy in the form of
indicator or pilot lamp. An LED array may include
photons. The color of the light (corresponding to the
controlling circuits within the same package, which
energy of the photons) is determined by the energy
may range from a simple resistor, blinking or color
required for electrons to cross the band gap of the
changing control, or an addressable controller for
semiconductor.[5] White light is obtained by using
RGB devices. Higher-powered white-emitting devices
multiple semiconductors or a layer of light-emitting
will be mounted on heat sinks and will be used for
phosphor on the semiconductor device.[6]
illumination. Alphanumeric displays in dot matrix or
bar formats are widely available. Special packages
Appearing as practical electronic components in
permit connection of LEDs to optical fibers for high-
1962, the earliest LEDs emitted low-intensity infrared
speed data communication links.
(IR) light.[7] Infrared LEDs are used in remote-control
circuits, such as those used with a wide variety of
The circuit consists of a power supply (the Raspberry
consumer electronics. The first visible-light LEDs were
Pi), an LED that lights when the power is applied, and
of low intensity and limited to red. Early LEDs were
a resistor to limit the current that can flow through
often used as indicator lamps, replacing small
the circuit.
incandescent bulbs, and in seven-segment displays.
• Efficiency: LEDs emit more lumens per watt than
incandescent light bulbs.[138] The efficiency of
Recent developments have produced LEDs available
LED lighting fixtures is not affected by shape and
in visible, ultraviolet (UV), and infrared wavelengths,
size, unlike fluorescent light bulbs or tubes.
with high, low, or intermediate light output, for
• Color: LEDs can emit light of an intended color
instance white LEDs suitable for room and outdoor
without using any color filters as traditional
area lighting. LEDs have also given rise to new types
lighting methods need. This is more efficient and
of displays and sensors, while their high switching
can lower initial costs.
rates are useful in advanced communications
• Size: LEDs can be very small (smaller than 2 mm2
technology with applications as diverse as aviation
[139]) and are easily attached to printed circuit
lighting, fairy lights, automotive headlamps,
boards.
advertising, general lighting, traffic signals, camera
• Warmup time: LEDs light up very quickly. A
flashes, lighted wallpaper, horticultural grow lights,
typical red indicator LED achieves full brightness
and medical devices.[8]
Page 8 of 11
Priyanka Dubey. International Journal of Science, Engineering and Technology, 2022, 10:3 International Journal of Science,
Engineering and Technology
An Open Access Journal
in under a microsecond.[140] LEDs used in • You should turn your Raspberry Pi off for the
communications devices can have even faster next bit just in case you accidentally short
response times. something out.
• Cycling: LEDs are ideal for uses subject to • Use one of the jumper wires to connect a ground
frequent on-off cycling, unlike incandescent and pin to the rail, marked with blue, on the
fluorescent lamps that fail faster when cycled breadboard. The female end goes on the
often, or high-intensity discharge lamps (HID Raspberry Pi's pin, and the male end goes into a
lamps) that require a long time before restarting. hole on the breadboard.
• Dimming: LEDs can very easily be dimmed • Then connect the resistor from the same row on
either by pulse-width modulation or lowering the the breadboard to a column on the breadboard,
forward current. [141] this pulse-width as shown above.
modulation is why LED lights, particularly • Next, push the LEDs legs into the breadboard,
headlights on cars, when viewed on camera or by with the long leg (with the kink) on the right.
some people, seem to flash or flicker. This is a • Lastly, complete the circuit by connecting pin 18
type of stroboscopic effect. to the right-hand leg of the LED. This is shown
• Cool light: In contrast to most light sources, here with the orange wire.
LEDs radiate very little heat in the form of IR that
can cause damage to sensitive objects or fabrics. 2. The Code:
Wasted energy is dispersed as heat through the You are now ready to write some code to switch the
base of the LED. LED on. Turn on your Raspberry Pi and open the
• Slow failure: LEDs mainly fail by dimming over terminal window.
time, rather than the abrupt failure of
incandescent bulbs.[142] Create a new text file “LED.py” by typing the
• Lifetime: LEDs can have a relatively long useful following:
life. One report estimates 35,000 to 50,000 hours • nano LED.py
of useful life, though time to complete failure • Type in the following code:
may be shorter or longer.[143] Fluorescent tubes • import RPi.GP IO as GPIO
typically are rated at about 10,000 to 25,000 • import time
hours, depending partly on the conditions of use, • GPIO.setmode(GPIO.BCM)
and incandescent light bulbs at 1,000 to 2,000 • GPIO.setwarnings(False)
hours. Several DOE demonstrations have shown • GPIO.setup(18,GPIO.OUT)
that reduced maintenance costs from this • print "LED on"
extended lifetime, rather than energy savings, is • GPIO.output(18,GPIO.HIGH)
the primary factor in determining the payback • time.sleep(1)
period for an LED product.[144] • print "LED off"
• Shock resistance: LEDs, being solid-state • GPIO.output(18,GPIO.LOW)
components, are difficult to damage with
external shock, unlike fluorescent and Once you have typed all the code and checked it,
incandescent bulbs, which are fragile.[145] save and exit the text editor with “Ctrl + x” then “y”
• Focus: The solid package of the LED can be then “enter”.
designed to focus its light. Incandescent and
fluorescent sources often require an external 3. Running the Code:
reflector to collect light and direct it in a usable To run this code type:
manner. For larger LED packages total internal
reflection (TIR) lenses are often used to the same sudo python LED.py
effect. You will see the LED turn on for a second and then
turn off.
However, when large quantities of light are needed
many light sources are usually deployed, which are Explanation
difficult to focus or collimate towards the same So, what is happening in the code? Let’s go through
target. it a line at a time:
Page 9 of 11
Priyanka Dubey. International Journal of Science, Engineering and Technology, 2022, 10:3 International Journal of Science,
Engineering and Technology
An Open Access Journal
import RPi. GPIO as GPIO: A longer life, brighter illumination and less electricity
The first line tells the Python interpreter (the thing consumption are some of the reasons why LED is
that runs the Python code) that it will be using a being chosen above others.
‘library’ that will tell it how to work with the
Raspberry Pi’s GPIO pins. A ‘library’ gives a
programming language extra commands that can be
used to do something different that it previously did
not know how to do. This is like adding a new
channel to your TV so you can watch something
different.
import time:
Imports the Time library so that we can pause the
script later on.
GPIO.setmode(GPIO.BCM)
Each pin on the Raspberry Pi has several different
names, so you need to tell the program which
naming convention is to be used.
Fig 10. LED Demonstration.
print "LED on"
Entertainment and furnishing markets are also trying
This line prints some information to the terminal.
to make the most out of the latest LED innovations
as well. Use of LED technology is on a rise in these
GPIO.output(18, GPIO.HIGH)
sectors and many LED manufacturing companies
This turns the GPIO pin ‘on’. What this means is that
have started designing and manufacturing their
the pin is made to provide power of 3.3volts. This is
products exclusively for entertainment and
enough to turn the LED in our circuit on.
decorative sectors.
time. sleep (1)
Superbright blue, green, and white InGaN SQW LEDs
Pauses the Python program for 1 second
have been discussed in this book. By combining
high-power, high-brightness blue InGaN SQW LEDs,
print "LED off"
green InGaN SQW LEDs, and red GaAlAs LEDs, many
This line prints some information to the terminal.
kinds of applications, such as LED full-color displays
and LED white lamps for use in place of light bulbs or
GPIO.output(18, GPIO.LOW)
fluorescent lamps, are now possible with
This turns the GPIO pin ‘off’, meaning that the pin is
characteristics of high reliability, high durability, and
no longer supplying any power.
low energy consumption. Also, a white LED made by
combining a blue InGaN SQW LED and YAG
And that's it! You are now able to turn an LED on and
phosphor, which was less expensive than a white LED
off.
composed of three primary color LEDs, has been
developed. The luminous efficiencies of the blue,
X. CONCLUSIONS green, and white LEDs are 3, 10, and 5 lm/W, which
are much higher than that of conventional light
LED light is blinked when we programmed it the help bulbs (1 lm/W).
of whiteboard if we are not having female to female
jumper wires. if we are having female to male jumper Superbright blue, green, and white InGaN SQW LEDs
wire, then whiteboard is required. We have used have been discussed in this book. By combining
resistors, led, with the help of python programming. high-power, high-brightness blue InGaN SQW LEDs,
green InGaN SQW LEDs, and red GaAlAs LEDs, many
LED innovations across various sectors including kinds of applications, such as LED full-color displays
hospitality, retail and entertainment among others. and LED white lamps for use in place of light bulbs or
Priyanka Dubey. International Journal of Science, Engineering and Technology, 2022, 10:3 International Journal of Science,
Engineering and Technology
An Open Access Journal
Page 11 of 11