Electronic – DHT11 Temperature/Humidity Sensor

microcontrollerpicsensoruart

Currently I am trying to read a byte from my dht11 sensor and write it to virtual terminal, but I couldn't find a simple datasheet for beginners. I found a bit complicated mikroC code and set-up which has pic16f628a and lcd. You can find it here That works, but if I add virtual terminal on it. There is nothing appear on virtual terminal even if I add required mikroC codes on it.
While my second try, I replace my microcontroller with pic16f877a, I can see this text on virtual terminal "No response from sensor" which is the error message of my mikroC code.

Here is some info about dht11;
DHT11 uses a simplified single-bus communication. Single bus that only one data line, the system of data exchange, control by a single bus to complete. It sends 40 bit Data format:
The 8bit humidity integer data + 8bit the Humidity decimal data +8 bit temperature integer data + 8bit fractional temperature data +8 bit parity bit.
You can also find datasheet from: here

As you can see there is no defective connection

Best Answer

A couple of weeks ago i wanted to set up a simple circuit, which is going to measure humidity and temperature level in air. When i start to research I don’t have any knowledge about how to set up a circuit or how can i embed a code in a microcontroller unit and many similar question.

So, below I will explain my little challange step by step. I hope it will be helpfull. Before we start you should refill your cups it might be a little long.

Now here is my components list;

Pic 16F877A (40-Pin) Crystal 4.0 MHz 22pF Capacitor 100nJ Capacitor (0.1uF) 330nJ Capacitor (0.3uF) DHT11 (Humidity & Temperature sensor) 1uF Capacitor (1 Micro F) 10uF Capacitor max232 RS232 DB9 Connector 9V battery & battery snap

Setting up;

1) Breadboard

Some kind of breadboards have positive or negative line through the board, on the other hand the other types’ power and ground lines might be diveded in two. You can figure out this via looking at the red and blue lines on the breadboard. And inner part of it, there are letters and numbers. Numbers represent the columns which are connected with each others. So, if I connect 5V to A-0 then B-0, C-0, D-0 and E-0 have same voltage. enter image description here

2) 7805 Regulator (9V to 5V)

If you read pic16f877a and dht11’s datasheets, you learned that they working with 5V but our battery is 9V. enter image description hereenter image description here

As you can see above you can easily regulate your voltage. Now you can feed your positive terminal/s from output of regulator.

3) DHT11 (Sensor)

This is a cheap, digital humidity and temperature sensor with 4 pin. Here is nice datasheet (because of my reputation i couldn't post more than 2 links. You can find it on google, if you find, take a look at two or more datasheet because some of them might not be clear enough) which is vital to understand how dht11 works. While you working on electronic projects you will come across with Vcc, Vdd, Vss, Vee pins in a really short explanation Vcc and Vdd are positive voltage supply, Vee and Vss are negative ground supply. So you can assume that Vcc and Vdd are same for the following pictures. enter image description hereenter image description here

4) Pic 16F877A (40 Pin)

This is a well known micro controller unit, easy to find and have enough leg for my project that’s why I prefer this pic. Here is datasheet: (unfortunately because of my reputation i couldn't post more than 2 links. You can find it on mikrochip's webpage) Datasheet show you the pin diagram. In diagram you will see numbers from 1 to 40. On following picture notice the small caved in point. It represents the first leg.

enter image description here

5) Crystal (Clock)

Just like every processor, 877a also needs to clock. Please don’t consider the scheme’s crystal Mhz, our circuit works with 4MHz Quartz crystal with 22pF Capacitors.

enter image description here

6) Max232

Ok, as i said that this project going to measure temperature - humidity and send these values to the computer. At this point, to able to get these values from our circuit to pc, we have to build a serial communication part. Do not be afraid of telecommunications words, with the help of RS-232 cable everything is really simple. Before connect the cable you should build the following scheme. On the picture you will see TTL serial TX and RX these two cable going to connect with the pic’s 25 (TX) and 26th (RX) pins.

enter image description here

Above scheme may be confusing imagine that at the bottom of the picture there is a ground and four black node (which are near to the bottom) connected to ground.

For complete proteus tutorial you can find my video below, but do not forget proteus is just a simulation and that is not guaranteed that your hardware is going to work properly. Especially frequency can cause some problems. Here is

My YouTube Video!

In mikroC code I set the leds according to the humidity if it is greater than 20% upper led turns on, and if humidity level pass over 30% 2nd led also turns on.

enter image description here

MikroC Code;

sbit Data    at RB0_bit;
sbit DataDir at TRISB0_bit;
unsigned short k;

unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;
char temp[] = "Temperature is 00.0 C";
char hum[] = "Humidity is 00.0 %";

void DHT11StartSignal(){
  DataDir = 0;
  Data    = 0;
  Delay_ms(25);
  Data    = 1;
  Delay_us(30);
  DataDir = 1;
}

unsigned short DHT11CheckResponse(){
  k = 150;
  while(!Data){
    Delay_us(2);
    k--;
    if(k<1) return 0;  // time out
  }
  k = 150;
  while(Data){
    Delay_us(2);
    k--;
    if(k<1) return 0;   // time out
  }
  return 1;
}


unsigned short DHT11ReadByte(){
  int i;
  unsigned short num = 0;
  DataDir = 1;
  for (i=0; i<8; i++){
    while(!Data);
    Delay_us(40);
    if(Data) num |= 1<<(7-i);
    while(Data);
  }
  return num;
}

void main() {
  UART1_Init(9600);
  TRISC.RC0 = 0;
  TRISC.RC1 = 0;
  TRISC.RC2 = 0;

  while(1){
    DHT11StartSignal();
    if(!DHT11CheckResponse()) continue;
    RH_Byte1 = DHT11ReadByte();
    RH_Byte2 = DHT11ReadByte();
    T_Byte1 = DHT11ReadByte();
    T_Byte2 = DHT11ReadByte();
    DHT11ReadByte(); /* Checksum */

    // Set temp
    temp[15] = T_Byte1/10 + 48;
    temp[16] = T_Byte1%10 + 48;
    temp[18] = T_Byte2/10 + 48;
    UART1_Write_Text(temp);
    UART1_Write(10);
    UART1_Write(13);

    // Set hum
    hum[12] = RH_Byte1/10 + 48;
    hum[13] = RH_Byte1%10 + 48;
    hum[15] = RH_Byte2/10 + 48;
    UART1_Write_Text(hum);
    UART1_Write(10);
    UART1_Write(13);
    UART1_Write(10);
    UART1_Write(13);

    PORTC.RC0 = (RH_Byte1) >= 20;
    PORTC.RC1 = (RH_Byte1) >= 30;
    PORTC.RC2 = (RH_Byte1) >= 40;

    // Wait
    Delay_ms(1000);
  }
}

And final part:

Embeding code into the MPU;

pic station

enter image description here

Related Topic