The Micro:bit has the ability to measure temperature by sensing the temperature of the silicon die inside the main CPU. This works because the Micro:bit runs cold, and the temperature of the CPU is usually a good approximation of ambient temperature. What do we do if we want more precise measurements of the temperature, or if we want to measure the temperature of something that is external to the board or in a harsh environment? We use an external sensor of course! In this tutorial, we will use the simple and inexpensive DHT11 Sensor. These sensors are simple and pretty robust and have the added feature of being able to sense humidity. This is the perfect sensor to put inside a hot and humid place that you wouldn't want to subject your Micro:bit to, like the inside of a terrarium.
This project will use:
- A Micro:bit
- A DHT11 Sensor (any DHT11 will work, just be aware that the pins are different on some models)
- Alligator Clips to Jumper Pin
- Female to Female Jumpers
- Battery Pack (optional)
Function
This project will take readings of both temperature and humidity, and scroll the values across the LED array on the front of the Micro:bit. Prior to each value being displayed the word "Temp:" and "Hum:" will scroll to identify the value.
The Circuit
The DHT11 Sensor has just three pins to connect it. To keep things solderless we can use Female-Female jumper wires, and Alligator Clips to Male Jumper Pins to connect the sensor to the Micro:bit. Just push the female jumpers onto the DHT11 sensor pins, connect the pin side of the alligator clips to the jumper wires, and clip the Alligator clips on the edge of the Micro:bit.
Downloading Code to the Micro:bit
To load a program onto the Micro:bit, first connect it to your computer with a micro USB cable. The Micro:bit will appear as a drive on your computer called MICROBIT.
When you are ready to try your code just click the DOWNLOAD button at the bottom left corner of the screen in MakeCode. Save the file inside the MICROBIT drive. If the download saves somewhere else, just drag and drop it into the MICROBIT drive.
The light on the Micro:bit will flash for a few seconds, and when it stops your program will be loaded and the Micro:bit will run it every time its powered up (until you replace it with a new program).
The Code
First, we will show you the completed code, then we will show how its done step by step!
MakeCode Micro:bit Temperature and Humidity Sensing Code
Blocks Used
Basic:
Forever: everything within these brackets runs continuously in a loop whenever the Micro:bit is powered.
Show string: this block scrolls text across the LED grid on the front of the Micro:bit.
Show number: this block scrolls the number entered across the LED grid on the Micro:bit.
Environment:
This is a special class of blocks that must be added to MakeCode by going to Advanced, Extensions, and searching for DHT11.
Value of DHT11: This block reads the DHT11 Sensor on whatever pin it is connected to.
Measuring Temperature and Humidity
The first thing we need to do is add the necessary blocks to interface with the DHT11. Under the menu Advanced, select Extensions.
In the search bar, type "DHT11" and hit search.
The only result should be for the IoT-environment-kit. We won't be using this kit, but this extension includes the blocks we need to use a DHT11 with the Micro:bit.
Now to the program. Under the Basic menu, select the Basic: show string block and place it within the Basic: Forever brackets.
From the Basic menu, drag a Basic: show number block into the bottom of the Basic: forever brackets. Change the string in the Basic: show string block to read "Temp".
In the newly added Environment menu, select the Environment: value of DHT11 bubble and place it in the Basic: show number block. Whenever a block is a bubble shape, it will return a value that is a number.
In the drop-down options in the Environment: value of DHT11 bubble, select Temperature, and pin P0.
We now have a working program that will display the word "Temp:" and then display the temperature reading. We will now add humidity sensing into the program
From the Basic menu, drag a Basic: show string block into the bottom of the Basic: forever brackets.
Place a Basic: show number block in the bottom of the Basic: forever brackets. Change the string in the Basic: show string to "Hum:" for humidity.
From the Environment menu, drag the Environment: value of DHT11 bubble into the new Basic: show number block.
Within the drop-down options of the Environment: show value of DHT11 bubble, change the reading to Humidity and the pin to P0.
That's it! Download the code to your Micro:bit and test it out!
Complete Code
Going Further
To find more projects with the Micro:bit, check out our Micro:bit Tutorials! To add more to this project you could change it to include data logging or plot the readings over time! A programming challenge would be code that would take the median reading automatically.