This small post shows how to connect PIC16F877A microcontroller with SSD1306 OLED display and DHT22 (AM2302, RHT03) digital humidity and temperature sensor. The measured values of the relative humidity and temperature are displayed on the SSD1306 OLED screen.
The SSD1306 OLED used in this project is configured to work in I2C mode, some SSD1306 OLED boards may require a small hardware modifications (to select between SPI mode or I2C mode) such as soldering, placing jumpers …
Related Projects:
The following topics may contain some useful information about the current project.
Interfacing PIC16F877A with SSD1306 OLED display
Interfacing PIC16F877A with DHT22 sensor
PIC16F877A with DHT11 sensor and SSD1306 OLED
Hardware Required:
- PIC16F877A microcontroller
- SSD1306 OLED display (128×64 Pixel)
- DHT22 sensor (or RHT03, AM2302)
- 8 MHz crystal oscillator
- 2 x 22pF ceramic capacitor
- 10k ohm resistor
- 4.7k ohm resistor
- 5V power source
- Breadboard
- Jumper wires
The Circuit:
The following image shows project circuit schematic diagram.
(All grounded terminals are connected together)
The PIC16F877A microcontroller has one hardware I2C module with SDA on pin RC4 (#23) and SCL on pin RC3 (#18). The SDA pin of the MCU is connected to the SDA pin of the display and the SCL 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 the I2C slave address of the display is 0x7A.
The DHT22 sensor has 4 pins (from left to right): VCC (+5V), data pin, NC (not connected pin) and GND. The data pin is connected to pin RD5 (#28). A pull-up resistor of 4.7k ohm is required for the data pin.
The Code:
The C code below is for CCS C compiler, it was tested with version 5.051.
To be able to compile the C code below with no error, a driver for the SSD1306 OLED display is required, it’s name is SSD1306.C, its download link is below:
SSD1306 OLED display driver
after the download, add the driver file to the project folder or CCS C compiler drivers folder.
Functions used in the code:
Start_Signal(): this function sends the start signal to the sensor, it sends a logic low for 25 ms and a logic high for 30 us.
Check_Response(): this function checks if there is a response from the sensor (after sending the start signal using the previous function), returns 1 (true) if ok and 0 (false) if error (for example a connection problem).
Read_Data(*dht_data): this function reads temperature and humidity data from the sensor (4 bytes), it also reads an other byte (5th byte) which is used as a checksum. This function returns 0 (false) if data read was ok and 1 (true) if there was a time out problem.
CCS C 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 | /************************************************************************************** PIC16F877A microcontroller with SSD1306 OLED (128x64 Pixel) and DHT22 (AM2302) sensor C Code for CCS C compiler Crystal oscillator used @ 8MHz http://simple-circuit.com/ ***************************************************************************************/ // SSD1306 OLED reset pin definition #define SSD1306_RST PIN_D4 // DHT22 sensor connection #define DHT22_PIN PIN_D5 #include <16F877A.h> #fuses HS, NOWDT, NOPROTECT, NOLVP #use delay(clock = 8MHz) #use fast_io(D) #use I2C(MASTER, I2C1, FAST = 400000, stream = SSD1306_STREAM) // initialize I2C // include SSD1306 OLED driver source code #include <SSD1306.c> char Temperature[] = " 00.0 C"; char Humidity[] = " 00.0 %"; int8 T_Byte1, T_Byte2, RH_Byte1, RH_Byte2, CheckSum; int16 Temp, RH; char degree[] = {0, 7, 5, 7, 0}; // degree symbol custom char // Send start signal to the sensor void Start_Signal(void) { output_drive(DHT22_PIN); // Configure connection pin as output output_low(DHT22_PIN); // Connection pin output low delay_ms(25); // Wait 25 ms output_high(DHT22_PIN); // Connection pin output high delay_us(30); // Wait 30 us output_float(DHT22_PIN); // Configure connection pin as input } // Check sensor response int1 Check_Response(void) { set_timer1(0); // Set Timer1 value to 0 while(!input(DHT22_PIN) && get_timer1() < 100); // Wait until DHT22_PIN becomes high (cheking of 80µs low time response) if(get_timer1() >= 100) // If response time >= 100µS ==> Response error return 0; // Return 0 (Device has a problem with response) else { set_timer1(0); // Set Timer1 value to 0 while(input(DHT22_PIN) && get_timer1() < 100); // Wait until DHT22_PIN becomes low (cheking of 80µs high time response) if(get_timer1() >= 100) // If response time >= 100µS ==> Response error return 0; // Return 0 (Device has a problem with response) else return 1; // Return 1 (response OK) } } // Data read function int1 Read_Data(int8 *dht_data) { int8 j; *dht_data = 0; for(j = 0; j < 8; j++){ set_timer1(0); // Reset Timer1 while(!input(DHT22_PIN)) // Wait until DHT22_PIN becomes high if(get_timer1() >= 100) { // If low time >= 100µs ==> Time out error (Normally it takes 50µs) return 1; } set_timer1(0); // Reset Timer1 while(input(DHT22_PIN)) // Wait until DHT22_PIN becomes low if(get_timer1() > 100) { // If high time > 100µs ==> Time out error (Normally it takes 26-28µs for 0 and 70µs for 1) return 1; // Return 1 (timeout error) } if(get_timer1() > 50) // If high time > 50µS ==> Sensor sent 1 bit_set(*dht_data, (7 - j)); // Set bit (7 - j) } return 0; // Return 0 (data read OK) } // main function void main(void) { setup_timer_1(T1_INTERNAL | T1_DIV_BY_2); // Start Timer1 with internal clock source + 2 prescaler delay_ms(1000); // Initialize the SSD1306 OLED with an I2C addr = 0x7A (default address) SSD1306_Init(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS); // clear the whole display SSD1306_ClearDisplay(); SSD1306_GotoXY(3, 1); SSD1306_PutC("DHT22 TEMPERATURE:"); SSD1306_GotoXY(5, 6); SSD1306_PutC("DHT22 HUMIDITY:"); while(TRUE) { Start_Signal(); // Send a start signal to the sensor if(Check_Response()) { // Check if there is a response from sensor (If OK start reding humidity and temperature data) // Response OK ==> read (and save) data from the DHT22 sensor and check time out errors Read_Data(&RH_Byte1); // Read humidity 1st byte and store its value in the variable RH_Byte1 Read_Data(&RH_Byte2); // Read humidity 2nd byte and store its value in the variable RH_Byte2 Read_Data(&T_Byte1); // Read temperature 1st byte and store its value in the variable T_Byte1 Read_Data(&T_Byte2); // Read temperature 2nd byte and store its value in the variable T_Byte2 Read_Data(&CheckSum); // Read checksum and store its value in the variable CheckSum // Test if all data were sent correctly 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( RH >= 1000) Humidity[0] = '1'; else Humidity[0] = ' '; if(Temp > 0x8000) { Temperature[0] = '-'; Temp = Temp & 0x7FFF; } else Temperature[0] = ' '; Temperature[1] = (Temp / 100) % 10 + 48; Temperature[2] = (Temp / 10) % 10 + 48; Temperature[4] = Temp % 10 + 48; Humidity[1] = (RH / 100) % 10 + 48; Humidity[2] = (RH / 10) % 10 + 48; Humidity[4] = RH % 10 + 48; } // Checksum error else { Temperature[0] = Temperature[1] = Temperature[2] = Temperature[4] = 'E'; Humidity[0] = Humidity[1] = Humidity[2] = Humidity[4] = 'E'; } } // Sensor response error (connection error) else { Temperature[0] = Temperature[1] = Temperature[2] = Temperature[4] = 'E'; Humidity[0] = Humidity[1] = Humidity[2] = Humidity[4] = 'E'; } // Display the temperature SSD1306_GotoXY(8, 3); printf(SSD1306_PutC, Temperature); SSD1306_GotoXY(13, 3); SSD1306_PutCustomC(degree); // degree symbol ( ° ) // Display the relative humidity SSD1306_GotoXY(8, 8); printf(SSD1306_PutC, Humidity); delay_ms(1000); // Wait 1 second between readings } } // End of code. |
PIC16F877A + SSD1306 OLED + DHT22 sensor Proteus simulation:
Proteus simulation file download link is below, use it with version 8.6 or higher:
PIC16F877A + SSD1306 OLED + DHT22
The following video shows my simulation result (simulation circuit is not the same as the hardware circuit, hardware circuit diagram is shown above!):
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.