RASPBERRY Pi – CONTROLLING OF LEDs
STUDY OF LED CONTROLLING USING RASPBERRY Pi
AIM: Programming of Raspberry Pi to control LEDs
APPARATUS: Raspberry Pi with Raspbian Pi OS, LEDs, Resistors, connecting wires, USB
Keyboard, mouse LCD Monitor, HDMI to VGA converter, 5V Power Adapter.
CIRCUIT DIAGRAM:
1kΩ
8
1kΩ
10
1kΩ
16
1kΩ
18
RASPBERRY Pi 1kΩ
3B + 22
1kΩ
24
1kΩ
26
1kΩ
32
39
Procedure:
1. The kit consists of Raspberry Pi 3B+, Power adapter, HDMI to VGA converter cable and
PCB with LEDs connected and the 9 pin connector.
2. Connect the circuit as shown in the circuit above. Use Female to Female connecting
wires to connect Raspberry Pi pins to the Led pins available on the PCB.
3. Run the program using “run” button in the IDE, it will ask you to save the file. If not
saved before, save it with file name extension .py
4. You will observe the flashing of the LEDs after every two seconds duration. You can
change the time duration by changing the time in seconds.
5. Now load the other programs, save with other name and observe the working according
to the program.
RASPBERRY Pi – CONTROLLING OF LEDs
Program 1: Toggling of all LEDs simultaneously
#
# Program for flashing all LEDs which are interfaced to Raspberry Pi
# pin numbers 8, 10, 16, 18, 22, 24, 26, 32 are used to connect LEDs
#
import [Link] as GPIO # import GPIO library
import time # import time library
[Link]([Link]) # set the BOARD with pin numbers
[Link](8, [Link]) # initialize pins as output
[Link](10, [Link])
[Link](16, [Link])
[Link](18, [Link])
[Link](22, [Link])
[Link](24, [Link])
[Link](26, [Link])
[Link](32, [Link])
[Link](8, False) # initialize the pins as in LOW state (LED OFF)
[Link](10, False)
[Link](16, False)
[Link](18, False)
[Link](22, False)
[Link](24, False)
[Link](26, False)
[Link](32, False)
while (True):
[Link](8, True) # if the state is True (Logic HIGH), turn ON LED
[Link](10, True)
[Link](16, True)
[Link](18, True)
[Link](22,True)
[Link](24,True)
[Link](26,True)
[Link](32,True)
print("LED ON") # Print the message on the console
[Link](2) # wait for time 2 sec
[Link](8, False) # if the state is False (Logic LOW), turn OFF LED
[Link](10, False)
[Link](16, False)
[Link](18, False)
[Link](22, False)
[Link](24, False)
[Link](26, False)
[Link](32, False)
print("LED OFF") # Print the message on the console
[Link](2) # wait for 2 sec
RASPBERRY Pi – CONTROLLING OF LEDs
# repeats the procedure in the while LOOP for infinite iteration.
# To stop the program press STOP button on IDE.
------------------------------------------------------------------------------------------------------
Program 2: Flashing of LEDs Turn ON from left to right one by one and turn off from left
to right one by one in sequence.
#
# Program to interface LED to Raspberry Pi and Flashing them IN- OUT and OUT-IN way.
#pin numbers 8, 10, 16, 18, 22, 24, 26, 32 are used to connect LEDs
#
import [Link] as GPIO # import GPIO library
import time # import time library
[Link]([Link]) # set the BOARD with pin numbers
[Link](8, [Link]) # initialize pins as output
[Link](10,[Link])
[Link](16,[Link])
[Link](18,[Link])
[Link](22,[Link])
[Link](24,[Link])
[Link](26,[Link])
[Link](32,[Link])
[Link](8,False)
[Link](10,False) # initialize the pins as in LOW state (LED OFF)
[Link](16,False)
[Link](18,False)
[Link](22,False)
[Link](24,False)
[Link](26,False)
[Link](32,False)
while(True):
[Link](8,True) # if the state is True (Logic HIGH), turn ON LED
[Link](0.2) # wait for 0.2 sec
[Link](10,True)
[Link](0.2)
[Link](16,True)
[Link](0.2)
[Link](18,True)
[Link](0.2)
[Link](22,True)
[Link](0.2)
[Link](24,True)
[Link](0.2)
[Link](26,True)
RASPBERRY Pi – CONTROLLING OF LEDs
[Link](0.2)
[Link](32,True)
[Link](0.2)
[Link](8,False) # if the state is False (Logic LOW), turn OFF LED
[Link](0.2) # wait for 0.2 sec
[Link](10,False)
[Link](0.2)
[Link](16,False)
[Link](0.2)
[Link](18,False)
[Link](0.2)
[Link](22,False)
[Link](0.2)
[Link](24,False)
[Link](0.2)
[Link](26,False)
[Link](0.2)
[Link](32,False)
[Link](0.2)
# repeats the procedure in the while LOOP for infinite iteration.
# To stop the program press STOP button on IDE.
-----------------------------------------------------------------------------------
Program 3: Flashing of LEDs Turn ON in sequence 1-8, 2-7, 3-6, 4-5 and afrom left to right
one by one and turn off from left to right one by one in sequence.
# Program to interface LED to Raspberry Pi
import [Link] as GPIO
import time
[Link]([Link])
[Link](8,[Link])
[Link](10,[Link])
[Link](16,[Link])
[Link](18,[Link])
[Link](22,[Link])
[Link](24,[Link])
[Link](26,[Link])
[Link](32,[Link])
[Link](8,False)
[Link](10,False)
[Link](16,False)
[Link](18,False)
[Link](22,False)
[Link](24,False)
[Link](26,False)
RASPBERRY Pi – CONTROLLING OF LEDs
[Link](32,False)
while(True):
[Link](10,False)
[Link](26,False)
[Link](8,True)
[Link](32,True)
[Link](0.5)
[Link](8,False)
[Link](32,False)
[Link](10,True)
[Link](26,True)
[Link](0.5)
[Link](10,False)
[Link](26,False)
[Link](16,True)
[Link](24,True)
[Link](0.5)
[Link](16,False)
[Link](24,False)
[Link](18,True)
[Link](22,True)
[Link](0.5)
[Link](18,False)
[Link](22,False)
[Link](16,True)
[Link](24,True)
[Link](0.5)
[Link](16,False)
[Link](24,False)
[Link](10,True)
[Link](26,True)
[Link](0.5)
# repeats the procedure in the while LOOP for infinite iteration.
# To stop the program press STOP button on IDE.
--------------------------------------------------------------------------------------------------