DHT11 Humidity and Temperature Sensor
Description
This project shows how to use the DHT11 humidity and temperature sensor. The DHT11 is a low-cost digital humidity and temperature sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air and puts out a digital signal on the data pin.
Technical sensor details: * Low cost * 3V to 5V power and I/O voltage * measures 20-80% humidity with 5% accuracy * measures 0-50°C temperature with ±2°C accuracy * size 15.5mm x 12mm x 5.5mm with 4 pins
Hardware
The hardware configuration is very simple. All what is needed is the DHT11 sensor and a 4.7k or 10k pull up resistor. VCC and GND are getting connected to the power supply, an IO pin of the microcontroller is connected via the pullup resistor to the data pin.
Block Diagram
Software
The driver which is getting described here is written in C language for the CCS C Compiler.
Driver configuration
To use the driver, you just have to include the header file dht11.h in your code. Before the include of the driver, you have to specify which pin of the PIC microcontroller shall be used for DHT11 communication, i.e. which pin is connected to the DHT11 data pin. In the following example it is connected to the pin RC0.
/* --- configure DHT11 sensor driver --- */ #define DHT11_PIN PIN_C0 #include
Reading Humidity and Temperature
First off all, the driver has to be initialized via DHT11_Init;
DHT11_Init();
Once this is done we can read the raw data via DHT11_ReadData. If the reading was successfull, DHT11_GetHumidity() and GetTemperature() return the humidity and temperature.
status = DHT11_ReadData(); switch (status) { case DHT11_OK: printf("DHT11 OK! humidity=%d%% temperature=%d°C\n\r", DHT11_GetHumidity(), GetTemperature()); break; case DHT11_ERROR_NOT_FOUND: printf("DHT11 not found!\n\r"); break; case DHT11_ERROR_CHECKSUM: printf("DHT11 checksum error!\n\r"); break; }
Driver usage example
void main() { uint8 status; uint8 cnt = 0; printf("\n\r*** DHT11 demo ***\n\r"); DHT11_Init(); while(TRUE) { delay_ms(5000); Led_On(); status = DHT11_ReadData(); printf("#%03u: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X ", cnt, DHT11_data[0], DHT11_data[1], DHT11_data[2], DHT11_data[3], DHT11_data[4]); switch (status) { case DHT11_OK: printf("DHT11 OK! humidity=%d%% temperature=%d°C\n\r", DHT11_GetHumidity(), GetTemperature()); break; case DHT11_ERROR_NOT_FOUND: printf("DHT11 not found!\n\r"); break; case DHT11_ERROR_CHECKSUM: printf("DHT11 checksum error!\n\r"); break; } delay_ms(100); } }
Here some pictures of my demo setup.
Download
- Demo project (CCS Compiler): dht_demo_v1..zip
- Driver source code (CCS Compiler): dht_driver_v1.0.zip