Lesson 1 – Single Color LEDs

Hummingbird Components

3 Single Color LEDs

Python Concepts

Importing libraries, declaring an object, using object methods

Teacher Materials

Get Access

The Hummingbird Robotics Kit enables you to create your own robots with lights, motors, and sensors connected to the Hummingbird board. In these lessons, you will learn how to write programs in Python to control the different Hummingbird components. Before you start, make sure that you have installed Python and set up the Hummingbird. You can also explore the different parts of the Hummingbird kit here.

In this lesson, you will write programs to turn single color LEDs on and off. A single color LED is a small light with two wires. The colored wire shows the color of the LED. The Hummingbird kit comes with red, green, and yellow LEDs.

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

Use the terminal tool to plug three single color LEDs into LEDS port 1, 2, and 3 on the Bit. The colored wire should be connected to ‘+’ and the black to ‘-.’

Connect to the Hummingbird in the BlueBird Connector, and then open a new file in Python. Make sure that this file is inside the BirdBrainPython folder that contains BirdBrain.py. To use the Hummingbird in Python, you must import the Hummingbird class from the BirdBrain library. A library in Python is a collection of Python code that you can use in your program, and the Hummingbird class contains the methods that you will use to write programs to use the Hummingbird’s lights, motors, and sensors. In addition, you should import the sleep() function from the time library.

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

Next, declare an object that represents the Hummingbird. This line of code gives your Hummingbird a name, in this case “bird,” and tells the program that it is Hummingbird.

The ‘A’ in parentheses tells the program that your Hummingbird is device A in the BlueBird Connector.

You can use the # sign to add comments to your program. Comments do not affect how your program works, but they make it easier for other people to use your code (and for you to remember what it does!).

Once you have declared a Hummingbird object, you can use the lights, motors, and sensors by calling methods on that object. Methods are all the different functions that let you do things with your Hummingbird. For example, you can use the setLED() method to turn a single color LED on or off. This method 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 3. The second parameter is the intensity, or brightness, of the LED from 0 to 100. 0 means that the LED is off, and 100 means that the LED is at maximum brightness.

The simplest way to use an LED is to turn it on, pause the program, and then turn it off. An example program is shown below. The sleep() function pauses the program; it takes a single parameter that is a number of seconds. The stopAll() method turns off all the lights and motors of the Hummingbird. You should get in the habit of calling this method at the end of every program.

Exercise 1

Create a new file named “SingleColorLED.py” and try out the sample code shown above (remember to save the file within the BitPython folder). It sets LED 1 to maximum brightness. Try out other intensity values between 0 and 100 to explore the different levels of brightness that are possible.

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

 

Exercise 2

Modify your code to make the LED stay on for five seconds.

You have attached three LEDs to the Hummingbird board. To use LED 2, call the setLED() method with the first parameter set to 2. The code below turns on LED 1 for one second, then turns it off and turns on LED 2 for one second.

Exercise 3

Try out the program above. Then modify it to include all three LEDs.

Exercise 4

Write a program that turns on all three LEDs at intensity 100 for two seconds, then intensity 75 for two seconds, then intensity 50 for two seconds, and then intensity 25 for two seconds.