Blinking LED is basis any programming. This is complete code for blinking LED, turn ON or turn OFF LED on GPIO 7. The program is written in python language and you find complete tutorial.
You have to create file *.py in the home folder.
Copy this code:
import RPi.GPIO as GPIO ## Import GPIO Library
import time ## Import ‘time’ library. Allows us to use ‘sleep’
GPIO.setmode(GPIO.BOARD) ## Use BOARD pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO pin 7 to OUT
## Define 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(7, True) ## Turn on GPIO pin 7
time.sleep(speed) ## Wait
GPIO.output(7, False) ## Switch off GPIO pin 7
time.sleep(speed) ## Wait
print “Done” ## When loop is complete, print “Done”
GPIO.cleanup()
## Prompt user for input
stav = int(raw_input(“On-1, Blink -2, Off -3: “))
if stav == 1:
GPIO.output(7, True) #Turn on GPIO on 7
print “GPIO.7 is ON”
#GPIO.cleanup()
print “Done”
elif stav == 2:
iterations = int(raw_input(“Enter the total number of times to blink: “))
speed = raw_input(“Enter the lenght of each blink in seconds: “)
## Start Blink() function. Convert user input from strings to numeric data types and pass to Blink() as parameters
Blink(int(iterations),float(speed))
else:
GPIO.output(7, False)
print “GPIO.7 is OFF”
GPIO.cleanup()
print “Done”
This command is used for running blink.py:
Control program is easy. You will have to write number 1- Turn ON LED, 2 -Blinking LED a 3 – Turn OFF LED.
Choice 2 – You have to specify number repeating and blinking period.
If you turn ON LED and run program again, appears this message:
GPIO.setup(7, GPIO.OUT) ## Setup GPIO pin 7 to OUT
This message is no problem, because only highlights that the GPIO 7 is used.
Source of:
Pinout of Raspberry Pi:
https://projects.drogon.net/raspberry-pi/wiringpi/pins/
Original program: