Using Sensors with the Hummingbird (Python)
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.’
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.
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.
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°.
- The program ends when the light sensor detects that it is dark.