Creating Light with LEDs (Python)

The Hummingbird kit contains two type of LEDs, single color LEDs and tri-color LEDs. Single color LEDs have two wires, while tri-color LEDs have four wires. This lesson will show you how to control both type of LEDs in Python.

Getting Started

Before you begin this lesson, complete the following tasks:

  • Watch the Connecting Electronics video, which will show you how to attach components to the Hummingbird board.
  • Follow the instructions on the Python Programming page to install the Hummingbird API for Python.

A full description of the Hummingbird API can be found in the Hummingbird Python Package Description, but these lessons will show you how to begin to write Python programs to control the Hummingbird.

The Hummingbird is defined in Python as a class. To use the Hummingbird in a program, you must import this class and declare an object of type Hummingbird. We will call our instance of the Hummingbird class bird.

At the end of each program, remember to close the connection to the Hummingbird with the close() method.

In addition, you should import the sleep() function from the time module. You will use the sleep() function to pause the program to give your robot time to light up and move.

Single Color LEDs

A single color LED can be connected to one of four ports labeled “LEDS” on the Hummingbird board. Since you have four ports, you can connect up to four single color LEDs. The black LED wire should be connected to the ‘-’ terminal, while the colored wire should be connected to the ‘+’ terminal.

 

The set_single_led() method is used to control a single color LED. This function requires two parameters. The first parameter is the number of the Hummingbird port to which the LED is attached. This will be a number between 1 and 4. The second parameter is the intensity of the LED. This is a number between 0 and 255. 0 means that the LED is off, and 255 means that the LED is at maximum brightness. What do you think the program below will do? After you make a hypothesis, try it and find out. 

Exercise: Modify this program so that the LED blinks on and off repeatedly. 

Building Tip: LEDs are quite small, but you can use a styrofoam ball to diffuse the light. This will make the light appear larger.

Programming Tip: When Python reaches a call to set_single_led(), it sets the LED and moves immediately to the next line of code. This means that if you do not have a call to sleep() between two commands for the same LED, you may not see the effects of the first command. For example, you may not see the LED turn on when you run the script below. Add a call to sleep() between these lines in order to see the LED turn on and then off.

Tri-Color LEDs

A tri-color LED can be attached to one of the two ports labeled “TRI-COLOR” on the Hummingbird board. The four wires of the tri-color LED are red, green, blue, and black. The black wire should be connected to the ‘-’ terminal. The red wire should be connected to the ‘R’ terminal, the green to the ‘G’ terminal, and the blue to the ‘B’ terminal.

The tri-color LED actually has three tiny light elements inside it. One is red, one is green, and one is blue. This is important for programming the tri-color LED. The set_tricolor_led() method is used to control a tri-color LED. This method requires four parameters. The first is the number of the port to which the tri-color LED is attached (1 or 2). The second, third, and fourth parameters are the values for red, green, and blue, respectively. Each value controls the intensity of that color from 0 (none) to 255 (maximum brightness). 

What color will the tri-color LED be after you run the line of code shown below? How will you turn the tri-color LED off?

Exercise: Make a tri-color LED blink on and off in your favorite color at least ten times in five seconds. Then modify your program so that the tri-color LED blinks in random colors at least ten times in  five seconds.