Micro:bit Temperature and Humidity Sensing

Updated 30 January 2019

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:

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.

microbit-temp-and-humidity-sensor-schematic

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

microbit-temp-and-humidity-sensor-complete-code

Blocks Used

Basic:

Forever: everything within these brackets runs continuously in a loop whenever the Micro:bit is powered.

Makecode-blocks-forever

Show string: this block scrolls text across the LED grid on the front of the Micro:bit.

Makecode-microbit-blocks-show-string

Show number: this block scrolls the number entered across the LED grid on the Micro:bit.

show-number-block-makeblock

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.

Makecode-block-environment-dht11

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.

Microbit-makecode-temp-and-humid-sense-code-step-1

In the search bar, type "DHT11" and hit search.

Microbit-makecode-temp-and-humid-sense-code-step-2

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.

Microbit-makecode-temp-and-humid-sense-code-step-3

Now to the program. Under the Basic menu, select the Basic: show string block and place it within the Basic: Forever brackets.

Microbit-makecode-temp-and-humid-sense-code-step-4

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".

Microbit-makecode-temp-and-humid-sense-code-step-5

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.

Microbit-makecode-temp-and-humid-sense-code-step-6

In the drop-down options in the Environment: value of DHT11 bubble, select Temperature, and pin P0.

Microbit-makecode-temp-and-humid-sense-code-step-7

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.

Microbit-makecode-temp-and-humid-sense-code-step-8

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.

Microbit-makecode-temp-and-humid-sense-code-step-9

From the Environment menu, drag the Environment: value of DHT11 bubble into the new Basic: show number block. 

Microbit-makecode-temp-and-humid-sense-code-step-10

Within the drop-down options of the Environment: show value of DHT11 bubble, change the reading to Humidity and the pin to P0.

Microbit-makecode-temp-and-humid-sense-code-step-11

That's it! Download the code to your Micro:bit and test it out!


Complete Code

Microbit-makecode-temp-and-humid-sense-code-complete

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.

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.