0% found this document useful (0 votes)
153 views4 pages

Interfacing PIC18F4550 With DHT22

This document describes interfacing a PIC18F4550 microcontroller with a DHT22 temperature and humidity sensor and displaying the results on an LCD. It includes a list of components, a brief setup description, and the code to initialize the microcontroller and sensor, read the temperature and humidity data, and display it on the LCD in Celsius and percent values every 2 seconds.

Uploaded by

Jahanzaib Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views4 pages

Interfacing PIC18F4550 With DHT22

This document describes interfacing a PIC18F4550 microcontroller with a DHT22 temperature and humidity sensor and displaying the results on an LCD. It includes a list of components, a brief setup description, and the code to initialize the microcontroller and sensor, read the temperature and humidity data, and display it on the LCD in Celsius and percent values every 2 seconds.

Uploaded by

Jahanzaib Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Interfacing PIC18F4550 with DHT22 (AM2302, RHT03) sensor

circuit
Components Required:
1. PIC18F4550 microcontroller
2. DHT22 (AM2302, RHT03)
3. 1602 LCD to display humidity and temperature results
4. 4.7k resistor

Setup

Code:
#include <xc.h>

#include <stdio.h>

#include <stdlib.h>

#include "lcd.h"

#pragma config FOSC = HS

#pragma config PWRT = OFF

#pragma config BOR = OFF


#pragma config WDT = OFF

#pragma config DEBUG = OFF

#pragma config LVP = OFF

#define _XTAL_FREQ 20000000 // Set the oscillator frequency

// Function prototypes

void Delay_ms(unsigned int milliseconds);

void DHT22_Start();

unsigned char DHT22_Check_Response();

unsigned char DHT22_Read();

void main()

unsigned char humidity_data[2];

unsigned char temperature_data[2];

float temperature, humidity;

char lcd_text[16];

TRISB = 0x00; // Set PORTB as output

PORTB = 0x00; // Initialize PORTB to 0

LCD_Init(); // Initialize the LCD

LCD_Clear(); // Clear the LCD

LCD_Set_Cursor(1, 1); // Set cursor to the first line

LCD_Write_String("Temperature:"); // Display "Temperature:"

LCD_Set_Cursor(2, 1); // Set cursor to the second line

LCD_Write_String("Humidity:"); // Display "Humidity:"

while (1)

DHT22_Start(); // Start communication with DHT22

if (DHT22_Check_Response()) // If DHT22 responds correctly


{

humidity_data[0] = DHT22_Read(); // Read humidity data (MSB)

humidity_data[1] = DHT22_Read(); // Read humidity data (LSB)

temperature_data[0] = DHT22_Read(); // Read temperature data (MSB)

temperature_data[1] = DHT22_Read(); // Read temperature data (LSB)

humidity = (float)((humidity_data[0] << 8) + humidity_data[1]) / 10; // Calculate humidity value

temperature = (float)((temperature_data[0] << 8) + temperature_data[1]) / 10;

sprintf(lcd_text, "%.1f C", temperature); // Format temperature for LCD display

LCD_Set_Cursor(1, 16); // Set cursor to the temperature position on LCD

LCD_Write_String(lcd_text); // Display temperature

sprintf(lcd_text, "%.1f %%", humidity); // Format humidity for LCD display

LCD_Set_Cursor(2, 10); // Set cursor to the humidity position on LCD

LCD_Write_String(lcd_text); // Display humidity

} Delay_ms(2000); // Wait for 2 seconds before next reading}

void Delay_ms(unsigned int milliseconds)

unsigned int I;

for (i = 0; i < milliseconds; i++)

__delay_ms(1);

void DHT22_Start()

TRISBbits.RB0 = 0; // Set RB0 as output

PORTBbits.RB0 = 0; // Pull data line low

Delay_ms(18); // Wait for at least 18m

PORTBbits.RB0 = 1; // Pull data line high

Delay_us(30); // Wait for 30us

}
unsigned char DHT22_Check_Response()

TRISBbits.RB0 = 1; // Set RB0 as input

if (PORTBbits.RB0)

Delay_us(40); // Wait for 40us

if (PORTBbits.RB0)

return 0;

Delay_us(80); // Wait for 80us

if (!PORTBbits.RB0)

return 0;

Delay_us(80); // Wait for 80us

return 1;

unsigned char DHT22_Read()

unsigned char i, data = 0;

for (i = 0; i < 8; i++)

while (!PORTBbits.RB0); // Wait until data line goes high

Delay_us(30); // Wait for 30us

if (PORTBbits.RB0)

data |= (1 << (7 - i)); // If data line is high, set the respective bit in data

while (PORTBbits.RB0); // Wait until data line goes low

return data;

You might also like