Flashing Single LED

Project 2b: Pulsating 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 project modifies what we accomplished in Project 2a by simply replacing the code that makes our LED flicker with a basic loop that slowly ramps up our PWM pin’s power. The LED is then shut off completely and the loop continues until Ctrl + C is pressed.

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 -*-
#
#  pulsating_LED.py
#
# Make an LED connected to GPIO 18 pulsate
#
#  Copyright 2015  Ken Powers
#

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

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

# 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 "Pulsating LED. Press CTRL + C to quit"

# Main loop
try:
    while RUNNING:
	# Start PWM with the LED off
        pwm.start(0)
        # Slowly ramp up the brightness of the LED
        for x in range (0,100):
		    pwm.ChangeDutyCycle(x)
		    # Pause to slow ramping
		    time.sleep(0.02)

# 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 -*-
#
#  pulsating_LED.py
#
# Make an LED connected to GPIO 18 slowly pulsate
#
#  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.

import 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

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 uses a loop to slowly ramp up the PWM pin’s power until it hits 100%. The loop continues until Ctrl + C is pressed.

# Main loop
try:
    while RUNNING:
	# Start PWM with the LED off
        pwm.start(0)
        # Slowly ramp up the brightness of the LED
        for x in range (0,100):
		    pwm.ChangeDutyCycle(x)
		    # Pause to slow ramping
		    time.sleep(0.02)

# 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()

Enjoy modifying the code above. Try adjusting the amount of delay to change the rate at which the pulsating occurs. Be creative and above all, have fun!



Leave a Reply