You are on page 1of 5

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 RPi.GPIO as GPIO # import GPIO library
import time # import time library
GPIO.setmode(GPIO.BOARD) # set the BOARD with pin numbers
GPIO.setup(8, GPIO.OUT) # initialize pins as output
GPIO.setup(10, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)
GPIO.setup(32, GPIO.OUT)
GPIO.output(8, False) # initialize the pins as in LOW state (LED OFF)
GPIO.output(10, False)
GPIO.output(16, False)
GPIO.output(18, False)
GPIO.output(22, False)
GPIO.output(24, False)
GPIO.output(26, False)
GPIO.output(32, False)
while (True):
GPIO.output(8, True) # if the state is True (Logic HIGH), turn ON LED
GPIO.output(10, True)
GPIO.output(16, True)
GPIO.output(18, True)
GPIO.output(22,True)
GPIO.output(24,True)
GPIO.output(26,True)
GPIO.output(32,True)
print("LED ON") # Print the message on the console
time.sleep(2) # wait for time 2 sec
GPIO.output(8, False) # if the state is False (Logic LOW), turn OFF LED
GPIO.output(10, False)
GPIO.output(16, False)
GPIO.output(18, False)
GPIO.output(22, False)
GPIO.output(24, False)
GPIO.output(26, False)
GPIO.output(32, False)
print("LED OFF") # Print the message on the console
time.sleep(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 RPi.GPIO as GPIO # import GPIO library
import time # import time library
GPIO.setmode(GPIO.BOARD) # set the BOARD with pin numbers
GPIO.setup(8, GPIO.OUT) # initialize pins as output
GPIO.setup(10,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
GPIO.setup(26,GPIO.OUT)
GPIO.setup(32,GPIO.OUT)
GPIO.output(8,False)
GPIO.output(10,False) # initialize the pins as in LOW state (LED OFF)
GPIO.output(16,False)
GPIO.output(18,False)
GPIO.output(22,False)
GPIO.output(24,False)
GPIO.output(26,False)
GPIO.output(32,False)
while(True):
GPIO.output(8,True) # if the state is True (Logic HIGH), turn ON LED
time.sleep(0.2) # wait for 0.2 sec
GPIO.output(10,True)
time.sleep(0.2)
GPIO.output(16,True)
time.sleep(0.2)
GPIO.output(18,True)
time.sleep(0.2)
GPIO.output(22,True)
time.sleep(0.2)
GPIO.output(24,True)
time.sleep(0.2)
GPIO.output(26,True)
RASPBERRY Pi – CONTROLLING OF LEDs

time.sleep(0.2)
GPIO.output(32,True)
time.sleep(0.2)
GPIO.output(8,False) # if the state is False (Logic LOW), turn OFF LED
time.sleep(0.2) # wait for 0.2 sec
GPIO.output(10,False)
time.sleep(0.2)
GPIO.output(16,False)
time.sleep(0.2)
GPIO.output(18,False)
time.sleep(0.2)
GPIO.output(22,False)
time.sleep(0.2)
GPIO.output(24,False)
time.sleep(0.2)
GPIO.output(26,False)
time.sleep(0.2)
GPIO.output(32,False)
time.sleep(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 RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8,GPIO.OUT)
GPIO.setup(10,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
GPIO.setup(26,GPIO.OUT)
GPIO.setup(32,GPIO.OUT)
GPIO.output(8,False)
GPIO.output(10,False)
GPIO.output(16,False)
GPIO.output(18,False)
GPIO.output(22,False)
GPIO.output(24,False)
GPIO.output(26,False)
RASPBERRY Pi – CONTROLLING OF LEDs

GPIO.output(32,False)
while(True):
GPIO.output(10,False)
GPIO.output(26,False)
GPIO.output(8,True)
GPIO.output(32,True)
time.sleep(0.5)
GPIO.output(8,False)
GPIO.output(32,False)
GPIO.output(10,True)
GPIO.output(26,True)
time.sleep(0.5)
GPIO.output(10,False)
GPIO.output(26,False)
GPIO.output(16,True)
GPIO.output(24,True)
time.sleep(0.5)
GPIO.output(16,False)
GPIO.output(24,False)
GPIO.output(18,True)
GPIO.output(22,True)
time.sleep(0.5)
GPIO.output(18,False)
GPIO.output(22,False)
GPIO.output(16,True)
GPIO.output(24,True)
time.sleep(0.5)
GPIO.output(16,False)
GPIO.output(24,False)
GPIO.output(10,True)
GPIO.output(26,True)
time.sleep(0.5)
# repeats the procedure in the while LOOP for infinite iteration.
# To stop the program press STOP button on IDE.
--------------------------------------------------------------------------------------------------

You might also like