Interfacing
of Raspberry pi with Led:
1) Led Blinking for Finite Time:
Now ready
to write code to switch the LED on and LED off. Turn on your Raspberry Pi
and Open the terminal window.
Create a
new text file “led1.py” by typing the following:
Sudo nano led1.py
After
that write following code to the Text editer page:
import
RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM) #board usage gpio pin , bcm usasge pin mnumber
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
print "LED on"
GPIO.output(18,GPIO.HIGH)
time.sleep(1)
print "LED off"
GPIO.output(18,GPIO.LOW)
import time
GPIO.setmode(GPIO.BCM) #board usage gpio pin , bcm usasge pin mnumber
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
print "LED on"
GPIO.output(18,GPIO.HIGH)
time.sleep(1)
print "LED off"
GPIO.output(18,GPIO.LOW)
After
writing the code we checked it, save and exit the text editor with
“Ctrl +
x” then “y” then “enter”.
To
Running this Code Type:
sudo python led1.py
then
You will
see the LED turn on for a second and then turn off for a second.
2)
Led Blinking for Infinite Time:
Now ready
to write code to switch the LED on and LED off for infinite time. Turn on
your Raspberry Pi and Open the terminal window.
Create a
new text file “led2.py” by typing the following:
Sudo nano led2.py
After that write following code to the Text editer
page:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)#board
usage gpio pin , bcm usasge pin mnumber
GPIO.setwarnings(False)
for i in range(2,5):
GPIO.setup(i,GPIO.OUT)
while(1):
for i in range(2,5):
GPIO.output(i,True)
print("true")
sleep(1)
GPIO.output(i,False)
print("false")
sleep(1)
After
writing the code we checked it, save and exit the text editor with
“Ctrl +
x” then “y” then “enter”.
To
Running this Code Type:
sudo python led2.py
then
You will
see the Leds turn on and turn off for
infinite time .
Circuit
Diagram:
0 comments:
Post a Comment
if you have any doubt please let me know