Measure Temperature and Humidity with micro:bit

Difficulty
Moderate
Steps
12

While the micro:bit can estimate the ambient temperature, it isn't as accurate as using an external temperature sensor. This is because the micro:bit actually measures the temperature of a silicon die on its CPU. 

In this guide, you will learn to use an external temperature sensor module and the micro:bit to make a temperature and humidity display! 

After completing this guide, you can use it to detect the temperature around you much more accurately.

Parts Used in This Guide

Buy

Step 1  The Module

Let's take a closer look at the DHT11 Module that we'll be working with today. It is capable of both temperature and humidity sensing. 

It has three pins:

  • Ground (GND)
  • Data Out (DATA)
  • Voltage Common Collector (VCC)

Step 2  Connect module to breadboard

Step 3  Connect GND to GND

Step 4  Connect P0 to DATA

Step 5  Connect VCC to 3.3V

Step 6  Download the DHT library


After creating the circuit, now it's time to start coding. First, you will need to download the package to program the temperature sensor using the Arduino IDE!

  1. Open the Arduino IDE. If you have not got it installed, please follow our previous guide on how to install and set up the Arduino IDE for micro:bit.
  2. Click on File > New
  3. Click on 'Tools' 
  4. Click on 'Manage libraries'
  5. Type 'DHT' in the search bar 
  6. Click on the install button for 'DHT sensor library' 

Note: An additional library may be required. If you get an error when trying to upload the code, please also search for and download 'Adafruit Unified Sensor' in the library manager.

Step 7  Include the necessary library and definitions


              #include "DHT.h"
#define DHTPIN 0     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);
            

Copy the following code into the Arduino IDE. 

There's an important library required here to get a temperature reading from the DHT11 using the micro:bit!

It is included with the command  #include "DHT.h"

Next, we need to indicate which pin is connected to the sensor's DATA pin. We connected it to Pin 0, so enter the following into the Arduino IDE: #define DHTPIN 0

The next definition involves indicating which DHT sensor type is being used. The sensor we are using today is the DHT11, however there are other models such as the DHT22. 

#define DHTTYPE DHT11



Step 8  void setup()


              void setup() {
  Serial.begin(9600);
  Serial.println("DHT11 Sensor");

  dht.begin();
}
            

Next, add the following code in the void setup() which is empty at the moment.

The Serial.begin() command sets the data rate for communicating with the computer. We will use 9600 bits per second (baud).

Next, to initialize the device, we use the command dht.begin();

Then, we will print the string, "DHT11 Sensor" in the serial monitor for viewing.

Step 9  void loop()


              void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Read Humidity
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.println("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("\n");
  Serial.println("Temperature: ");
  Serial.print(t);
  Serial.print("\n");
 
}
            

Now, in void loop() add the following code. 

  • delay(2000); ensures that there are two seconds between each measurement. 
  • We then create two float variables, h and t. 

h will store the humidity and t will store the temperature. 

How are they stored? Using the dht library, we can simply use the commands: dht.readHumidity(); and dht.readTemperature(); and store them in each variable respectively.

  • To check if the sensor has failed to get a reading, we use an if statement. 

if(isnan(h) || isnan(t)) 
This means "if h is not true or t is not true" then print to the serial monitor "Failed to read from DHT sensor!".

  • Finally, we want to print the temperature and humidity readings to Arduino IDE's in-built serial monitor display. It's a simple display window that we can output values to.

We simply use the command: Serial.print(t) and Serial.print(h)

Note: \t  prints a tab. \n will create a new line so it will look more readable on the monitor!

Step 10  The full code


              #include "DHT.h"
#define DHTPIN 0     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHT11 test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Read Humidity
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.println("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("\n");
  Serial.println("Temperature: ");
  Serial.print(t);
  Serial.print("\n");

}

            
  • Copy this code and upload it to your Micro:bit using the Arduino IDE.

Step 11  Upload the code

That's it! It's time to upload the code to the micro:bit. 

  • Click on the tick icon on the top left-hand corner to verify the code. 
  • Then click on the upload button next to it. 
  • Wait for the code to finish uploading. You will know it is done when it states 'Done uploading' on the bottom left-hand corner.

Step 12  Serial Monitor

Finally, let's open up the serial monitor!

  • Click on Tools > Serial Monitor  
  • You should now see temperature and humidity readings taken every two seconds.