# DHT11 Temperature/Humidity Sensor

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

• Am also getting the same problem once i try to use pic16f877a, sorry if you have the solution on that please help me Justine Joseph – user59829 Dec 6 '14 at 1:59
• @JustineWambura after December 19 i'm going to post a step-by-step solution which includes mikroC code, proteus simulation, hardware images etc. If you think it's too late here is a hint: DHT11 sends 40 bits in sequence before get these bits you should start the signal, you can find an example here. In this example timer part can be confusing and problematic. Take a look at that part. Good luck :) – Sanberk Dec 7 '14 at 9:50

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;

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.

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.

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.

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.

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.

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.

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

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.

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(){
Data    = 0;
Delay_ms(25);
Data    = 1;
Delay_us(30);
}

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;
}

int i;
unsigned short num = 0;
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;
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;

DHT11 ATMEGA8 MikroC Programming...

sbit Data    at PORTC5_Bit;
sbit Data_In at PINC5_Bit;
sbit DataDir at DDC5_bit;

unsigned short k,l;

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

void DHT11StartSignal(){
Data    = 0;
Delay_ms(18);
Data    = 1;
Delay_us(40);
//Data = 0;
}

int DHT11CheckResponse(){
k = 150;
while(!Data_In)
{
Delay_us(10);
k--;
if(k<=1)
{
UART1_Write('D');
return 0;  // time out
}
}

k = 150;
while(Data_In)
{
Delay_us(20);
k--;
if(k<=1)
{
UART1_Write('T');
return 0;   // time out
}
}
return 1;
}

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

void main() {
UART1_Init(9600);
while(1){
again:
DHT11StartSignal();
if(!DHT11CheckResponse()) continue;
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);

// Wait
Delay_ms(1000);
}
}