Lesson 4: Environment Exploration Program
To Know - Create a Environment Exploration Program
To Be Able To - Develop a Environment Exploration Program
Step 1: Make It
In this tutorial, we’ll learn how to create a program that uses the micro:bit to explore the environment by detecting light, temperature, and sound levels. This program will let you check these conditions by pressing different buttons on the micro:bit.
Temperature
We'll use the micro:bit as a thermometer to measure differences in temperature in different places. When we press button A we will show the temperature in degrees Celsius on the display.
TIP: As the thermometer takes time to adjust to temperature, it’s a good idea to leave the micro:bit in a new location for a couple of minutes to ensure you get an accurate reading.
Sound
We'll use the micro:bit as sound level meter. When we press button B we'll show the sound level on a scale from 0 (quietest) to 255 (loudest).
We'll add a short delay before it takes the sound reading to make sure the sound of pressing the button isn’t recorded.
Light
We'll use the micro:bit as a light meter. When we press the touch logo, it will show a light level reading, on a scale from 0 (darkest) to 255 (lightest).
Again, we'll add a short delay before it takes the light reading to make sure our hand isn't blocking the light as we touch the button.
Your flowchart should look similar to the example shown.
Step 2: Code It
Function - This line imports all the micro:bit libraries, which contain tools for using its buttons, screen, and sensors.
Python Code - from microbit import *
Function - This starts a loop that will keep running as long as the micro:bit is on. It lets us continuously check if a shake gesture is detected.
Python Code - while True:
display.read_light_level()
Measures the light level around the Micro:bit
display.scroll()
Will scroll this light level across the LED display as a number.
sleep(200)
Pauses the program for 200 milliseconds to avoid multiple readings happening too quickly.
elif
The elif statement means "else if" and is checked only if the previous condition wasn’t true.
button_b.is_pressed():
Next, we check if button B is pressed.
display.scroll(temperature)
temperature() measures the current temperature in degrees Celsius and displays it on the LED screen.
sleep(200)
adds a short pause for better usability.
elif
The final elif statement check if the touch logo is touched
pin_logo.is_touched():
Pin logo is the touch sensitive logo above the LED display
sleep(2000)
Adds a 2 second pause so that the sound of the button isn't detected by the microphone.
display.scroll(microphone.sound_level())
Measures the current sound level around the micro:bit. This sound level is then scrolled across the LED display.
Step 3: Collect the Data
Using the micro:bit, collect environment data from six different locations around the school.
Use the Data Collection sheet to record the readings taken with the micro:bit.
Remember to wait for 30 seconds to allow the thermometer to adjust to the ambient temperature before taking a reading.
HINT: gathering as much data as you can is good scientific practice, so you might want to take several readings in each location and calculate an average.
Step 4: Analyse your Data
Once you’ve recorded your data you can analyse it to draw conclusions. What is there to learn from your data about temperature, sound and light levels around you?
Where was warmest?
Where was coolest?
Where was loudest?
Where was quietest?
Where was lightest?
Where was darkest?
What factors may have affected this?