What to expect
Welcome back to SUPERCHARGE! Last week, we played the Electric Grid card game, where you built your own city’s electric grid using different forms of energy. If you have free time during the rest of this year, you can break out the Electric Grid card game whenever you have time. Feel free to modify or make up your own rules to the game!
This week, you will program a micro:bit device to measure different forms of energy in your classroom and around your school. If you’ve never programmed before, don’t worry! We’ll show you how. In case you get stuck, we’ll also include a link to the completed code that you can download to your device.
Connections
Last week in the Electric Grid card game, there were different Energy Generation cards to represent different forms of energy, each of which could be used to produce electricity. Today you will use a micro:bit device to directly measure some of the forms of energy that are all around you; light, sound, and heat.
Light, sound and heat are forms of energy that relate to problems that people in STEM careers are working to solve. Light, sound and heat pollution are challenges that affect everyone in Chicago. For young people, protecting your hearing is important because hearing loss is permanent. Scientists at the Centers for Disease Control estimate that 12.5% of adolescents aged 6–19, or about 5.2 million, have already suffered permanent hearing damage from excessive noise.
Some high-pitched sounds are more difficult for older people to hear than younger people. This is called presbycusis, a medical term that describes the gradual fading of our ability to hear high-pitched frequencies after age 25:
12,000 Hz: Difficult for people over 50 to hear
15,000 Hz: Difficult for people over 40 to hear
17,400 Hz: Only teenagers can hear, and most people over 18 usually can't detect it
Haptics is the sense of touch mediated by technologies (like a cell phone on vibrate mode). Technologists and engineers are using haptics to help people with hearing loss experience sounds. The Chicago Lyric Opera is one organization that provides haptics technology to audience members.
What problems related to heat, light, and sound can you solve?
Materials
Laptop or Computer with access to the MakeCode Website
USB to Micro USB cable
micro:bit (1 per person)
Scratch paper and pencil
Instructions
In this activity you will become familiar with the micro:bit computer. A micro:bit is a credit card-sized computer called a microcomputer. Microcomputers are small and inexpensive, but mighty! They can do many of the same things that a full-size computer can do. In this activity we will use the MakeCode website to create some computer code, then download it to your micro:bit device, and then use it to take measurements of different kinds of energy in your classroom and school building. The code will allow you to measure the temperature, sound level, and light level, which are different forms of energy.
The micro:bit device (shown in Figure 1) has numerous built-in sensors and two input buttons. Some of the built-in sensors on the micro:bit include a temperature sensor, light sensor, sound sensor, compass, and accelerometer. It also has two buttons, button A and button B. In between the buttons there is an array of LEDs that functions as a simple screen. Today, we will program the micro:bit to do the following tasks:
When button A is pressed: display the temperature in degrees F
When button B is pressed: display the sound level
When buttons A and B are pressed at the same time: display the light level
Figure 1. Micro:bit device (colors will vary) showing button A, the LED display, and button B
Steps to create the code
If you get stuck, ask your friends or teacher for help. If you are still having trouble, there is a link to the completed code at the bottom of the instructions.
Go to the MakeCode micro:bit website at https://makecode.microbit.org/
Click on “New Project”. Name your new project “Energy Measurement” or something similar, and click the green “Create!” button.
The code has three sections, which we will do one at a time.
Figure 2. Code to make the micro:bit display temperature in degree F when button A is pressed
CODE SECTION 1: When button A is pressed: display the temperature in degrees F
The first section of the code tells the micro:bit to display the temperature in degrees F when button A is pressed. This portion of the code will look like what you see on the left.
From the Input menu, drag the “on button A pressed” block out into your workspace. Note that all the blocks are color-coded so that you can see what menu they came from. The “on button A pressed” block and the Input menu are both bright purple.
From the Basic menu, drag the “clear screen” block onto your workspace and put it under the “on button A pressed” block. This tells the micro:bit to clear its LED display. You can tell that the “clear screen” block is from the Basic menu because they are both light blue.
Also from the Basic menu, drag the “pause (ms)” block onto your workspace and put it under the “on button A pressed” block. Change the pause time to 200 ms.
We need to tell the micro:bit to display the temperature. The micro:bit has a built-in temperature sensor, but its default is to show the temperature in degrees C. If we want to show the temp in degrees F, we need to convert from degrees C to degrees F. The conversion is:
Degrees F = Degrees C x 1.8 + 32
a. From the Math menu, drag a multiplication block, “0 x 0”, out onto the workspace but do not drop it under any other block. Change the second number in this block from 0 to 1.8.
b. From the Input menu, drag the “temperature (C)“ block onto the workspace and drop it into the first spot in the multiplication block that you just created.
c. From the Math menu, drag an addition block, “0 + 0” out onto the workspace but do not drop it under any other block. Change the second number in this block from 0 to 32.
d. Drag the entire multiplication block, “temperature (C) x 1.8”, into the first spot in the addition block that you just created.
e. Next, drag the “show number” block from the Basic menu and drop it under the “pause (ms)” block.
f. Drag the entire math block, “temperature (C) x 1.8 + 32” into the “show number” block.
5. To show the degrees symbol, drag the “show leds” block from the Basic menu and drop it under the math blocks that you just created. Click on the LEDs in the block to draw a “degrees” symbol.
6. Next, we want to show the temperature label, F. From the Basic menu, drag the “show string “Hello”” block onto the workspace and place it under the “show leds” block. In computer jargon, a “string” is a piece of text. Change the text Hello to F for degrees Fahrenheit.
7. Finally, from the Basic menu, drag the “clear screen” block and put it under the “show string” block. This tells the micro:bit to clear its LED display.
That’s it for the temperature display!
Figure 3. Code to make the micro:bit display sound level when button B is pressed
CODE SECTION 2: When button B is pressed: display the sound level
The last two pieces of the code are simpler, because there is no math required. A few other notes that might be useful as you are programming the micro:bit:
If you can’t find a particular block of code that you are trying to find, you can search for it by using the “Search” field at the top of the list of menus
You can learn more about how any block works by right-clicking on it and selecting “Help”
The second section of the code tells the micro:bit to display the sound level when button B is pressed. The code will look like what you see on the left.
Notice that all of these blocks are either bright purple or light blue, which means all of these blocks come from the Input or Basic menus. Add this code to your workspace next to (but not directly touching) the code that you have already created. Start by adding the “on button A pressed” block from the Input menu, and change “A” to “B”. Then add the clear screen, pause, show number, and clear screen blocks from the Basic menu and the sound level block from the Input menu.
Note: the sound level block displays a unitless number between 0 and 255, with 0 being quiet and 255 being very loud. If you wanted to convert this number into sound level units like decibels, you would have to do some math conversions. For us, this scale of 0 to 255 is good enough. Remember, you can learn more about the sound level measurement - or any other block - by right clicking on it and selecting “Help”.
Figure 4. Code to make the micro:bit display light level when buttons A AND B are pressed
Code Section 3: When buttons A AND B are pressed at the same time: display the light level
The last section of the code tells the micro:bit to display the light level when both buttons A AND B are pressed at the same time. The code will look like what you see on the left.
Again, note that all of these blocks are either bright purple or light blue, which means all of these blocks come from the Input or Basic menus. Add this code to your workspace next to (but not directly touching) the code that you have already created. Start by adding the “on button A pressed” block from the Input menu, and change “A” to “A+B”. Then add the clear screen, pause, show number, and clear screen blocks from the Basic menu and the light level block from the Input menu.
Note: the light level block is similar to the sound level block. It displays a unitless number between 0 and 255, with 0 being dark and 255 being very bright. If you wanted to convert this number into light level units like lumens, you would have to do some math conversions. For us, this scale of 0 to 255 is good enough.
Figure 5. Complete code
If you’re having difficulty, ask a friend or your teacher for help. If you’re still stuck, you can use this link to access the fully complete code: https://makecode.microbit.org/_1wRhgr2d61xe
After going to the link, click the "Edit" button to use the code.
Figure 6. Drag your file from the Downloads folder to the MICROBIT device
Now that you’re done with the code, you need to download it onto your micro:bit device so that you can use it. To download the code to your micro:bit:
Connect the USB-A side (the larger side) of the USB cable to your computer, and the micro USB (smaller side) of the cable to your micro:bit.
On the MakeCode website, click on the purple “Download” button on the lower left corner of your screen.
Go to the Downloads folder on your computer, and drag the file that you just downloaded into the MICROBIT device as shown in Figure 6. It will take a few seconds to transfer, and you will see a light blinking on your micro:bit. Occasionally there is a communication error between the computer and the micro:bit. If that happens, just unplug the cables and try again.
Test your code by pressing button A. Does it show the temperature in degrees F? Press button B. Does it show a sound level between 0 and 255? Press A and B together. Does it show a light level between 0 and 255? If any of these are not working correctly, double-check your code, make corrections, re-download the file to your computer, and re-download the file to the micro:bit.
If it works correctly, you can disconnect the micro:bit from the computer. Don’t worry, the file is saved on the micro:bit. Plug the battery pack into the micro:bit to turn it on again. Be careful - the battery pack connection is fragile. Never push or pull on the wires themselves; only the connector.
Congratulations! You have just created your first SUPERCHARGE code of the school year: a program that allows you to measure three different kinds of energy: temperature (thermal energy), sound, and light.
Using your micro:bit and a piece of paper, measure and record the temperature, sound, and light levels at different locations. Walk around your classroom and/or the hallway and nearby rooms, and make a map showing the temperature, sound level, and light level in different parts of your school. If you want, you can divide up into groups, with one person from each group taking temperature measurements, one person taking sound measurements, and one person taking light measurements. Then come back together as a group and compare your maps.
Turning the micro:bit off
To save batteries, you should turn off the micro:bit when you aren’t using it. There are two ways to turn the micro:bit off. Carefully disconnect the battery pack by pulling out the connector (don’t pull the wires themselves), or, press and hold the small black button next to the micro USB connection for about 5 seconds. To turn the micro:bit back on, simply re-connect the batteries or push the same small black button if the batteries are already connected.
Think about it
In this activity, we created our first micro:bit code of the year and used it to measure and record three different forms of energy: temperature, sound level, and light level.
How much variation did you see in the temperature, sound, and light levels between different locations in your school?
Is there any other form of energy that you would like to be able to measure with the micro:bit?
Do you have any questions about how the micro:bit works? Feel free to reach out to us!
Feedback Link: Click here to provide feedback on this activity
Next Time
In next week’s activity, we will use an electricity meter to measure how much power different electrical devices consume. Feel free to bring your own plug-in electrical device, like a phone or laptop charger. After that, we will use a micro:bit program similar to the one you created today to measure the light output from different kinds of light bulbs. This way, we can compare their light output to their power consumption.