Saturday, August 01, 2015

Blinking LEDs using Raspberry Pi

My first project of blinking LEDs .

A very simple project involving a 
- Raspberry Pi Model B.
- Resistor of 270 ohms ( 1 rupee at SP road)
- A LED  ( 1 rupee )
- Jumper wires
- Some simple python code.

Important note: I used Pin 40 for Output and Pin 3 for Gnd.

Things I learnt:

First try to connect the 3.3V ouput ( i.e Pin 1 ) to a 270 ohms resistor -> Anode ( long end LED) and the cathode of the LED has to be connected to Pin 6, ( or GND) .
See that the LED blinks and once you are assured that the LED is good, resistor is good and connections are good, then you can try to control the LED by making it blink.

Next, the python code has to be instructed to use the correct mode to communicate to the board. ie. GPIO.setmode ( ). It can be either GPIO.BCM or GPIO.BOARD.
In my case I am using BOARD, The GPIO.BOARD option specifies that you are referring to the pins by the number of the pin the the plug - i.e the numbers printed on the board (e.g. P1)

http://raspberrypi.stackexchange.com/questions/12966/what-is-the-difference-between-board-and-bcm-for-gpio-pin-numbering

== Cut and Paste of LED blinking python code. ==
import time
import RPi.GPIO as GPIO
from time import sleep

GPIO.cleanup()

GPIO.setmode(GPIO.BOARD)

GPIO.setup(40,GPIO.OUT)

try:
    while True:
        GPIO.output(40, 1)         # set GPIO24 to 1/GPIO.HIGH/True
        sleep(0.5)                 # wait half a second
        GPIO.output(40, 0)         # set GPIO24 to 0/GPIO.LOW/False
        sleep(0.5)                 # wait half a second

except KeyboardInterrupt:          # trap a CTRL+C keyboard interrupt
    GPIO.cleanup()                 # resets all GPIO ports used by this program

The pin diagram I used was from this link.



0 Comments:

Post a Comment

<< Home