Freemindscafe

Raspberry pi - GPIO (blinking the LEDs)

When i thought about exploring the GPIO (general purpose input output) of raspberry pi, I had so many doubts on where to start. In the end nothing works better than working via simple examples. The following is a very basic sample which does the yellow and green led blinking.

Yellow LED = PIN NO. 23
Green LED = PIN NO. 18

Following is the breadboard layout.

Following is simple python based program.

#!/usr/bin/env python

import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import 'time' library. Allows us to use 'sleep'

GPIO.setmode(GPIO.BCM)
GREEN_LED = 18
YELLOW_LED = 23
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(YELLOW_LED, GPIO.OUT)
GPIO.output(GREEN_LED,False)
GPIO.output(YELLOW_LED,False)


##Define a function named Blink()
def Blink(numTimes,speed):
        for i in range(0,numTimes):## Run loop numTimes
                print "Iteration " + str(i+1)## Print current loop
                GPIO.output(GREEN_LED,True)
                GPIO.output(YELLOW_LED,True)
                time.sleep(speed)
                GPIO.output(GREEN_LED,False)
                GPIO.output(YELLOW_LED,False)
                time.sleep(speed)
                print "Done" ## When loop is complete, print "Done"
        GPIO.cleanup()

## Ask user for total number of blinks and length of each blink
iterations = raw_input("Enter total number of times to blink: ")
speed = raw_input("Enter length of each blink(seconds): ")

## Start Blink() function. Convert user input from strings to numeric data types and pass to Blink() as parameters
Blink(int(iterations),float(speed))