Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Programming of Raspberry Pi to control LEDs

Programming of Raspberry Pi to control LEDs

Become More Then A coder | Learn & Start Coding Now.
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:
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.
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
# 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)
 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)
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)

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.