Lesson 2 – Exploring Sensors

Python Concepts

Data types, variables, arithmetic, printing to the screen

Teacher Materials

Get Access

In the last lesson, you learned to make the Finch move and turn. This uses the motors in the wheels, which are Finch outputs. Outputs are ways that the Finch can act on the world or communicate with the user; they include the motors, lights, and buzzer. The Finch also has inputs called sensors. The sensors collect data about the environment around the Finch and provide this data to your program. In this lesson, you will explore some of the kinds of data that you can collect with the Finch sensors.

A picture of the Finch with the seven sensors highlighted. The wheel encoders are within the wheels, and the line tracking sensors are on the bottom of the Finch. The distance sensor is underneath the beak, and the light sensors are on the top of the Finch toward the beak. The compass, accelerometer, and buttons are all in the micro:bit in the tail of the Finch.

Creating a New File

Open a new file. Remember, every program that uses the Finch must import the Finch library and declare a Finch object. This means that every program must use the template shown here. All the code that you write should go in the space marked by the “Write code here!” comment. For the remainder of these lessons, we will assume that your program has these elements, and we will not show them in the sample code.

from BirdBrain import Finch # Import Finch library 
bird = Finch() # Declare Finch object 
# Write code here!

Printing Sensor Values

The distance sensor measures the distance between the Finch and the nearest obstacle. You can read the value of the distance sensor with the getDistance() function. This method returns a numerical value that is the distance in centimeters. In programming, to return a value means to provide that value to the program when a function or method is called. 

To see the value of the distance sensor, you can use the print() function to print the value to the screen.

print(bird.getDistance())

Exercise 1

Try out this sample code. It prints the value of the distance sensor with a label. Run your program with the Finch at different distances from the wall or an obstacle (the distance sensor must be pointed at the obstacle to see it). What is the smallest distance you can measure? What is the largest?

print("Distance: ", bird.getDistance())

Exercise 2

Print the values of some of the other Finch sensors using the methods below. Each value should have a label. Use your program to explore these sensors. How can you change their values? Don’t worry if you don’t understand these methods completely. You will learn more about the sensors in future lessons; you can also consult the Finch Python Library Description for a full list of sensor methods. 

  • getLight(‘R’) – returns the value of the right light sensor
  • getLight(‘L’) – returns the value of the left light sensor
  • getButton(‘A’) – returns the value of button A on the micro:bit
  • getOrientation() – returns the orientation of the Finch
  • getEncoder(‘R’) – returns the number of rotations that the right wheel has turned

Data Types

You probably noticed in Exercise 2 that these Finch methods gave you different kinds of data. In Python, every value has a different data type. There are four common data types in Python: 

  • int – Values of type int are integers, or whole numbers.
  • float – Values of type float are decimal numbers, which are often called floating point numbers in computer science.
  • bool – Type bool is short for Boolean. A Boolean value is either True or False. For example, getButton(‘A’) is True when button A is being pressed and False otherwise.
  • str  – Values of type str are strings. A string is a group of characters, like “hello”. In Python, strings are surrounded by single or double quotes.

You can use the type() function to find the data type of a value. For example, this line of code prints <class ‘int’> to the screen. That tells us that the value returned by getDistance() is type int.

print(type(bird.getDistance()))

Exercise 3

Find the data types returned by all the methods in Exercise 2. 

Variables and Arithmetic

Often, you want to save a value to use it later. You can do this in Python by using a variable. A variable is a name that represents a value. To create a variable, choose a name and use the equals sign to assign a value to the variable. For example, this line of code creates a variable named currentDistance and assigns the value of the distance sensor to it. 

currentDistance = bird.getDistance()

To assign a value to a variable, the name of the variable must be on the left of the equals sign. Note that this is a little different from how the equals sign is used in math. In math, the equals sign indicates that two quantities are equal, and it doesn’t matter which one is on the left side of the equals sign. In Python, the equals sign is called the assignment operator. It means that the value on the right side of the equals sign is being assigned the name on the left side. The first time you assign a value to a variable is called initializing or declaring a variable.

After a variable has been initialized, you can use it later in the program. Every time the program sees the name of the variable, it will substitute the value of that variable. You can use arithmetic operators in Python to perform addition (+), subtraction (-), multiplication (*), and division (/) with numbers and variables.

Exercise 4

What will this code do if the robot is 20 cm from the wall? Make a prediction, and then try out this code to test your prediction.

currentDistance = bird.getDistance() 
print(currentDistance) 
print(2*currentDistance) 
print(4*currentDistance)

Exercise 5

Assign the values of the left and right light sensors to variables. Then use these variables to calculate the difference between the two sensors. Store that in a variable called difference and print the value of difference to the screen.

Exercise 6

Extend your program from Exercise 5 so that it also calculates the mean of the left and right light sensors. Store that in a variable called mean and print the value of mean to the screen.

Hint: You may need to use parentheses as well as arithmetic operators.