Disclaimer: Keuwlsoft is a participant in the Amazon EU Associates Programme, an affiliate advertising programme designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.co.uk (also .com .de .fr .it .es & .ca). This means that if you purchase from any of the Amazon links on this page you will also be supporting Keuwlsoft.

dht11 demo

Temperature and Humidity


A DHT11 temperature and humidity sensor, together with an XBee Bluetooth shield are used to demonstrate the temperature and bubble gauge indicators on the Bluetooth Electronics app.

DHT11 Sensor


The DHT11 Sensor has 4 connections, one of which is not used. The sensor often comes packaged on a small PCB with three connections for Vcc, Data and GND.

dht11 sensor diagram
DHT11 Sensor.

Approx Specifications of the DHT11:

Humidity: 20 to 90 % RH , 5% accuracy, resolution of 1% RH.
Temperature: 0-50 °C, 2 °C, resolution of 1 °C.
Powered by 3-5.5V DC

The DHT11 will send a reading when its data line is held low for 18 ms and then returned high. It responds with a 80 ms low and then 80 ms high signal, to say 'ok, I got you, i'm working on it'. It then follows with 40 more pulses held High for varying lengths of time. If these pulses are high for 26-28 ms then it is sending a '0', whereas if it is 70 ms, then it is sending a '1'. These 40 bits correspond to 5 bytes. The first two bytes are the humidity with the most significant byte being first (byte 1). Since the sensor only has 8 bit resolution, no extra information is sent in byte 2, so it can be ignored. The 3rd and 4th bytes give the temperature in degrees Celsius. Again byte 4 has no extra information and can be ignored unless doing check sum checking. The fifth byte is a checksum which if not valid (e.g. some data has got corrupted in the transfer) then the Arduino should be programmed to ignore the reading. The checksum is not analysed in this demo, but should be considered if you intend to deploy a product with the sensor as part of it.


Components used



Circuit Diagram


circuit diagram

There's not much to this circuit. Just plug the Bluetooth module into the shield and the shield into the Arduino. Then connect the DHT11 sensor across pins 8-10 with GND on pin 10. Breadboard could easily have been used and the power lines connected so that only one digital pin is used. However this illustrates how sometimes the digital pins can be a source of power if the load is low (for example the DHT11 draws a maximum current of 2.5mA). Modifying the circuit/code to use alternate Bluetooth modules or power from the power lines is left to the user.

Arduino code


// DHT11 Temperature and Humidity Via Bluetooth
// By keuwlsoft:  www.keuwl.com  26th Sept 2015
// cc Attribution-ShareAlike

// The DHT11 returns 5 bytes of data:
// Bytes 1 & 2 are Humidity, Bytes 3 & 4 are Temperature
// and Byte 5 is the checksum (ignored in this demo).
// Since sensor is only 8-bit resolution, we ignore bytes 2 and 4.
// Internal 20k pullup on Arduino used, so no external 5k resistor needed.
// Powered from digital output lines so no breadboard/cables required.

int data_pin = 9;
int vcc_pin=8;
int gnd_pin=10;
boolean result[41]; //holds the result
int interval=2000; //Sample every 2 seconds
unsigned int temp; //in celcius
unsigned int humidity; //in %RH

void setup() {

  //Supply power to DHT11 (Uses upto 2.5mA)
    pinMode(gnd_pin, OUTPUT);
    pinMode(vcc_pin, OUTPUT);
    digitalWrite(gnd_pin,LOW);
    digitalWrite(vcc_pin,HIGH);
    
  //Initiate Serial for Bluetooth Communication
    Serial.begin(9600); 
}

void loop() {
  //Pause before taking next measurement
    delay(interval); 
 
  //Trigger reading by holding data pin low for 18ms
    pinMode(data_pin, OUTPUT);
    digitalWrite(data_pin,LOW);
    delay(18);
    digitalWrite(data_pin,HIGH);
    pinMode(data_pin, INPUT_PULLUP); 
  
  //read 41 bits of signal
    for(int i=0;i<=40;i++){
      result[i]=(pulseIn(data_pin, HIGH)>40);
    }

  //Extract Humidity (from byte 1)
    humidity=0;
    for (int i=1;i<=8;i++){
      humidity=humidity<<1;
      if (result[i]) humidity|=1;
    }
      
  //Extract Temperature (from Byte 3)
    temp=0;
    for (int i=17;i<=24;i++){
      temp=temp<<1;
      if (result[i]) temp|=1;
    }
    
   //Send data over Bluetooth
    Serial.print("*T"+String(temp)+"*");
    Serial.print("*H"+String(humidity)+"*");

}


Programming the Arduino


To program the device, make sure you remove pins 0 and 1 to the Bluetooth module otherwise the Arduino will get confused trying to communicate to two serial devices simultaneously on the same pins. On the XBee shield there is a switch to select whether the 0 and 1 pins are connected to the USB or XBee Bluetooth module. Make sure to switch back after programming. Run the Arduino software, select the correct COM Port and Arduino device in the Tools menu. Copy and paste the above sketch and click upload.

The pulseln() command returns the time in microseconds that a pin is HIGH or LOW. e.g. pulseIn(data_pin, HIGH) starts counting when the data pin goes HIGH and stops counting when it goes LOW again, returning the time in micro seconds that the data pin was held HIGH. If this time is 26 to 28 ms, the DHT11 is sending a '0', if it is longer, at 70 ms, the DHT11 is sending a '1'.

The strings sent over Bluetooth begin with '*T' or '*H' depending on which indicator on the Android device we want the information sent to (In this case a Temperature gauge indicator and a bubble gauge indicator). This 'receive character' can be set in the Edit screen of the 'Bluetooth Electronics' app. The string is terminated with a '*' to tell the app where the end of the string is so that it can start processing the command.

Bluetooth Electronics App


1) Run the Bluetooth Electronics app, click edit.

2) Select library and navigate to the 'Temperature and Humidity' demo and copy to panel.

3) Now connect to the Bluetooth device. Turn on power to your circuit so that the LED on the Bluetooth module starts flashing. Click connect on the main screen of the app. If not already paired, click on discover and wait for the device to appear in the list below. Select the device (e.g. HC-06) and click on pair. When requested you will need to enter a pin number, which is usually 1234 for these devices. Once paired, the device will appear on the right hand side. Select it and click on connect. Hopefully this was successful, return to the main screen.

4) The run button should be enabled now that we have connected to the Bluetooth device. Click run and test it out.