Saturday, December 28, 2013

RPi - Python and Blinking LED

I successfully created a python script to blink an LED attached to the RPi/Pi Cobbler.

The circuit attaches to the 3.3V output, routes through the LED, a 470R, and back to GPIO pin 18.

I wrote it using IDLE on the RPi GUI.

I used this blog post as my guide: http://www.thirdeyevis.com/pi-page-2.php
I also referred to this one and even started to set up the WebIDE: http://www.akeric.com/blog/?tag=pi-cobbler

The code:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

def Blink(numTimes,speed):
    for i in range(0,numTimes):
        print "Iteration " + str(i+1)
        GPIO.output(18,True)
        time.sleep(speed)
        GPIO.output(18,False)
        time.sleep(speed)
    print "Done"
    GPIO.cleanup()

iterations = raw_input("Enter total number of times to blink: ")
speed = raw_input("Enter length of each blink(seconds): ")

Blink(int(iterations),float(speed))


No comments:

Post a Comment