Wednesday, December 18, 2013

Raspberry Pi with LED ON/OFF

We did a very simple experiment on interfacing a single LED to the GPIO of Raspberry Pi.

The basic circuit diagram is shown below:



Here, we used GPIO8 of the Raspberry Pi and connected it to the LED through a current limiting resistor of 200E.

The python code to blink the LED ON/OFF continuously is shown below:

import RPi.GPIO as GPIO
import time
pinNum = 8
GPIO.setmode(GPIO.BCM) #numbering scheme that corresponds to breakout board and pin layout
GPIO.setup(pinNum,GPIO.OUT) #replace pinNum with whatever pin you used, this sets up that pin as an output
#set LED to flash forever
while True:
  GPIO.output(pinNum,GPIO.HIGH)
  time.sleep(0.5)
  GPIO.output(pinNum,GPIO.LOW) 
  time.sleep(0.5)


To run the above code, save it in any file, say led.py using any text editor. Once saved, run this command to see the LED in action:
sudo python led.py




Change with the different GPIO pins and have a fun...................

No comments:

Post a Comment

python class topic video