The Micro:bit is a feature-packed development board that’s perfect for the classroom or the beginner programmer. Each Micro:bit has a built-in radio that can transmit and receive signals sent from other Micro:bits. The radio has a range of up to 70 meters and is perfect for collecting remote data or communicating between devices. For this project, we will be making a natural disaster sensor. We will monitor the wind, temperature, and seismic activity (motion). There are no limits to the sensors that can interface with on the Micro:bit. We will use onboard sensors and external sensors for this project, and even make a sensor of our own out of a motor! We will program our Micro:bits in MakeCode and we will monitor each element with a dedicated Micro:bit and transmit our readings back to a single receiving Micro:bit that will be connected to a computer. From there we will graph our readings, store them, and even export them to a spreadsheet!
To follow along with this Tutorial you will need the following parts:
- Four Micro:bits (Most parts are freestanding so if you only have one you can still follow along)
- A computer with a USB port
- A Hobby Motor
- Alligator Clips
- A Breadboard
- A Sparkfun Micro:bit Breakout w/ Headers
- A YuRobot 3.3v/5v Breadboard Power Supply
- A 9V Power Supply
- An LM35DZ Analog Temperature Sensor
- Jumpers, M-M, M-F
- Two or more Battery Packs
If you have any questions during this tutorial, please reach out on the Forum! We are here to help!
- Wind Sensor
- Building the Wind Sensor
- Wind Transmitter Code
- Going Further
- Temperature Sensor
- Build the Temperature Sensor
- The Code
- Going Further
- Seismic Sensor
- The Code
- Going Further
- Data Monitoring
- The Receiver
- The Code
- Going Further
Building the Wind Sensor
Sure you can use an Anemometer that will give you precise readings about wind speed, but its way more fun to make one yourself! We will make a simple wind speed sensor out these parts:
- A hobby motor
- Paddle pop sticks
- A paper towel or toilet paper roll
- Hot glue
- A small cardboard box or scrap cardboard for the base
When you spin the hobby motor backwards it generates a tiny amount of electricity. We can read the voltage that the motor generates using the analog read command from MakeCode. We checked this out on an oscilloscope beforehand to make sure we wouldn’t be generating enough voltage to damage the Micro:bit. There is no danger of overvoltage from this motor.
The first step is to cut apart the paper roll into small fan blades, and glue them onto the paddle pop sticks.
Use a craft knife to make a small hole in the centre of your propeller blade, and put it on the end of the hobby motor. It's good to be a little tight. Hot glue the second propeller blade on top.
Use two more paddle pop sticks and some hot glue to make a mount for the motor.
Use the knife to cut two slits in your cardboard box or base, and push your fan inside.
Use alligator clips to connect the positive lead from the motor to one of the Analog Inputs. We’re using P2. Connect the negative lead from the motor to ground. This sensor is set up to spin clockwise. If it spins counter-clockwise then you won't get a reading. If you make your fan so it spins the wrong way, just swap your leads and it will still work.
Wind Transmitter Code
We will use MakeCode for Micro:bit for all our coding for this project. On start we will set our radio transmit power to maximum, and set our radio group to 1 so all our Micro:bits are on the same channel. We will then scroll the text “Wind” so we can identify the device.
We don’t really need to send wind reading constantly since wind isn’t constantly changing. So we have two forever loops. The first plots the readings from our wind sensor on P2 up to 200. The second forever loop transmits the wind sensor readings every 9 seconds.
There are some obvious limitations to our homemade wind sensor. It can only detect wind from one direction for starters. An anemometer has a design that allows it to always spin the same direction no matter what angle the wind hits it from. The next level of design challenge might be to create a more effective anemometer design (did I hear someone say 3D printing?).
The other limitation is calibration. We are reading an analog voltage from the motor, but how that translates into actual windspeed would take testing and calibration. Spinning the sensor with known windspeeds and recording the output would allow you to create a plot graph and make a formula to calculate actual windspeed effectively. This sensor won’t have a linear output as wind speed increases!
Building the Temperature Sensor
The next element that we are going to monitor is temperature. The Micro:bit has an onboard temperature sensor, but we use an external sensor to demonstrate how it works. We will be using an LM35DZ analog temperature sensor for our temperature sensing, and we will connect it to Analog Input P0. The advantage of using an external sensor is that you can put the temperature probe places that you couldn’t put the Micro:bit directly, like near water, or you could measure the temperature from five sensors at once!
The LM35DZ may not work reliably with voltage under 5v, and the Micro:bit uses 3.3v. You may get lucky and get good results with less voltage, but for this project, we’ve used a YuRobot BreadBoard Power Supply. This can provide 3.3v and 5v, directly to a breadboard, and can be powered by USB or a power supply. We’ve also used a Sparkfun Micro:bit breakout to easily connect our Micro:bit to the breadboard. The LM35DZ is pretty small, and not really easy to connect with alligator clips. Its best to use a breadboard, or solder some wires onto its leads. If soldering isn’t your thing you could also use some male to female jumpers.
Pay Careful attention to the pinout of the LM35DZ. Connecting it incorrectly will cause it to burn up! This view is looking up from the bottom.
We start the code by setting the radio power to maximum, and the radio group to be the same as the receiver. On start, we also scroll the word “Temp”. The temperature is read through the analog read pin P0. We then multiply the input by 100 and then divide by 1024 to convert the raw analog input to Celsius. The value is plotted and transmitted every six seconds.
This element of the project could be expanded on by going into detail about how the temperature formula is made. The datasheet for the LM35DZ provides a formula for calculating the temperature, and some basic algebra will land you on the formula. Also, you can add a resistor into the circuit for more precise measurement.
The Micro:bit has five analog inputs, and could read five external analog temperature sensors at once.
The Micro:bit has an accelerometer built right in. We can use this sensor to simulate an earthquake sensor. By adding the acceleration of the x and y-axis we can measure the total acceleration the board is undergoing. To make it into a seismic sensor it should be anchored to something immobile like a slab of concrete. We don’t want to wait for an earthquake to test our program so we left it loose.
Like the previous transmitters, we start our code by setting the radio power and radio group. We then scroll text that identifies the Micro:bit sensor in use.
In the forever loop readings are taken from the accelerometer axis and added together. We used the absolute value so any movement will result in a positive number. The number is then plotted to the device and transmitted to the central receiver.
There are a few ways we could increase the complexity of an earthquake sensor. We could start by measuring each axis individually, including the z-axis. During an earthquake, the ground moves in waves, so the vertical motion that the board undergoes would be relevant data. Three networked Micro:bits could be used to triangulate an earthquake's epicentre if its strong enough to be measured on each device. This could be done with a timestamp and some data logging.
We are collecting all this great data from remote (but not too remote) sensors and sending it out hoping someone will listen. Now its time to make the receiver. For this part of the project, we will use a Micro:bit connected via USB to a computer running MakeCode for Micro:bit. It’s important to note that we will need to use the Windows App version of the software so we can read the serial data that is outputting from the device, and save it to a spreadsheet.
On Start, we set our radio group, and initialize two variables, “Mode” and “Text”. We change the mode by pressing button A or B, but we start in Mode 0 which is temperature. We will change what data is displayed on the device by cycling through with the buttons. All the data will be available in MakeCode at all times.
When button A or B is pressed, we set Mode to 3, so no data is being plotted on the LEDs, then change the text up or down by one, and scroll the text associated with that mode. Once the text has scrolled we set the mode to the same number as the text so the data is plotted. We need to disable the data plotting temporarily so the text will show, otherwise it will try to show both simultaneously and it won't work.
When we are in mode 0, the temperature is plotted. In mode 1 the seismic data is plotted, and in mode 2 the wind data is plotted. The Micro:bit converted the LEDs into a vertical bar graph and plots the data with the maximum being defined by the second number on the block.
On our other Micro:bits we transmitted our data with a text string and then the value. On radio received we save the received string as “name” and the number as “value”. We then print the current running time to serial to use as a timestamp. Some simple logic separates the incoming values into unique variables and writes them to serial.
If we turn on our three remote sensors and connect our receiver, we can then follow the link for “Show data Device”. This is live data from the receiving Micro:bit. The first plotted field has no label, and it is created by the “plot bar graph” block that is currently selected. The rest of the data will have a label so we can clearly identify it. We can hit the download button in the top right of the screen to save the collected data as a .csv. You need to be in the device console for the data to be logged, so don’t leave this screen before saving your data!
Being able to download all the collected data with timestamps makes it possible to analyze the data that has been collected. You could also potentially create a method to remove errors from collected data within the spreadsheets. You may notice that there is a small amount of error inherent to devices like the accelerometer. Rather than trying to eliminate it with code, you would likely reach more repeatable results by creating a formula to remove it from your spreadsheet.
Now that you are done with all the parts of the natural disaster sensor project, all we need to do is make nice disaster proof housings for our sensors and wait for disaster to strike! Perhaps we can just simulate one? If you are interested in learning more about the BBC Micro:bit, there is a growing section of Tutorials for the BBC Micro:bit. If you enjoy programming with MakeCode and want to try out a board a little more suitable for wearables or the beginner programmer, check out the Circuit Playground Express and look at our Circuit Playground Express Tutorials or our Circuit Playground Online Workshop!