Switched LED

Project 7a: Switched LED

What you will need:

  • Raspberry Pi
  • Breadboard
  • 1 Push-button Switch
  • 1 Red LED (or whatever color you have handy)
  • 1 1kΩ Resistors —[III I]—
  • 1 220Ω Resistor —[III I]—
  • 1 .1μF Ceramic Disk Capacitors
  • 6 Female to Male Jumper Wires
  • 4 Male to Male Jumper Wires

Instructions:

In Project 7, we discussed multiple ways to implement a push-button switch with Python and the Raspberry Pi microcomputer. The obvious next step is to accomplish something with that information. What better way to connect what we have learned so far than by using the switch to turn on an LED?

My son and I basically combined Project 2 and Project 7 in order to arrive at this particular experiment. As before, once you connect all your components by following the breadboard layout above, use your favorite editor to enter the Python script below:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  switched_LED.py
#
# A short program to test implementing a switch
# connected to GPIO pin 18 which will turn on an LED
# connected to GPIO 23
#
#  Copyright 2015  Ken Powers
# 

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

# Set constants
RUNNING = True
led_button = 18
led = 23

# Select GPIO numbering method and set switch pin
# as an input with internal pull-down resistance
# and LED pin as an output
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(led, GPIO.OUT)
GPIO.output(led, GPIO.LOW)

# Illuminate an LED connected to GPIO 23
# if the push-button is pressed.

print("Press button to illuminate LED")

try:
	while RUNNING:
		while True:
			input_value = GPIO.input(led_button)
			if input_value == True:
				GPIO.output(led, GPIO.HIGH)
			while input_value == True:
				input_value = GPIO.input(led_button)
				time.sleep(0.01)
			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 (be it an error or an interrupt)
finally:
    # Stop and cleanup to finish cleanly so the pins
    # are available to be used again
    GPIO.cleanup()

Code Analysis:

Once again, we begin our Python script by letting the interpreter know which language we are using. Next, we make a few comments that let readers know some of the details of our program. Specifically, we want programmers to know that we are connecting a switch to the Raspberry Pi’s GPIO 18 and an LED to GPIO 23. By the way, although we suggest a red LED in our project requirements, you can utilize any color you have handy.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  switched_LED.py
#
# A short program to test implementing a switch
# connected to GPIO pin 18 which will turn on an LED
# connected to GPIO 23
#
#  Copyright 2015  Ken Powers
#

Our next section of code simply imports some necessary modules we will be using and defines some necessary constants.

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

# Set constants
RUNNING = True
led_button = 18
led = 23

We then define our GPIO numbering method, set the appropriate GPIO pins as inputs and outputs, and toggle the internal pull-down resistance for our push-button input. It is very important to make sure the push-button switch pin is set as an INPUT and NOT an OUTPUT as this could potentially damage the Raspberry Pi.

# Select GPIO numbering method and set switch pin
# as an input with internal pull-down resistance
# and LED pin as an output
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(led, GPIO.OUT)
GPIO.output(led, GPIO.LOW)

Our main Python script loop simply checks the status of the push-button pin. If it is TRUE (or depressed) the LED pin’s status is changed to HIGH (or on). Once the switch is released, the LED is turned off and the script continues until CTRL + C is pressed.

# Illuminate an LED connected to GPIO 23
# if the push-button is pressed.

print("Press button to illuminate LED")

try:
	while RUNNING:
		while True:
			input_value = GPIO.input(led_button)
			if input_value == True:
				GPIO.output(led, GPIO.HIGH)
			while input_value == True:
				input_value = GPIO.input(led_button)
				time.sleep(0.01)
			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 (be it an error or an interrupt)
finally:
    # Stop and cleanup to finish cleanly so the pins
    # are available to be used again
    GPIO.cleanup()

As you can see, it is very simple to combine the elements from our previous Projects to accomplish the task of turning an LED on and off. An additional fun experiment might be to attempt to modify the Python script to allow the LED to turn on with a single press of the switch and off with an additional press much like a standard light switch. Above all, however, have fun with your projects and what you have learned!



Leave a Reply