Flashing Single LED

Project 2a: Flickering Single LED

What you will need:

  • Raspberry Pi
  • Breadboard
  • 1 Light Emitting Diode (LED)
  • 1 220Ω to 330Ω Resistor —[III I]—
  • 2 Female to Male Jumper Wires

Instructions:

This is a very simple variation of Project #2. The breadboard layout is identical and we are still using GPIO 18 for the LED. The interesting thing about this specific pin is that it is designed for Pulse Width Modulation (PWM). Instead of just creating an on/off state for what is connected to this pin, we can vary its power. Randomly varying this will create an interesting candle-like flickering effect.

Once again, create a python script using the Geany IDE or your favorite command line editor and enter the code below:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  Flickering_LED.py
#
# Make an LED connected to GPIO 18 flicker randomly
#
#  Copyright 2015  Ken Powers
#

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

# Assign the hardware PWM pin and name it
led = 18
RUNNING = True
strength = 9

# Configure the GPIO to BCM and set it to output mode
GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)

# Set PWM
pwm = GPIO.PWM(led, 100)

print "Flickering LED. Press CTRL + C to quit"

# Main loop
try:
    while RUNNING:
        # Start PWM with the LED off
        pwm.start(0)
        # Randomly change the brightness of the LED
        pwm.ChangeDutyCycle(random.randint(5, 100))
        # Randomly pause on a brightness to simulate flickering
        time.sleep(random.random() / strength)

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

# Actions under 'finally' will always be called
finally:
    # Stop and finish cleanly so the pins
    # are available to be used again
    pwm.stop()
    GPIO.cleanup()

Code Analysis:

The first lines of our Python script above simply let the system know which interpreter to use once execution begins. Every line that begins with a # symbol contains comments to help us know what is going on when we look at our code at a later date.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  Flickering_LED.py
#
# Make an LED connected to GPIO 18 flicker randomly
#
#  Copyright 2015  Ken Powers
#

Our script begins by using the import statement to include the time and GPIO libraries in our code so they can be referenced later. We will be using the sleep() function of the time library to create a simple delay between turning our LED on and turning it off. We will also use the random function to determine the brightness of our LED.

import random, time
import RPi.GPIO as GPIO

Next, we define a couple constants for later use.

# Assign the hardware PWM pin and name it
led = 18
RUNNING = True
strength = 9

We then set up our GPIO pin assigned to the variable “led” to use the Broadcom numbering system and set the pin to output mode. We also set our PWM pin to 100%.

# Configure the GPIO to the BCM numbering scheme and set it to output mode
GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)

# Set PWM
pwm = GPIO.PWM(led, 100)

Our main program loop begins by turning the PWM pin to 0% power, then sets a random brightness between 5% and 100%. This brightness is held for a random amount of time and the entire process is repeated. The loop continues until Ctrl + C is pressed.

# Main loop
try:
    while RUNNING:
        # Start PWM with the LED off
        pwm.start(0)
        # Randomly change the brightness of the LED
        pwm.ChangeDutyCycle(random.randint(5, 100))
        # Randomly pause on a brightness to simulate flickering
        time.sleep(random.random() / strength)

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

# Actions under 'finally' will always be called
finally:
    # Stop and finish cleanly so the pins
    # are available to be used again
    pwm.stop()
    GPIO.cleanup()

Try experimenting with the above code by changing the “strength” variable to simulate different amounts of flickering times. You could also modify this code by creating a simple loop that slowly increases the PWM pin’s power to simulate pulsing. Above all, have fun!



Leave a Reply