02 - Servo Motor and Data

micro:bit with servo motor

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!

circle-info

Requirements

🌡️ Thermometer

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.

💡Light level

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.

chevron-rightSet Up the Servo Motorhashtag

Add the servo motor extension following the steps from our previous section, Coding your first servo motor on MakeCode

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 Pin 0 (P0).

Nothing to test at this point.

chevron-rightForever Read and Display the Current Temperaturehashtag

As we want to continuously read and display the temperature, we will use the forever block. 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.

chevron-rightLight Levelhashtag

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:

circle-check
chevron-right🍎Teacher's Corner - Temperature rangehashtag

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:

  1. In Halifax.

  2. Where you live.

  3. In the Sahara

  4. North Pole

  5. At different times of the year:

    1. In the summer?

    2. In the winter?

    3. 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.

chevron-right🍎Teacher's Corner - map functionhashtag

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:

chevron-right🍎Teacher's Corner - Expaning on the learned conceptshashtag

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?

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.

Last updated

Was this helpful?