How to Interface DHT11 Sensor in Embedded C: Simple Guide
To interface a
DHT11 sensor in Embedded C, you need to send a start signal by pulling the data pin low, then read the sensor's response bits by timing the high and low pulses. Use GPIO pin control and precise delays to capture temperature and humidity data from the sensor.Syntax
The basic steps to interface the DHT11 sensor in Embedded C include:
- Start Signal: Pull the data pin low for at least 18 ms to signal the sensor to start.
- Response Signal: Release the pin and wait for the sensor to pull it low and then high, indicating it is ready to send data.
- Data Reading: Read 40 bits of data by measuring the length of high pulses (short pulse = 0, long pulse = 1).
- Data Processing: Convert the 40 bits into humidity and temperature values.
embedded_c
void DHT11_Start(void) { // Set pin as output DHT11_PIN_DIR = 1; // Pull pin low for 18 ms DHT11_PIN = 0; delay_ms(18); // Pull pin high for 20-40 us DHT11_PIN = 1; delay_us(30); // Set pin as input to read response DHT11_PIN_DIR = 0; } uint8_t DHT11_CheckResponse(void) { uint8_t response = 0; delay_us(40); if (!DHT11_PIN) { delay_us(80); if (DHT11_PIN) response = 1; delay_us(40); } return response; } uint8_t DHT11_Read(void) { uint8_t i,j; uint8_t data = 0; for (j=0; j<8; j++) { while(!DHT11_PIN); // wait for pin to go high delay_us(30); if(DHT11_PIN == 1) data |= (1 << (7-j)); while(DHT11_PIN); // wait for pin to go low } return data; }
Example
This example shows how to read temperature and humidity from the DHT11 sensor using Embedded C. It demonstrates sending the start signal, checking the response, reading data bytes, and printing the results.
embedded_c
#include <stdint.h> #include <stdio.h> #define DHT11_PIN (P1_0) // Example GPIO pin #define DHT11_PIN_DIR (P1DIR) // Direction register void delay_ms(int ms); void delay_us(int us); void DHT11_Start(void) { DHT11_PIN_DIR |= (1 << 0); // Set pin as output DHT11_PIN &= ~(1 << 0); // Pull pin low delay_ms(18); DHT11_PIN |= (1 << 0); // Pull pin high delay_us(30); DHT11_PIN_DIR &= ~(1 << 0); // Set pin as input } uint8_t DHT11_CheckResponse(void) { uint8_t response = 0; delay_us(40); if (!(DHT11_PIN & (1 << 0))) { delay_us(80); if (DHT11_PIN & (1 << 0)) response = 1; delay_us(40); } return response; } uint8_t DHT11_Read(void) { uint8_t i,j; uint8_t data = 0; for (j=0; j<8; j++) { while(!(DHT11_PIN & (1 << 0))); // wait for pin high delay_us(30); if (DHT11_PIN & (1 << 0)) data |= (1 << (7-j)); while(DHT11_PIN & (1 << 0)); // wait for pin low } return data; } int main() { uint8_t humidity_int, humidity_dec, temp_int, temp_dec, checksum; DHT11_Start(); if (DHT11_CheckResponse()) { humidity_int = DHT11_Read(); humidity_dec = DHT11_Read(); temp_int = DHT11_Read(); temp_dec = DHT11_Read(); checksum = DHT11_Read(); if ((humidity_int + humidity_dec + temp_int + temp_dec) == checksum) { printf("Humidity: %d.%d%%\n", humidity_int, humidity_dec); printf("Temperature: %d.%d C\n", temp_int, temp_dec); } else { printf("Checksum error\n"); } } else { printf("No response from sensor\n"); } return 0; } // Dummy delay functions for simulation void delay_ms(int ms) { /* Implement hardware specific delay */ } void delay_us(int us) { /* Implement hardware specific delay */ }
Output
Humidity: 45.0%
Temperature: 23.0 C
Common Pitfalls
Common mistakes when interfacing the DHT11 sensor include:
- Not setting the GPIO pin direction correctly before sending or reading data.
- Incorrect timing delays causing missed bits or wrong data.
- Ignoring the sensor's response signal, leading to failed communication.
- Not verifying the checksum, which can cause invalid readings to be accepted.
Always use precise microsecond delays and check the sensor's response before reading data.
embedded_c
/* Wrong: Not setting pin as input before reading data */ DHT11_Start(); // Missing pin direction change to input here if (DHT11_CheckResponse()) { // Reading data will fail } /* Right: Set pin as input after start signal */ DHT11_Start(); // Pin direction changed inside DHT11_Start() if (DHT11_CheckResponse()) { // Safe to read data }
Quick Reference
- Start Signal: Pull data pin low for 18 ms, then high for 20-40 µs.
- Response: Sensor pulls pin low 80 µs, then high 80 µs.
- Data Bits: 40 bits total: 16 bits humidity, 16 bits temperature, 8 bits checksum.
- Bit Value: 26-28 µs high pulse = 0, 70 µs high pulse = 1.
- Checksum: Sum of first 4 bytes must equal checksum byte.
Key Takeaways
Send a low pulse for at least 18 ms to start communication with the DHT11 sensor.
Use precise timing to read the sensor's 40-bit data stream correctly.
Always check the sensor's response signal before reading data.
Verify the checksum to ensure data integrity.
Set GPIO pin direction correctly when switching between output and input modes.