Sequential LEDs

Project 4: Sequential LEDs

What you will need:

  • Raspberry Pi
  • Breadboard
  • 8 Red LEDs
  • 8 220Ω to 330Ω Resistor —[III I]—
  • 9 Female to Male Jumper Wires
  • 8 Male to Male Jumper Wires

Instructions:

This project builds upon what we accomplished in Project 3. The only real difference is we have added 4 additional LEDs to our breadboard layout. My son and I chose to use red LEDs but obviously you could use any color or even an assortment of colored LEDs if you desire.

Since the procedure for populating the breadboard for this project is not much different than Project 3, we won’t spend time re-describing the steps. Our LEDs for this project are connected to the following GPIO pins: 18, 23, 24, 25, 12, 16, 17, and 27. If you have trouble locating the GPIO pins on the Raspberry Pi’s 40 pin GPIO port, you can always reference this diagram on our site: Raspberry Pi GPIO

As before, my son and I used the Geany IDE from within the Raspbian desktop in order to create a Python script. Although there are many tools and editors available, we really like the way Geany works. Project 2 on this website describes the installation procedure for Geany if you choose to use it. Using an editor such as Geany can really help with making sure all your code is properly indented so the interpreter can run your script correctly.

Once you have entered the script below, execute it and watch the LEDs sequentially turn on and turn off.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  sequential_LED.py
#
# A short program to sequentially turn on 8 LEDs
# connected to GPIO 18, 23, 24, 25, 12, 16, 17, or 27
# for a quarter of a second and then turn them off
# and reverse the procedure
#
#  Copyright 2015  Ken Powers
# 

# Import the modules used in the script
import time
import RPi.GPIO as GPIO

# Define Constants
RUNNING = True
led_list = [18, 23, 24, 25, 12, 16, 17, 27]

# Configure the GPIO to BCM numbering scheme and set pins to output mode
GPIO.setmode(GPIO.BCM)
for x in range(0, 8):
	GPIO.setup(led_list[x], GPIO.OUT)
	GPIO.output(led_list[x], GPIO.LOW)

print("Let the flashing begin!")
print("Press CTRL + C to quit.")

# Main loop
try:
    while RUNNING:
		for x in range(0, 8):
			GPIO.output(led_list[x], GPIO.HIGH)
			time.sleep(0.25)
			GPIO.output(led_list[x], GPIO.LOW)
		for x in range(7, 0, -1):
			GPIO.output(led_list[x], GPIO.HIGH)
			time.sleep(0.25)
			GPIO.output(led_list[x], GPIO.LOW)

# If CTRL+C is pressed the main loop is broken
except KeyboardInterrupt:
    RUNNING = False
    print "\Quitting"

# Actions under 'finally' will always be called
# regardless of what stopped the program
finally:
    # Stop and finish cleanly so the pins
    # are available to be used again
    GPIO.cleanup()

Code Analysis:

As always, we began our Python script by letting the interpreter know we are using Python and by adding some comments to clarify our script’s purpose, etc…

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  sequential_LED.py
#
# A short program to sequentially turn on 8 LEDs
# connected to GPIO 18, 23, 24, 25, 12, 16, 17, or 27
# for a quarter of a second and then turn them off
# and reverse the procedure
#
#  Copyright 2015  Ken Powers

As in all our previous projects, we imported the Python libraries that are required for the program to function correctly.

# Import the modules used in the script
import time
import RPi.GPIO as GPIO

Next, we defined some constants for our script. In this case, we define a variable used to determine if our main loop should run or not and we create a simple list of the GPIO pins we are using for our LEDs.

# Define Constants
RUNNING = True
led_list = [18, 23, 24, 25, 12, 16, 17, 27]

Once again, we configured the GPIO to use the BCM numbering scheme. In our previous projects, we individually set each GPIO pin for our LEDs to output mode. This time, however, we created a list of LEDs in a one-dimensional array called led_list. Next, we simply use a for-loop to set each pin to an output mode and set its state to GPIO.LOW (or off).

# Configure the GPIO to BCM numbering scheme and set pins to output mode
GPIO.setmode(GPIO.BCM)
for x in range(0, 8):
	GPIO.setup(led_list[x], GPIO.OUT)
	GPIO.output(led_list[x], GPIO.LOW)

For our script’s main loop, we nested a simple for-loop to turn each LED on for a quarter second and then off. Next, we created the same loop in reverse so that the LEDs light sequentially one direction and then flow the opposite direction. The process continues until Ctrl + C is pressed.

# Main loop
try:
    while RUNNING:
		for x in range(0, 8):
			GPIO.output(led_list[x], GPIO.HIGH)
			time.sleep(0.25)
			GPIO.output(led_list[x], GPIO.LOW)
		for x in range(7, 0, -1):
			GPIO.output(led_list[x], GPIO.HIGH)
			time.sleep(0.25)
			GPIO.output(led_list[x], GPIO.LOW)

# If CTRL+C is pressed the main loop is broken
except KeyboardInterrupt:
    RUNNING = False
    print "\Quitting"

# Actions under 'finally' will always be called
# regardless of what stopped the program
finally:
    # Stop and finish cleanly so the pins
    # are available to be used again
    GPIO.cleanup()

As you can see from the Python scripts we have used for our projects, there are many ways to interact with the Raspberry Pi’s GPIO in order to accomplish different goals. Try experimenting with the code above in order to make the LEDs react in different ways. Above all, have fun!



Leave a Reply