Flashing LEDs

Project 3: Flashing LEDs

What you will need:

  • Raspberry Pi
  • Breadboard
  • 1 Green LED
  • 1 Red LED
  • 1 Blue LED
  • 1 Yellow LED
  • 4 220Ω to 330Ω Resistor —[III I]—
  • 5 Female to Male Jumper Wires
  • 4 Male to Male Jumper Wires

Instructions:

For this project, we are simply building upon what we established in Project 2. It is important to understand what is happening with the Python script in that project as well as making sure the dependencies and libraries mentioned in its instructions are properly installed.

To make things a bit more interesting, we are adding 3 LEDs to our previous breadboard layout. It isn’t really necessary to use different colored LEDs but it definitely makes it a bit more colorful. My son and I chose green, red, blue, and yellow. Using the different colors also let us clarify our Python script a little bit by using descriptive  variable names that reference our individual LEDs.

An important thing to note is that in this project we ran a green jumper wire from Pin 3 (Ground) of the Raspberry Pi to one of the long rails on our breadboard. This allowed us to run short jumpers from the negative side of each LED to this common rail and kept our layout clean. Instead of using multiple ground connections on the Raspberry Pi, we were able to use one. This is important to mention because future projects on this website are going to utilize a positive 3.3 volt rail and a separate ground rail.

Next, we added the new LEDs, resistors, and jumper wires to our previous breadboard layout. The negative side of each LED connects to a resistor and the resistor’s other lead is connected to the common ground rail we created. The positive lead of each LED is connected to an individual pin on the Raspberry Pi so we can control them independently using the Python script below. We chose GPIO 18 for the green LED, GPIO 23 for the red LED, GPIO 24 for the blue LED, and GPIO 25 for the yellow LED.

As in Project 2, we used the Geany IDE application from the Raspbian desktop to create a new Python script. Perform the same steps you used from that project to create a new script, and enter the code below:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  flashing_LED.py
#
# A short program to randomly turn on one of 4 LEDs
# connected to GPIO 18, 23, 24, and 25
# for a random amount of time and then turn it off
#
#  Copyright 2015  Ken Powers
# 

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

# Define Constants
RUNNING = True
green_led = 18
red_led = 23
blue_led = 24
yellow_led = 25
led_list = [green_led, red_led, blue_led, yellow_led]

# Configure the GPIO to BCM numbering scheme and set pins to output mode
GPIO.setmode(GPIO.BCM)
GPIO.setup(green_led, GPIO.OUT)
GPIO.setup(red_led, GPIO.OUT)
GPIO.setup(blue_led, GPIO.OUT)
GPIO.setup(yellow_led, GPIO.OUT)

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

# Main loop
try:
    while RUNNING:
		on_time = random.uniform(0, .25)
		led = random.choice(led_list)
		GPIO.output(led, GPIO.HIGH)
		time.sleep(on_time)
		GPIO.output(led, 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:

Our Python script begins in the same fashion as what we did for Project 2. We indicate that we want the interpreter to use Python and give a brief description of the script in a comment section.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  flashing_LED.py
#
# A short program to randomly turn on one of 4 LEDs
# connected to GPIO 18, 23, 24, and 25
# for a random amount of time and then turn it off
#
#  Copyright 2015  Ken Powers
#

Next, we import the libraries we plan to use. Notice that in addition to the RPi.GPIO and time libraries, we are also importing the random library.

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

We define some global constants for this project and give them easily identifiable names. In this particular case, we are assigning the GPIO pin numbers to constants simply for the sake of code clarification. Then, we create a list of these GPIO pin numbers in a simple one-dimensional array.

# Define Constants
RUNNING = True
green_led = 18
red_led = 23
blue_led = 24
yellow_led = 25
led_list = [green_led, red_led, blue_led, yellow_led]

Although we could have looped through our list in order to perform the next step, we chose to simply make it known that we are using the Broadcom numbering system for the GPIO pins and then we individually set each pin to output mode.

# Configure the GPIO to BCM numbering scheme and set pins to output mode
GPIO.setmode(GPIO.BCM)
GPIO.setup(green_led, GPIO.OUT)
GPIO.setup(red_led, GPIO.OUT)
GPIO.setup(blue_led, GPIO.OUT)
GPIO.setup(yellow_led, GPIO.OUT)

For our main loop, we choose a random number between 0 and .25 for the amount of time our LED stays lit and pick a random LED from our list. The random LED is then turned on, a delay occurs, and then it is turned off. The loop continues until a Ctrl + C Keyboard Interrupt is detected. Notice we are using the same try, except, finally code as Project 2 to cleanly exit the program and reset the GPIO pins.

# Main loop
try:
    while RUNNING:
		on_time = random.uniform(0, .25)
		led = random.choice(led_list)
		GPIO.output(led, GPIO.HIGH)
		time.sleep(on_time)
		GPIO.output(led, 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()

Please note that the above Python script is not intended to necessarily be the most efficient way to accomplish our goals. Instead it is meant to be a very descriptive way to gently introduce new Python code and concepts within the context of a fun physical computing project.

Try modifying the above code to personalize it or change the way the LEDs are chosen or lit. Above all, have fun!



Leave a Reply