Lesson 2 – Tri-Color LEDs

Hummingbird Components

1 Tri-Color LED

Python Concepts

Variables, for loops

Teacher Materials

Get Access

In this lesson, you will learn to use the tri-color LEDs. A tri-color LED is a small light with four wires. The tri-color LED actually has three tiny lights inside it. One is red, one is green, and one is blue. You can combine different amounts of red, green, and blue light to make different colors.

If you want to complete this lesson with a robot, try building an animal or a jitterbug.

Use the terminal tool to plug a tri-color LED into TRI-COLOR port 1 on the Bit. The red wire connects to ‘R,” the green to ‘G,’, the blue to ‘B,’ and the black to ‘-.’

Open a new file. Remember, every program that uses the Hummingbird must import the Hummingbird object and the sleep() function. You must also declare the Hummingbird object and call stopAll() at the end of the program. This means that every program must use the template shown here. All the code that you write should go in the space marked by a “Write code here!” comment. For the remainder of these lessons, we will assume that your program has these elements, and we will not show them in the sample code.

Note: Remember, if you are using Brython, do not include the “from time import sleep” line. The sleep() function is included automatically in Brython.

Use the setTriLED() method to control the tri-color LED. This method requires four parameters. The first parameter is the number of the Hummingbird port to which the LED is attached. This will be 1 or 2. The red parameter controls the amount of red light from 0 (none) to 100 (maximum brightness). The green and blue parameters control the amount of green and blue light, respectively, from 0 to 100. By combining different amounts of these three colors, you can also create other colors. For example, red light and blue light will combine to make purple.

Exercise 1

Try out this sample code. The tri-color LED should turn red, then green, then blue.

Exercise 2

Write a program to make the beak turn purple, then aqua, then yellow.

To make the tri-color LED change color multiple times, you can use a for loop. A loop is a structure that repeats actions within a program. A for loop repeats whatever is inside it a certain number of times. For example, this code turns the tri-color LED on and off 5 times.

In Python, range(5) produces a list of numbers from 0 to 4: [0,1,2,3,4]. The for loop creates a variable, which is a name that represents a value. Here, the variable is named i. The for loop sets this variable equal to 0 and then executes the four lines of code within the loop. These four lines of code make the LED blink green once. Then the program goes back to the top of the for loop, sets i equal to the next number in the list (1), and executes the four lines of code within the loop, blinking the LED again. This process continues until i is equal to 4, the last number in the list. When i is 4, the program executes the lines of code inside the loop and then moves on to whatever code follows the for loop.

Notice that the four lines of code inside the for loop are indented four spaces (one tab). This is how Python knows what is inside the for loop. All indented lines are within the for loop, and the next unindented line is outside the for loop. For example, this code turns the LED red after the for loop ends.

Exercise 3

Try out this code, and then modify the code to make the LED blink green 10 times and then turn red.

Exercise 4

Write a program to make the tri-color LED blink red 6 times, then blue 3 times, and then green 5 times.

Exercise 5

Write a program to make the tri-color LED blink from purple to green slowly 5 times and then blink from aqua to red quickly 10 times.

Extra Challenge

What do you think this code will do? Make a hypothesis, and then test it out. How can you modify this program to do something different?

Back to Top