Site Search

Lessons

Before you can build an awesome robot with your Hummingbird, you need to know how to use all the parts.

This lesson will first show you how to use the Hummingbird outputs, the lights and motors. These components are called outputs because programs in Python send commands to them to make something happen. Then you will learn how to use the Hummingbird inputs, the sensors that provide information to the robot about its environment.

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.

Using the Outputs

First, you will learn to use the lights, which are also called LEDs. The Hummingbird kit contains two types of LEDs, single color LEDs and tri-color LEDs. Single color LEDs have two wires, while tri-color LEDs have four wires.

Module 1: 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.

Module 2: 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.

Module 3: Motors

The Hummingbird kit works with three different kinds of motors: vibration motors, servo motors, and gear motors.

Vibration Motors

A vibration motor can be attached to one of the two ports labeled “VIBRATION” on the Hummingbird board. It does not matter which wire is attached to ‘+’ and which is attached to ‘-.’

The set_vibration_motor() method is used to control the vibration motor. This block requires two parameters. The first is the port attached to the vibration motor (1 or 2), and the second is the intensity of the vibration from 0 to 255. 0 means no vibration, and 255 means maximum vibration. The code below turns a vibration motor in port 1 on for 2 seconds.

Exercise

What do you think the script below will do? After you make a hypothesis, try it and find out. Modify this program to enable the user to control a tri-color LED as well.

Building Tip

Small bells can be attached to the vibration motor. Activating the motor will cause the bell to ring. Also, googly eyes can be attached to a vibration motor so that they move when the motor vibrates.

Servo Motors

A servo motor is a motor that moves to a particular angle. The Hummingbird servo motor can rotate to any angle from 0° to 180°. When using the servo motor, you must also use the AC power adapter (or a battery pack). Otherwise, the Hummingbird board will not have enough power to run the motor.

The servo motor has a small plug. This plug should be placed on one of the four male servo connectors on the Hummingbird board. The black wire should be connected to the ‘-’ pin, the red wire to the ‘+,’ and the yellow wire to the ‘S.’

Use hot glue to attach a popsicle stick to the white plastic circle on the servo motor. This will help you to see the movement of the servo motor more easily.

The set_servo() method is used to move the servo motor. It requires two parameters. The first is the port attached to the servo (1-4), and the second is the angle (0° to 180°). The command below will move a servo motor attached to port 1 to 0°.

Try out the script shown below. What problem do you observe? How can you fix this problem?

Exercise

Write a program to make the servo motor “dance” to a variety of different positions.

Gear Motors

The gear motor can be attached to either of the ports labeled “MOTORS” on the Hummingbird board. It does not matter which wire is attached to ‘+’ and which is attached to ‘-.’ When using the gear motor, you must also use the AC power adapter (or a battery pack).

The set_motor() method is used to control the gear motor. This method requires two parameters. The first is the port attached to the motor, and the second is the speed of the motor. The speed can be any number from -1.0 to 1.0. The code below turns a motor on for three seconds.

Use a small screwdriver to attach a wheel to the motor so that you can see it move more easily. Try several speeds between 0 and 1.0. Describe how the speed affects the motor. Try several negative speeds. What does it mean for the speed to be negative?

Using the Inputs

The Hummingbird LEDs and motors are output devices. Programs in Python send commands to these devices to make something happen. The Hummingbird sensors, on the other hand, are input devices. They send information to the Python program. The program can use this information to make a decision within a control structure such as a while loop or an if-else statement.

All of the sensors are attached to the Hummingbird in the same way. A sensor can be attached to any one of the four ports labeled “SENSORS.” Each sensor has a red wire, a black wire, and a yellow wire. The red wire should be connected to ‘+,’ the black to ‘-,’ and the yellow to ‘S.’

Module 3: Distance Sensor

Attach a distance sensor to the Hummingbird. The get_distance() method can be used to read the value of this sensor. This method takes a single parameter, which is the number of the port attached to the sensor. This method returns the value of the distance sensor in centimeters. This value will be between about 8 cm and 100 cm. The sensor cannot detect objects very close to the sensor or very far from it. It works best in the range of 20 cm to 60 cm.

Exercise

As an example, consider the code shown below. As long as the value of the Hummingbird distance sensor is greater than 30, the light will be green. When the value of the distance sensor falls below 30, the light will turn red. The sleep() command is included so that you can see the light turn red before the program ends. Try out this script and make sure that it works as you expect. The value that a Boolean statement uses to make a decision is called the threshold. In this case, the threshold is 30 cm.

Exercise

Write a program that blinks a red light and moves a servo motor when an object is close to the distance sensor. When nothing is near the distance sensor, the light should be off and the servo should not move.

Module 5: Rotary Knob

The rotary knob is very similar to the distance sensor. You can use the get_knob_value() block to find the value of this sensor. This method returns a value between 0 and 255. This method takes one parameter, the number of the port attached to the rotary knob.

Exercise

You can use the value of a sensor anywhere that you would use a number. This means that you can use a sensor value to set the intensity of an LED. For instance, the code below uses the value of the knob sensor to set the intensity of an LED every 0.1s for approximately 1 minute. Try out this code. What is the value of the knob sensor when it is turned all the way to the left? All the way to the right?

Exercise

Write a script that sets the speed of the gear motor based on the value of the knob sensor. Make sure that the motor can spin both forward and backward.

Module 6: Light Sensor

The light sensor is very similar to the other sensors that you have learned about. You can use the get_light_sensor() method to find the value of this sensor. This method returns a value between 0 and 255. This method takes one parameter, the number of the port attached to the rotary knob.

Attach the light sensor to the Hummingbird board. Use a print() command to measure the amount of light in your classroom. Then measure the value of the light sensor when you cover it with your hand. The average of these two values is a good threshold for the light sensor.

Exercise

Write a script that vibrates a motor as long as the light sensor detects that it is dark.

Exercise

One control structure that uses a sensor can be nested inside another one. What do you think the script below will do? After you make a hypothesis, try it and find out.

Exercise

Write a program that meets the following specifications:

The value of the knob sensor determines the color of a tricolor LED.

The value of a distance sensor determines the position of a servo: when an object is close to the sensor, the servo should be at 60°. Otherwise, it should be at 120°.