Lesson 7 – Light Sensor

Hummingbird Components

1 LED, 1 Light Sensor, 1 Rotation Servo

Python Concepts

Boolean expressions, if-else statements

Teacher Materials

Get Access

In the previous lessons, you learned to use the Hummingbird outputs. In the next few lessons, you will learn to use the Hummingbird inputs, which are called sensors. Sensors enable a robot to collect information about the world around it. The Hummingbird sensors measure light, distance, sound, and the position of a dial.

This lessons will work with the light sensor. Connect the light sensor to SENSORS port 1 on the Hummingbird Bit. Connect a single color LED to port 1.

Use the getLight() method to find the value of the light sensor; it requires one parameter, the port to which the sensor is connected (1-3). This Hummingbird method returns a value, which means that when you call it, it gives your program a value. The getLight() method returns an integer that represents (in arbitrary units) the amount of light measured by the sensor from 0 (dark) and 100 (maximum light).

Exercise 1

You can use the print() function in Python to print a number or a string to the screen. The number will appear in the console; in IDLE, this is the window called the Python Shell. Try out this code to print the value returned by getLight(). What is the light sensor’s value in your room? What is the value of the sensor when you cover it with your hand?

Sensors can enable your robot to make a decision. For example, suppose you want to turn on a light if the room is dark. You can use an if-else statement to do this. For example, this code will turn on an LED if the value of the light sensor is less than 10. The keyword if is followed by a comparison, bird.getLight(1) < 10. This comparison is a Boolean expression, which means that it is either true or false. If the comparison is true, the program runs all indented lines inside the if part of the if-else statement. If the comparison is false, the program runs all indented lines inside the else part of the if-else statement. Then the program moves on to the next unindented line.

Exercise 2

Try out this code. If the value of the light sensor is less than 10, the light will turn on. If the value of the light sensor is NOT less than 10, the light will turn off. The program will pause for five seconds in both cases, because sleep(5) is not indented. After you have tried out the code, modify it so that a less than (<) sign is displayed on the micro:bit when it is dark and a greater than (>) sign is displayed on the micro:bit otherwise.

The if statement above compares the light sensor value to a specific value called a threshold; in this case, the threshold is 10. It is good programming practice to use a variable for the threshold. Remember, a variable is a name that represents a value. You have been using variables in for loops for several lessons. A for loop creates a variable, which we have been calling i. You can also create variable in Python by setting a name equal to a value. This line of code creates a variable named threshold with a value of 10. This is called declaring a variable. Note that the name of the variable has to be to the left of the equal sign (unlike a math equation).

Once you have declared a variable, you can use the name of that variable in your program. When the program runs, Python will use the value of the variable everywhere the name appears. In this code, the value of threshold is 10. In the if statement, the program will compare the value of the light sensor to 10.

Exercise 3

Try out this code. What will happen if you set threshold equal to -1? Make a hypothesis and then try it out. What will happen if you set threshold equal to 101? Make a hypothesis, and then try it out.

Exercise 4

Write a program that uses an if-else statement to control a rotation servo. If it is bright, turn the rotation servo on. If it is dark, turn the rotation servo off.

Exercise 5

Write a program that uses an if-else statement to control the Hummingbird buzzer. If it is dark, the buzzer should play a low-pitched sound. If it is bright, the buzzer should play a high-pitched sound.

Extra Challenge

Use a for loop to repeat your if-else statement from Exercise 5 25 times (make sure everything is indented inside the for loop). What happens as you cover and uncover the light sensor while the program is running?

micro:bit V2 Extra Challenge

If your Hummingbird contains a micro:bit V2, you can also use a temperature sensor in the micro:bit to measure the temperature around the Hummingbird in degrees Celsius. The getTemperature() method returns the value of this sensor. Room temperature is about 20°C. If you don’t have a micro:bit V2 in your Hummingbird, you will see an error when you use this method.

Write a program to alert you when the Hummingbird is too hot. If the Hummingbird is hot, a tricolor LED should be red.  Otherwise, it should be blue.