Blinking LEDs with Python on a Raspberry Pi

I have been curious about using Python to access the GPIO pins on a Raspberry Pi.  Here is a brief description of the code and some things I learned in the process.  See the video here: https://youtu.be/mGNyRZzSpLs 

  1. import RPi.GPIO as GPIO… To access the GPIO pins and to use functions associated with time you must use an import statement to bring in those modules.
  2. GPIO.setmode(GPIO.BOARD)… At least two numbering conventions are used on the GPIO pins: BOARD and BCM.  I chose BOARD because it uses the physical location of the pins on the RPi and was easier to understand.
  3. GPIO.setup(11,GPIO.OUT)…  This sets the number 11 pin as an output.  The output voltage is 3.3 volts.
  4. a = 0… This is the counter that I increment by 1 to control the number of times my loop (see step 5 below) is iterated.
  5. while (a < 15):…  This is the logical statement used to form an iterative loop.
  6. GPIO.output(11,True)
    time.sleep(0.125)
    GPIO.output(11,False)… Here pin 11 is powered up then an eighth of a second is slept through then the pin is powered down.
  7. a = a + 1… At the bottom of the loop I add 1 to the current value of the variable “a” so I can count how many times the loop has executed.

 

#example showing 5 LEDs blinking in a loop
import RPi.GPIO as GPIO
import time

#use the location numbering convention
GPIO.setmode(GPIO.BOARD)

#set the pins as outputs
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
a = 0
while (a < 15):

GPIO.output(11,True)
time.sleep(0.125)
GPIO.output(11,False)

GPIO.output(13,True)
time.sleep(0.125)
GPIO.output(13,False)

GPIO.output(16,True)
time.sleep(0.125)
GPIO.output(16,False)

GPIO.output(18,True)
time.sleep(0.125)
GPIO.output(18,False)

GPIO.output(22,True)
time.sleep(0.125)
GPIO.output(22,False)

GPIO.output(18,True)
time.sleep(0.125)
GPIO.output(18,False)

GPIO.output(16,True)
time.sleep(0.125)
GPIO.output(16,False)

GPIO.output(13,True)
time.sleep(0.125)
GPIO.output(13,False)

a = a + 1

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *