5
PROM: Primary Robotics On Micro:bits Adrian Oldknow [email protected] March 2018 Section 3: Using sensors A sensor is an electronic device which detects the state of something. It might be just a simple physical change, like opening and closing the fridge door, or it could be finding out your temperature, like a thermometer. The buttons A and B on the micro:bit detect whether you have pushed them down. The micro:bit has other more complicated sensors which can measure things like the temperature or the amount of light falling on its front. As we have seen, it also has a clock which can be used to control when things happen. Think about some of the devices you have in your home. I get woken up by my bedside alarm clock, set to go off at 07.00. When I get up, I change the setting on the knob which controls the central heating. I have a warm, but not boiling, shower thanks to the control of the immersion heater in the water tank. I make a cup of tea by boiling the kettle which automatically switches off when it senses that the water has started to boil. I make some toast using a toaster which has an adjustable timer so that it pops up when the toast is brown but not burnt. Modern cars have a wide range of sensors to make things happen automatically. Can you think of some? The micro:bit’s sensors can be accessed through the `Input’ blocks. Let’s see how we can turn the micro:bit into a thermometer. The tiny program below just shows the temperature sensed by the micro:bit as a whole number of degrees Celsius each time the A button is pressed. The simulator shows an adjustable thermometer. Try dragging the top of the red `liquid’ in the simulated temperature sensor up and down. What is the highest value it can show? What is the lowest? Connect your micro:bit to the computer and download the program to it. Disconnect the USB cable and plug in the battery box. What value does it show at room temperature? Try putting it in the fridge for a bit and then press button A as you take it out. You could use it to check the freezer but you

STEM Learning - Resources, CPD, STEM …€¦ · Web viewPROM: Primary Robotics On Micro:bits Adrian Oldknow [email protected] March 2018 Section 3: Using sensors A sensor is an electronic

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: STEM Learning - Resources, CPD, STEM …€¦ · Web viewPROM: Primary Robotics On Micro:bits Adrian Oldknow adrian@ccite.org March 2018 Section 3: Using sensors A sensor is an electronic

PROM: Primary Robotics On Micro:bits Adrian Oldknow [email protected] March 2018

Section 3: Using sensors

A sensor is an electronic device which detects the state of something. It might be just a simple physical change, like opening and closing the fridge door, or it could be finding out your temperature, like a thermometer. The buttons A and B on the micro:bit detect whether you have pushed them down. The micro:bit has other more complicated sensors which can measure things like the temperature or the amount of light falling on its front. As we have seen, it also has a clock which can be used to control when things happen.

Think about some of the devices you have in your home. I get woken up by my bedside alarm clock, set to go off at 07.00. When I get up, I change the setting on the knob which controls the central heating. I have a warm, but not boiling, shower thanks to the control of the immersion heater in the water tank. I make a cup of tea by boiling the kettle which automatically switches off when it senses that the water has started to boil. I make some toast using a toaster which has an adjustable timer so that it pops up when the toast is brown but not burnt. Modern cars have a wide range of sensors to make things happen automatically. Can you think of some?

The micro:bit’s sensors can be accessed through the `Input’ blocks. Let’s see how we can turn the micro:bit into a thermometer. The tiny program below just shows the temperature sensed by the micro:bit as a whole number of degrees Celsius each time the A button is pressed. The simulator shows an adjustable thermometer. Try dragging the top of the red `liquid’ in the simulated temperature sensor up and down. What is the highest value it can show? What is the lowest? Connect your micro:bit to the computer and download the program to it. Disconnect the USB cable and plug in the battery box. What value does it show at room temperature? Try putting it in the fridge for a bit and then press button A as you take it out. You could use it to check the freezer but you ought to wear gloves to keep your fingers from freezing when you take it out. DON’T put it in boiling (or any other kind of) water!

The next experiment tests the temperature and makes something happen if it is bigger (or less) than a given value. Maybe we can show a warning signal if the temperature rises above 25°C and sound an alarm if it falls below 15°C. We first need to store the current temperature in a variable such as `temp’. Then we pick one of three actions to perform according to the size of `temp’.

Page 2: STEM Learning - Resources, CPD, STEM …€¦ · Web viewPROM: Primary Robotics On Micro:bits Adrian Oldknow adrian@ccite.org March 2018 Section 3: Using sensors A sensor is an electronic

The code opposite uses button A to start it running. The green `while true’ block is a loop that will run continuously. Use the `Variables’ tools to create the variable `temp’. Use the `Input’ tools to pick up the temperature. Use the `Logic’ tools to bring in an `if’ block. Use the blue symbol before `if’ to create your own 3 choice structure as shown in the little diagram: `if …. else if ….. else ….’. Use it again to hide it.

Below is the complete program. If you run it on the simulator, the noise will come from your computer’s speakers. If you want to make the micro:bit generate a sound when it is detached from the computer you will need to connect a buzzer to the 0 and GND pins using crocodile clips or wire. Press button `A’ to run the program, and drag the thermometer level up and down to check all three states. You have now created your first `stand-alone’ system using a micro:bit. This is same structure as the program which controls the central heating of a house. In that case when the temperature falls below a certain level the heating is turned on and stays on until the temperature is warm enough, when the heating is turned off again.

In order to be able to collect data about the temperature when the micro:bit is not visible, we can create a program which records the lowest value reached in a certain period. When button `A’ is pressed the temperature is read every second for 10 seconds. The variable `min’ keeps track of the lowest temperature recorded, and the variable `clock’ keeps the time in seconds.

Page 3: STEM Learning - Resources, CPD, STEM …€¦ · Web viewPROM: Primary Robotics On Micro:bits Adrian Oldknow adrian@ccite.org March 2018 Section 3: Using sensors A sensor is an electronic

When the program has finished, the letter `E’ for `End’ is displayed. Then you can press button `B’ to read out the lowest temperature recorded. We only used collected data for 10 seconds to check that the simulator was working as expected. Now you can change value in the `while’ statement do 300 say, which will collect data for 5 minutes. Now you can download it to the micro:bit, disconnect the USB cable and connect the battery box. Then you put the device in the fridge, close the door and leave it to run. When you take it, after at least 5 minutes have passed, you should see the letter `E’ on the display. Then whatever the temperature is around you, pressing button B will show the lowest temperature it reached during its stay in the fridge.

You have now seen and used one way to make the program loop until a certain condition is met – in this case until the clock reaches a given value. We could equally as well have used a different structure called a `counted loop’. The `Blocks’ tools include a `repeat …. times’ block as shown alongside. So now we can capture data from scientific experiments.

Now it’s your turn to think of things you can do with the light sensor. You can read its value from the `light level’ variable in the `Input’ tools. It produces a number between 0 and 255 measured on the front of the micro:bit. Could you create a rear lamp for your bike which displays different patterns of LEDs according to the amount of light received?

Can you create a compass so that your micro:bit tells you in which direction you are walking?

Can you create a brake lamp which detects when you are slowing down?

You can also explore using external sensors connected to the pins of the micro:bit.

Here are some familiar objects which use sensors. Can you take photos of some more?