In the previous activity, we saw how can we manually collect and display data using the micro:bit. In this section, we will introduce two new different concepts:
Collect data continuously and automatically.
Display the data results using the Servo Motor.
The code for the activity is quite simple but useful and informative. We will create a thermometer and using the same code, a light meter!
Requirements
Use the micro:bit to continuously read the current temperature and point the servo motor arm accordingly. The micro:bit temperature sensor can read temperatures from -40 C° to 105 C°. Represent that temperature range with the servo motor.
Data Collection
The data will be collected as soon as and for as long as the micro:bit is powered.
Data Visualization
The servo motor will move its arm to a point representing the current temperature.
Display it!
Cut and draw different temperatures on a piece of cardboard for the servo motor to point to.
Follow the same instructions as above, but instead of reading temperature, create a light-reading device
Read the requirements and try to code the activity. Once you are done or you give it your best, take a look at the way we solved it, below.
Once you have the Servos set of blocks on MakeCode, set up the servo to have a range from 0° to 180° and listen to instructions provided by the micro:bit from Pin0 (P0).
Nothing to test at this point.
Forever Read and Display the Current Temperature
As we want to continuously read and display the temperature, we will use the foreverblock. In it, we need to add a block that moves the servo as the temperature changes.
As you test with the emulator, you'll see that the servo arm moves very little, as you change the temperature.
We want to use the full range of motion that the servo has (180°) to express the current temperature. To do so we need to find a way to convert our desired temperature range (-20 C to 40 C° *) to be equivalent to the servo motor arm range (0° to 180°). We can do this by replacing the temperature block above with the code below.
* See the Teacher's Corners below to explore more about temperature ranges and mapping
Now the changes in temperature are easier to see as the servo motor uses the whole 180° of motion.
Light Level
The instructions for the light level sensor are the same as per the thermometer, with some exceptions:
We use the light level sensor on the micro:bit instead of the temperature sensor.
The range of light sensitivity that the micro:bit reads ranges from 0 to 255.
The code for the full light application is as follows:
In the next section, we are utilizing the sensors on the micro: bit to collect data and display it using a Neopixel grid. See you at Neopixel and Data.
Thermometer
Light level
You can see our solved version
Teacher's Corner - Temperature range
The temperature range you choose depends on when and where you plan on using the thermometer.
Consider the following scenarios and think about what temperature range would you need:
In Halifax.
Where you live.
In the Sahara
North Pole
At different times of the year:
In the summer?
In the winter?
Year-round?
As you can see there is a lot to consider, but each decision will help you create the right thermometer for each situation.
Teacher's Corner - mapfunction
The map function in MakeCode takes two ranges of numbers, in this case, the temperature range we want to display (-20°C to 40°C) and adjusts it to the range of the servo-motor (0° to 180°).
Since we have a range of 60°C for temperature and 180° degrees for the servo-motor what the map function is doing is using a proportion. For example, if you want to find the angle for 20°C you would do the following calculation:
Temperature = 20°C
servo motor angle = x
degrees above -20°C = 20°C - (-20°C) = 40°C
Proportion:
x/180° = 40°C/60°C
x/180° = 2/3
x = 180°(2)/3
x = 120°
Teacher's Corner - Expaning on the learned concepts
To help reinforce the concepts learned, invite the students to share with the class their thought processes after completing the activity. The class can explore concepts such as:
What did you visualize?
Why did you choose to visualize that data?
What were your Data/Inputs?
Did you have to manipulate the data to match the constraints of the servo motor? For example, did you have to translate, calculate or change the input data to match the range of motion of the servo motor
What reaction or interpretation do you expect from your work?