Lesson 7 – Light Sensors

Python Concepts

Generating random numbers

Teacher Materials

Get Access

In Lesson 2, you looked at the values of the Finch light sensors. These two sensors detect the amount of light around the Finch. In this lesson, you will write programs that use the light sensors to control the Finch.

Picture showing the placement on the light sensors on top of the Finch. They are on the top of the Finch toward the beak. They are underneath the plastic shell of the Finch.

Using the Light Sensors

The getLight() method returns the value measured by a light sensor from 0 to 100 (no units). This method requires one parameter, a string that indicates whether you are interested in the right or left sensor.

bird.getLight('L') 
The getLight() function takes one parameter that must be either 'L' or 'R' to indicate the left or right light sensor.

Since getLight() returns a value between 0 and 100, you can call this method inside a Finch method that requires a parameter from 0-100. For example, you can call getLight() inside the setBeak() method. This program will continually set the brightness of the beak based on the right light sensor until you press button A.

while not bird.getButton('A'): 
bird.setBeak(0,bird.getLight('R'),0) # Control beak with right light sensor 
bird.stopAll()

Exercise 1

Try out the code above, and then modify it so that the Finch tail lights are also controlled by the left light sensor. 

Exercise 2

In Exercise 1, the beak gets brighter when there is more light around the Finch. Modify your code from Exercise 1 so that the beak and tail lights are brighter when it is dark around the Finch.

Exercise 3

Write a program that uses the left light sensor to control the speed of the right Finch motor. The right light sensor should control the speed of the left Finch motor. This program should make your Finch follow a flashlight! The Finch should follow the flashlight until you press either button A or button B.

You can also use the light sensor to control the behavior of the Finch with an if-else statement or a while loop. For example, the code below blinks the Finch tail until the Finch detects that it is dark. 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. If the amount of light in your room changes, you may need to adjust the threshold, and this is easier to do if you use a variable.

threshold = 10 
while (bird.getLight('L') > threshold): # As long as left sensor sees bright 
bird.setTail("all",100,0,0) 
sleep(0.1) 
bird.setTail("all",0,0,0) 
sleep(0.1)

Exercise 4

Write a program that moves the Finch forward until it detects that it is dark. The Finch should stop if either light sensor detects that it is dark. You can test your program by having your Finch drive into a box.

Exercise 5

Write a program that makes your Finch afraid of the dark. If both the Finch light sensors detect that it is dark, the Finch should turn red and run around in fear. Otherwise, the Finch should turn a nice, relaxed shade of green.

micro:bit V2 Extra Challenge

If your Finch contains a micro:bit V2, you can also use a temperature sensor in the micro:bit to measure the temperature around the Finch 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 Finch, you will see an error when you use this method.

Write a program to alert you when the Finch is too hot. As long as the Finch is nice and cool, its lights should blink blue. When it gets too hot, the lights should turn red.

Generating Random Numbers

To make the behavior of your robot less predictable, you can incorporate some variation so that your robot does something slightly different each time you run the program. To do this, you can use a random number generator. A random number generator is a function that picks a random number each time you call it. In Python, use randint() to pick an integer. This function takes two parameters that define the range of numbers that you are interested in. The first is the lowest number that should be generated, and the second parameter is the highest number that should be generated. For example, this line of code will generate a random number from 0 to 5.

random.randint(0,5) 
The randint() function takes two parameters. The first is the lower bound for the random number, and the second is the upper bound.

To use the randint() function, you must import it from the random library by placing this line of code at the beginning of your program.

import random # import random number generator

Exercise 6

What do you think this code will do? Make a hypothesis, and then try it out. Remember to import the random library before using the randint() function.

for i in range(5): 
bird.setMotors(random.randint(-100,100), random.randint(-100,100)) 
sleep(2) 
bird.stop()

Exercise 7

Write a program that turns the tail of the Finch a random color every second as long as the Finch detects that it is dark. For an added challenge, turn each of the tail lights a different color.

Exercise 8

The random library also includes other random number generators. For example, random.random() generates a random number between 0.0 and 1.0 (not including 1.0). Use this function to modify your code from Exercise 7 so that the tail stays each color for a random number of seconds between 0 and 1. Can you then make the LED stay each color for a random number of seconds between 0 and 5?

Note: random.random() does not take any parameters.

Exercise 9

Make the Finch into a spinner for a game like Twister or Chutes and Ladders. Write a program that spins the Finch for a random number of seconds between 0 and 2. The Finch’s beak should be red while it is spinning and then green for one second after it stops moving. The program should repeat this cycle until the Finch detects that it is dark.

Back to Top