Blink a Series of LEDs Using Python Programming Language and Raspberry Pi

by mirza irwan osman in Circuits > Raspberry Pi

14423 Views, 15 Favorites, 0 Comments

Blink a Series of LEDs Using Python Programming Language and Raspberry Pi

LEDs blinking controlled by Raspberry Pi
Overview of Steps

In the first phase, I will install only one LED. Next, I download the LED class python module that represent one hardware LED. I will run the LED class python module to blink that LED. The first phase serves as a "Proof of Concept".

In the second phase, I will install another 4 LEDs. Finally, I run the python main module to create a series of five sequentially blinking LEDs as depicted in the video above

Materials

Gather the following materials:

1 Raspberry Pi device running on standard Raspbian Linux installation
5 LEDs rated at 2.4V

5 Resistor (can be between 185 Ohms to 270 Ohms)

1 breadboard

6 Female to Male Jumper Wire

5 single-core wire (also called solid wires, solid-core or single-strand wire, consists of one piece of metal wire. Solid wire is useful for wiring breadboards. I stripped an unused telephone wire to get the single-core wire. There are 4 thin insulated single-core wire in a telephone cable. Choose any one and strip the insulator off just enough to expose bare metal at the two ends.)

Installing the First LED

Prototype
gpio3.jpg
WARNING

Be careful when installing the LED with the Raspberry Pi as it may short-circuit and damage Raspberry Pi device. Do this at your own risk. Remove all power supply source from your Raspberry Pi device.

Materials

The following materials are required:

1 X Raspberry Pi
1 x LED

1 X 165 Ohm Resistor

2 X Male-Female Jumper Wire

1 X 5cm single core wire 1 X Breadboard

Installation
  1. Read to the GPIO diagram to locate the pins.
  2. Connect female end of jumper wire to the GPIO Pin 11. (This pin will be programmed to supply 3.3 Volts)
  3. Connect male end of jumber wire to the breadboard as shown in video.
  4. Connect resistor to breadboard as show in video.
  5. Connect LED to breadboard as shown in video.
  6. Connect the single-core wire as shown in video
  7. Connect female end of another jumper wire to GPIO Pin 6 (This pis is at 0 Volts.)

Start Up Raspberry Pi

  1. Boot up the Raspberry Pi by pluggin in the power supply micro usb
  2. Log in using pi account

Use a Python Class Module to Flash an LED

Test LED installation
  1. Load the LXTerminal.
  2. Install or update the Rpi.GPIO software by typing the following bold commands. The command might ask you for your input:

    pi@raspberrypi ~ $ sudo apt-get update

    pi@raspberrypi ~ $ sudo apt-get install python-rpi.gpio

  3. Download the attached file called led.py and saved it as led.py in Raspberry Pi directory: /home/pi/python_proj/gpio
  4. Load the IDLE by typing the following:

    pi@raspberrypi ~ $ sudo idle

  5. On the IDLE's Python Shell Window: Click File -> Open -> Choose led.py that you saved in #1 of this step
  6. Read the documentation and source codes of led.py.
  7. In IDLE's Python Shell Window, type the following

    >>>import sys

  8. Then press ENTER
  9. Type the following:

    >>>sys.path.append('/home/pi/python_proj/gpio')

  10. Then press ENTER
  11. Type the following:

    >>>import led

  12. Then press ENTER
  13. Type the following to setup the LED connected to Pin 11:

    >>>led11 = led.Led(11)

  14. Then press ENTER
  15. Type the following to blink the LED connected to Pin 11:

    >>>led11.blink_non_stop()

  16. Then press ENTER key
  17. Observe that the LED blinks continuously.
  18. Press Ctlr+C to exit program

  19. Type the following:

    >>>led11.clean_up()

  20. You have successfully completed the "Proof of Concept" phase if the LED blinks continuously.

Downloads

Install and Test the Next 4 LEDs

Five LEDs in a row
Materials

You need the following:

4 x LED

4 X 165 Ohm Resistor

4 X Male-Female Jumper Wire

4 X 5cm single core wire

Installation
  1. Shut down Rasbian OS
  2. Unplug the power cable off Raspberry Pi
  3. Follow the same instruction described in Step 2.
  4. Use the following GPIO Pin:
    Pin 12
    Pin 15
    Pin 16
    Pin 18

  5. Play the attached video for detail to see the layout of the components in the breadboard and Raspberry Pi
  6. Load the IDLE (Python Editor) by typing the following:

    pi@raspberrypi ~ $ sudo idle
  7. On the IDLE's Python Shell Window: Click File -> Open -> Choose led.py
  8. In IDLE's Python Shell Window, type the following:

    >>>import sys
  9. Then press ENTER
  10. Type the following:

    >>>sys.path.append('/home/pi/python_proj/gpio')
  11. Then press ENTER
  12. Type the following:

    >>>import led
  13. Then press ENTER
  14. Type the following:

    >>>led12 = led.Led(12)
  15. Then press ENTER
  16. Type the following:

    >>>led12.blink_non_stop()
  17. Then press ENTER key
  18. Observe the LED blinks continuously.
  19. Press Ctlr+C to exit program
  20. Type the following:

    >>>led12.clean_up()
  21. Repeat instructions No 11-15 for the rest of the LEDs. Remember to change the Pin number for each LED:

    led15 = led.Led(15)

    led16 = led.Led(16)

    led18 = led.Led(18)

Blink the Series of LEDs

Finale of LEDs
  1. Download led_effects.py and led_main.py and save the 2 files as its original name in /home/pi/python_proj/gpio
  2. In IDLE's Python Shell window, click Menu -> Open -> Choose led_effects.py
  3. Read the source codes
  4. In IDLE's Python Shell window, click Menu -> Open -> Choose led_main.py

  5. Read the source codes

  6. In the window that displays led_main.py, click on Run -> Run Module
  7. Observe all 5 LEDs blinking in sequential order