After interfacing the microcontroller PIC12F1822 with DHT11 sensor at:
DHT11 Interfacing with PIC12F1822 microcontroller – CCS C
Now let’s see how to interface this microcontroller with DHT22 (AM2302) digital relative humidity and temperature sensor.
In order to understand this example please read the DHT22 (AM2302) datasheet.
To see how to simulate the DHT22 Proteus simulation and timing diagram read the following topic:
PIC16F877A and DHT22(AM2302, RHT03) sensor Proteus simulation
And the following topic shows how to interface LCD with PIC12F1822:
Interfacing PIC12F1822 microcontroller with LCD display
Required Components:
- PIC12F1822 Microcontroller
- LCD (1602 or 2004 or any compatible)
- DHT22 (AM2302 – RHT03) Sensor
- 74HC595 Shift Register (74HC164, CD4094….)
- 10K Variable Resistor
- 4.7K Resistor
- +5V Power Supply
- Breadboard
- Jumper Wires
PIC12F1822 + LCD + DHT22 (AM2302) Sensor Circuit:
PIC12F1822 microcontroller internal oscillator is used and MCLR pin function is disabled in the software.
To see how to use 74HC164 or CD4094 instead of 74HC595 go to the link above.
The DHT22 (AM2302 – RHT03) sensor has 4 pins:
VCC : Positive power supply (+5V)
DATA : Sensor data input and output
NC : Not connected terminal
GND : Ground (0V)
A pull-up resistor must be added between the sensor data pin and VCC (+5V) pin as shown in the circuit schematic (4.7K ~ 10K).
PIC12F1822 + LCD + DHT22 (AM2302) Sensor CCS C code:
If you want to understand the code please read the DHT22 datasheet.
Variables Time_out and k are used to test reading time to avoid wrong data reception and microcontroller hanging.
The microcontroller runs with its internal oscillator at 32MHz (8MHz + PLL).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | // Interfacing PIC12F1822 with DHT22 sensor CCS C code // 3-Wire LCD driver must be added //LCD module connections #define LCD_DATA_PIN PIN_A0 #define LCD_CLOCK_PIN PIN_A1 #define LCD_EN_PIN PIN_A2 //End LCD module connections #include <12F1822.h> #fuses NOMCLR INTRC_IO PLL_SW #use delay(clock=32000000) #include <3WireLCD.c> #use fast_io(A) #define DHT22_PIN PIN_A4 // Connection pin between DHT22 and mcu short Time_out; char message1[] = "Temp = 00.0 C "; char message2[] = "RH = 00.0 % "; unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ; unsigned int16 Temp, RH; void start_signal(){ output_drive(DHT22_PIN); // Configure connection pin as output output_low(DHT22_PIN); // Connection pin output low delay_ms(25); output_high(DHT22_PIN); // Connection pin output high delay_us(30); output_float(DHT22_PIN); // Configure connection pin as input } short check_response(){ delay_us(40); if(!input(DHT22_PIN)){ // Read and test if connection pin is low delay_us(80); if(input(DHT22_PIN)){ // Read and test if connection pin is high delay_us(50); return 1; } } } unsigned int8 Read_Data(){ unsigned int8 i, k, _data = 0; // k is used to count 1 bit reading duration if(Time_out) break; for(i = 0; i < 8; i++){ k = 0; while(!input(DHT22_PIN)){ // Wait until DHT22 pin get raised k++; if(k > 100){ Time_out = 1; break; } delay_us(1); } delay_us(30); if(!input(DHT22_PIN)) bit_clear(_data, (7 - i)); // Clear bit (7 - i) else{ bit_set(_data, (7 - i)); // Set bit (7 - i) while(input(DHT22_PIN)){ // Wait until DHT22 pin goes low k++; if(k > 100){ Time_out = 1; break; } delay_us(1);} } } return _data; } void main() { setup_oscillator(OSC_8MHZ | OSC_PLL_ON); // Set internal oscillator to 32MHz (8MHz and PLL) lcd_initialize(); // Initialize LCD module lcd_cmd(LCD_CLEAR); // LCD Clear while(TRUE){ delay_ms(1000); Time_out = 0; Start_signal(); if(check_response()){ // If there is a response from sensor RH_byte1 = Read_Data(); // read RH byte1 RH_byte2 = Read_Data(); // read RH byte2 T_byte1 = Read_Data(); // read T byte1 T_byte2 = Read_Data(); // read T byte2 Checksum = Read_Data(); // read checksum if(Time_out){ // If reading takes long time lcd_goto(1, 1); // Go to column 1 row 1 lcd_out(" Time out! "); lcd_goto(1, 2); // Go to column 1 row 2 lcd_out(" "); // Clear 2nd row } else{ if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){ RH = RH_byte1; RH = (RH << 8) | RH_byte2; Temp = T_byte1; Temp = (Temp << 8) | T_byte2; if (Temp > 0X8000){ message1[6] = '-'; Temp = Temp & 0X7FFF; } else message1[6] = ' '; message1[7] = (Temp / 100) % 10 + 48; message1[8] = (Temp / 10) % 10 + 48; message1[10] = Temp % 10 + 48; message2[7] = (RH / 100) % 10 + 48; message2[8] = (RH / 10) % 10 + 48; message2[10] = RH % 10 + 48; message1[11] = 223; // Degree symbol lcd_goto(1, 1); // Go to column 1 row 1 printf(lcd_out, message1); // Display message1 lcd_goto(1, 2); // Go to column 1 row 2 printf(lcd_out, message2); // Display message2 } else { lcd_goto(1, 1); // Go to column 1 row 1 lcd_out("Checksum Error! "); lcd_goto(1, 2); // Go to column 1 row 2 lcd_out(" "); // Clear 2nd row } } } else { lcd_goto(1, 1); // Go to column 1 row 1 lcd_out(" No response "); lcd_goto(1, 2); // Go to column 1 row 2 lcd_out("from the sensor "); } } } |
PIC12F1822 + LCD + DHT22 (AM2302) Sensor Video:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.