This post shows how to interface PIC18F46K22 microcontroller with DHT22 digital humidity and temperature sensor.
The PIC18F46K22 MCU reads temperature (in °C) & humidity (in rH%) values from the DHT22 sensor and print them on SSD1306 OLED display (128×64 pixel).
MikroC PRO for PIC compiler is used in this project.
In this project the SSD1306 OLED is configured to work in I2C mode, make sure that your display is configured to work in I2C mode, some displays need jumper placing or some soldering.
To see how to interface PIC18F46K22 MCU with SSD1306 OLED display (I2C mode), take a look at the following project:
Interfacing PIC18F46K22 with SSD1306 OLED display | mikroC Projects
Hardware Required:
- PIC18F46K22 microcontroller —-> datasheet
- SSD1306 OLED display
- DHT22 (AM2302, RHT03 …) humidity and temperature sensor —-> datasheet
- 4.7k ohm resistor
- 5V source
- Breadboard
- Jumper wires
PIC18F46K22 with SSD1306 OLED and DHT22 sensor circuit:
The image below shows project circuit diagram.
All the grounded terminals are connected together.
The DHT22 sensor has 4 pins (from left to right):
Pin 1 is power supply pin, connected to circuit +5V
Pin 2: data output pin, connected to PIC18F46K22 RD5 pin (#28)
Pin 3: not connected pin
Pin 4: GND (ground), connected to circuit ground.
The PIC18F46K22 microcontroller has 2 hardware I2C modules (MSSP1 and MSSP2 modules).
In this project I2C1 module is used with SDA1 on pin RC4 (#23) and SCL1 on pin RC3 (#18). The SDA1 pin of the MCU is connected to the SDA pin of the display and the SCL1 pin of the MCU is connected to the SCL pin of the display.
The reset pin of the display is connected to pin RD4 (#27) of the microcontroller.
The SSD1306 OLED display DC pin is connected to VDD which means I2C slave address of the device is 0x7A.
In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 16 MHz, MCLR pin is configured as an input pin.
PIC18F46K22 with SSD1306 OLED and DHT22 sensor C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.
To be able to compile the C code below with no error, a driver for the SSD1306 OLED display is required, its full name (with extension) is SSD1306OLED.C, download link is the one below:
SSD1306 OLED mikroC library
for more information about this driver, visit the following post:
SSD1306 OLED display library for mikroC compiler | mikroC Projects
after the download, add the driver file to mikroC project folder.
The connection of the DHT22 sensor data pin and the SSD1306 OLED reset pin are defined in the C code as shown below:
1 2 3 4 5 6 7 | // DHT22 pin connection (here data pin is connected to pin RD5) #define DHT22_PIN RD5_bit #define DHT22_PIN_DIR TRISD5_bit // SSD1306 OLED reset pin definition (if available) #define SSD1306_RST RD4_bit #define SSD1306_RST_DIR TRISD4_bit |
Functions used in the code:
void Start_Signal(void): sends start signal to the DHT22 sensor.
char Check_Response(): checks response signal of the DHT22 sensor (after sending the start signal using the previous function), returns 1 if OK and 0 if error.
void Read_Data(unsigned short* dht_data): reads 1 byte from the DHT22 sensor which is saved to the variable dht_data.
If there is a problem with the DHT22 sensor (for example bad connection) the display will display Sensor (instead of the temperature value) Error (instead of the humidity value), and if there is a check sum error (for example due to wrong data reading) the display will display Checksum (instead of the temperature value) Error (instead of the humidity value).
Timer0 module is used to measure pulse widths (high and low), it’s configured to work as 8-bit timer/counter and prescaler = 4 (increment by 1 every 1 microsecond ==> clock input = 1MHz).
Timer0 clock input = Fosc/(4 x prescaler) = 16MHz/(4 x 4) = 1MHz.
Rest of code is described through comments.
Full mikroC code:
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | /************************************************************************************** Interfacing PIC18F46K22 microcontroller with SSD1306 OLED (128x64 Pixel) and DHT22 (AM2302) temperature and humidity sensor. C Code for mikroC PRO for PIC compiler. Internal oscillator used @ 16MHz Configuration words: CONFIG1H = 0x0028 CONFIG2L = 0x0018 CONFIG2H = 0x003C CONFIG3H = 0x0037 CONFIG4L = 0x0081 CONFIG5L = 0x000F CONFIG5H = 0x00C0 CONFIG6L = 0x000F CONFIG6H = 0x00E0 CONFIG7L = 0x000F CONFIG7H = 0x0040 This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ // DHT22 pin connection (here data pin is connected to pin RD5) #define DHT22_PIN RD5_bit #define DHT22_PIN_DIR TRISD5_bit // SSD1306 OLED reset pin definition (if available) #define SSD1306_RST RD4_bit #define SSD1306_RST_DIR TRISD4_bit #include <SSD1306OLED.c> // include SSD1306 OLED display driver source code // send start signal to the sensor void Start_Signal(void) { DHT22_PIN = 0; // connection pin output low DHT22_PIN_DIR = 0; // configure connection pin as output delay_ms(25); // wait 25 ms DHT22_PIN = 1; // connection pin output high delay_us(30); // wait 30 us DHT22_PIN_DIR = 1; // configure connection pin as input } // check sensor response char Check_Response() { TMR0L = 0; // rest Timer0 low register // wait until DHT22_PIN becomes high (checking of 80µs low time response) while(!DHT22_PIN && TMR0L < 100) ; if(TMR0L >= 100) // if response time > 80µS ==> response error return 0; // return 0 (device has a problem with response) TMR0L = 0; // rest Timer0 low register // wait until DHT22_PIN becomes low (checking of 80µs high time response) while(DHT22_PIN && TMR0L < 100) ; if(TMR0L >= 100) // if response time > 80µS ==> response error return 0; // return 0 (device has a problem with response) return 1; // return 1 (response OK) } // data read function void Read_Data(unsigned short* dht_data) { char i; *dht_data = 0; for(i = 0; i < 8; i++) { TMR0L = 0; // rest Timer0 low register while(!DHT22_PIN) // wait until DHT22_PIN becomes high { if(TMR0L > 80) // if low time > 80µS ==> Time out error (Normally it takes 50µs) return; // return (timeout error) } TMR0L = 0; // rest Timer0 low register while(DHT22_PIN) // wait until DHT22_PIN becomes low { if(TMR0L > 80) // if high time > 80µS ==> Time out error (normally it takes 26-28µs for 0 and 70µs for 1) return; // return (timeout error) } if(TMR0L > 40) // if high time > 40µS ==> sensor sent 1 *dht_data |= (1 << (7 - i)); // set bit (7 - i) } return; // return (data read OK) } // main function void main() { OSCCON = 0x70; // set internal oscillator to 16MHz ANSELC = 0; // configure all PORTC pins as digital ANSELD = 0; // configure all PORTD pins as digital T0CON = 0x41; // configure Timer0 module (8-bit timer & prescaler = 4) I2C1_Init(400000); // initialize I2C communication with clock frequency of 400kHz delay_ms(1000); // wait a second // initialize the SSD1306 OLED with an I2C addr = 0x7A (default address) SSD1306_Begin(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS); SSD1306_ClearDisplay(); // clear the display buffer SSD1306_SetTextWrap(false); // disable text wrap SSD1306_GotoXY(29 , 0); SSD1306_Print("TEMPERATURE:"); SSD1306_GotoXY(38 , 37); SSD1306_Print("HUMIDITY:"); SSD1306_Display(); SSD1306_TextSize(2); // text size = 2 while(1) { char T_Byte1, T_Byte2, RH_Byte1, RH_Byte2, CheckSum; unsigned int Temp, Humi; char temp_a[9], humi_a[8]; bit sensor_ok; TMR0ON_bit = 1; // enable Timer0 module Start_Signal(); // send a start signal to the sensor if(Check_Response()) // check if there is a response from sensor (if OK start reading humidity and temperature data) { // response OK ==> read (and save) data from the DHT22 sensor Read_Data(&RH_Byte1); // read humidity 1st byte Read_Data(&RH_Byte2); // read humidity 2nd byte Read_Data(&T_Byte1); // read temperature 1st byte Read_Data(&T_Byte2); // read temperature 2nd byte Read_Data(&CheckSum); // read checksum TMR0ON_bit = 0; // disable Timer0 module // test if all data were sent correctly if( CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF) ) { sensor_ok = 1; Humi = (RH_Byte1 << 8) | RH_Byte2; Temp = (T_Byte1 << 8) | T_Byte2; if(Temp & 0x8000) { // if temperature is negative Temp = Temp & 0x7FFF; sprinti(temp_a, "-%02u.%01u C ", Temp/10, Temp % 10); } else // otherwise (temperature is positive) sprinti(temp_a, " %02u.%01u C ", Temp/10, Temp % 10); if(humi >= 1000) // if humidity >= 100.0 rH% sprinti(humi_a, "%03u.%01u %%", humi/10, humi % 10); else sprinti(humi_a, " %02u.%01u %%", humi/10, humi % 10); } else { sensor_ok = 0; sprinti(temp_a, "Checksum"); sprinti(humi_a, " Error "); } } else { sensor_ok = 0; sprinti(temp_a, " Sensor "); sprinti(humi_a, " Error "); } // print sensor data on the display SSD1306_GotoXY(17, 13); SSD1306_Print(temp_a); SSD1306_GotoXY(17, 50); SSD1306_Print(humi_a); if(sensor_ok) { // print degree symblos ( ° ) SSD1306_Color = true; // set screen color SSD1306_DrawCircle(82, 15, 2); // put circle } SSD1306_Display(); delay_ms(1000); // wait a second } } // end of code. |
The following picture shows a protoboard circuit of the project:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.