In this guide, we will learn how to interface the DHT11 sensor with the ATmega2560 development board using embedded C code. The sensor data will be displayed on an LCD. The DHT11 is a low-cost temperature and humidity sensor that provides reliable readings.
The objective of this project is to measure the temperature and humidity in the surroundings and display on LCD using Atmega2560 development board.
ATmega2560 development board.
DHT 11 Sensor.
Micro USB cable.
Breadboard
Jumper Wires
Potentiometer (Built-in)
The DHT11 sensor is a widely used digital temperature and humidity sensor module capable of measuring ambient temperature and relative humidity in various applications.
Temperature Measurement: The DHT11 sensor can measure temperature within a range of 0 °C to 50 °C (32 °F to 122 °F) with a typical accuracy of ±2 °C.
Humidity Measurement: It can measure relative humidity within a range of 20% to 90% RH with a typical accuracy of ±5%.
Digital Output: The sensor provides a digital output, making it easy to interface with micro-controllers and digital systems. It uses a single-wire communication protocol, where the data is transmitted serially and can be read using a simple software routine.
For LCD connection, refer to this page.
Connect VCC of DHT11 sensor to 5v of the ATmega2560 development board.
Connect GND of DHT11 sensor to GND of the ATmega2560 development board.
Connect Data signal pin of the DHT11 sensor to the Pin 5 of the port G (Pin 1 of the development board)of ATmega2560 development board
Open VS Code and paste the code into the file named main.c
The previously downloaded library for LCD should also be present for this code to work, along with that dht11 can be found here. (It is a zip file where you will find a main.c code file which containes the DHT11 main function implementation.)
To compile and convert this into hex, run the following command in your terminal-
avr-gcc -Wall -g -Os -mmcu=atmega2560 -o dht11.hex main.c
/*
* DHT11sensor.c
*
* Created: 30-05-2023 12:30:24
* Author : udit2
*/
#ifndef F_CPU
#define F_CPU 16000000UL // set the CPU clock
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd.c"
#define DHT11_PIN 5
uint8_t c = 0, I_RH, D_RH, I_Temp, D_Temp, CheckSum;
void Request() /* Microcontroller send start pulse/request */
{
DDRG |= (1 << DHT11_PIN);
PORTG &= ~(1 << DHT11_PIN); /* set to low pin */
_delay_ms(20); /* wait for 20ms */
PORTG |= (1 << DHT11_PIN); /* set to high pin */
}
void Response() /* receive response from DHT11 */
{
DDRG &= ~(1 << DHT11_PIN);
while (PING & (1 << DHT11_PIN));
while ((PING & (1 << DHT11_PIN)) == 0);
while (PING & (1 << DHT11_PIN));
}
uint8_t Receive_data() /* receive data */
{
for (int q = 0; q < 8; q++)
{
while ((PING & (1 << DHT11_PIN)) == 0); /* check received bit 0 or 1 */
_delay_us(30);
if (PING & (1 << DHT11_PIN)) /* if high pulse is greater than 30ms */
c = (c << 1) | (0x01); /* then its logic HIGH */
else /* otherwise its logic LOW */
c = (c << 1);
while (PING & (1 << DHT11_PIN));
}
return c;
}
int main(void)
{
char data[5];
LCD_Init(); /* Initialize LCD */
LCD_Clear(); /* Clear LCD */
LCD_Line_Column(0, 0); /* Enter column and row position */
LCD_WriteString("Humidity =");
lcd_gotoxy(0, 1);
LCD_WriteString("Temp = ");
while (1)
{
Request(); /* send start pulse */
Response(); /* receive response */
I_RH = Receive_data(); /* store first eight bit in I_RH */
D_RH = Receive_data(); /* store next eight bit in D_RH */
I_Temp = Receive_data(); /* store next eight bit in I_Temp */
D_Temp = Receive_data(); /* store next eight bit in D_Temp */
CheckSum = Receive_data(); /* store next eight bit in CheckSum */
if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
{
lcd_gotoxy(0, 0);
LCD_WriteString("Error");
}
else
{
itoa(I_RH, data, 10);
lcd_gotoxy(10, 0);
LCD_string(data);
LCD_string(".");
itoa(D_RH, data, 10);
LCD_string(data);
LCD_string("%");
itoa(I_Temp, data, 10);
lcd_gotoxy(6, 1);
LCD_string(data);
LCD_string(".");
itoa(D_Temp, data, 10);
LCD_string(data);
LCD_WriteData(0xDF);
LCD_string("C ");
itoa(CheckSum, data, 10);
LCD_string(data);
LCD_string(" ");
}
_delay_ms(10);
}
}
Now open AVRDUDES and from the Programmer (-c) dropdown select Any usbasp clone with the correct VID/PID
In the Flash section, click on three dots and navigate to the desired hex file, and select it. Finally, click on Program to burn the hex file in the microcontroller.
Upon successful upload and execution of the code, the LCD will show the temperature and humidity readings obtained from the DHT11 sensor. The readings will be continuously updated on the LCD.
By following this guide and using the provided code, you should be able to successfully interface the DHT11 sensor with the ATmega2560 development board and display temperature and humidity readings on the LCD. Feel free to modify the code to suit your specific requirements and explore further possibilities with the DHT11 sensor and ATmega2560 board.