Lighting LEDs with GPIOs

Description

LEDs and GPIOs

  • Connect individual LEDs to several GPIOs in order to control them using a program.

  • Learn how to use a loop to repeat a section of code multiple times

Materials

    RaspberrySTEM CREATOR kit with Raspberry Pi

    LEDs RaspberrySTEM Cell

Prerequisites

None


DISCUSSION

What is a GPIO?

  • General Purpose Input/Output.

  • A method for a computer to get or set the voltage (to high (3.3V) or low (0V, i.e. Ground) on a pin. Great for turning LEDS on/off, enabling devices (like other ICs, motors, relays, etc), or reading the state of sensors, buttons, switches, etc.

  • GPIO pin numbers are labelled on the RaspberrySTEM Lid Connector.


ACTIVITY

Hook up and light an LED

  • Choose any GPIO pin on the RaspberrySTEM Lid Connector. The GPIO Pin numbers are written as the first number next to each pin (not all pins are GPIO pins).

  • Connect up the circuit as shown. GPIO 14 is used in this example, but any other GPIO could be used as well.

                                

  • Run the following program. The first two lines initialize the GPIO software. The setup() function makes the selected GPIO pin an output pin. The final line is the critical line - it turns the GPIO on (and the LED will go on as a result).

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)


pin = 14

GPIO.setup(pin, GPIO.OUT)

GPIO.output(pin, True)


  • In the output() function, modify the parameter True to False. Rerun the program - this will turn the GPIO off, which turns the LED off.


DISCUSSION

Functions

  • Functions are a named section of a program.

  • Functions a grouped together in collections called libraries.

  • Functions are passed a list of parameters that change how the function works.

  • The sleep function, that is part of the time library, pauses for the amount of time given. It is called with time.sleep(seconds).


ACTIVITY

  • Use the time.sleep() function to flash an LED. The sleep function pauses the program for the amount seconds you give it as a parameter.

  • To use the sleep function, you'll need to add the following line to the top of your program:

import time


  • As an example, to sleep for two seconds, run:

time.sleep(2)


  • Change the previous program: Turn the LED ON, sleep for one second, turn the LED OFF, sleep for one second

  • Repeat the above multiple times, and the LED with flash.

  • The sleep function accepts fractional values as well - to sleep for half a second, you can use:

time.sleep(0.5)


  • Decrease the amount of time to have the LED flash faster.


DISCUSSION

Simple loops:

  • Loops are used to run a section of code for a specific number of times.

  • All code that is part of the loop must be indented to the same level


ACTIVITY

Loops allow a program to run the same section of code multiple times. The simplest loop is a “for” loop that runs a specific number of times.

  • Open a new file.

  • To print the word 'hello' five times, you can use:

for i in range(5):

        print('hello')


  • The variable i in the above loop increases by one each time the loop is run, starting at 0. To see the value of i each loop, print it.

  • Finally, flash the LED 10 times by adding a for loop to the previous program.


ACTIVITY

Two LED Siren

  • Add another LED/resistor to a different GPIO, as was done in the first activity.

  • setup() your new LED, and use output() to turn it on, as was done in the first activity.

  • Write a program to flash each LED, in an alternating fashion - when one LED is on, turn the other LED off. Flash the LED 10 times.


ACTIVITY

Four LED sweep

  • Add two more LEDs as done above for a total of four. Place them in a line.

  • In a loop, light each LED one at a time.

  • Back and forth: instead of lighting the LEDs just in one direction, switch direction when you get to the other side.