Previously I connected the PIC18F4550 microcontroller with SSD1306 OLED display and I was able to draw some shapes (lines, circles, triangles …) and write texts, next I build a simple relative humidity and temperature measurement station using the same microcontroller and display where the sensor used was DHT11. Now in this topic I am going to build the same project but instead of the DHT11 I’ll use DHT22 (AM2302, RHT03) sensor because it (DHT22) is better than the other (DHT11).
The SSD1306 OLED used in this post 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 PIC18F4550 with SSD1306 OLED
PIC18F4550 with SSD1306 OLED and DHT11 sensor
PIC18F4550 Interface with DHT22(AM2302) sensor
Hardware Required:
- PIC18F4550 microcontroller
- SSD1306 OLED display (128×64 Pixel)
- DHT22 temperature and humidity sensor — datasheet
- 4.7k ohm resistor
- 5V power source
- Breadboard
- Jumper wires
The Circuit:
Project circuit diagram is shown below.
(All grounded terminal are connected together)
There are 3 wires between the PIC18F4550 microcontroller and the SSD1306 OLED display: SDA (serial data), SCL (serial clock) and reset. SDA and SCL (SCK) pins of the display are connected respectively to pin RB0 (#33) and pin RB1 (#34) of the microcontroller, these pins are hardware I2C module SDA and SCL pins.
The reset pin (RES) of the display is connected to pin RB2 (#35) of the microcontroller.
The SSD1306 OLED display DC pin is connected to VDD which means the I2C slave address of the display is 0x7A. There is no need to use this pin if your display module doesn’t have a reset pin.
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 RB3 (#36). A pull-up resistor of 4.7k ohm is required for the data pin.
All the circuit is supplied with 5V including the SSD1306 OLED display.
In this project the PIC18F4550 runs with its internal oscillator and MCLR pin function is disabled.
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 SSD1306OLED.C, for more information about this driver, visit the following post:
SSD1306 OLED Library for CCS C compiler
It can be downloaded also from the link below:
SSD1306 OLED Library download
after the download, add the driver file to project folder or CCS C compiler drivers folder.
If there is a connection problem or checksum error the display will give “EEE.E°C” and “EEE.E %” instead of the temperature and humidity values as shown in the simulation video below.
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 | /****************************************************************************! PIC18F4550 microcontroller with SSD1306 OLED (128x64 Pixel) and DHT22 sensor C Code for CCS C compiler Internal oscillator used @ 8MHz http://simple-circuit.com/ *******************************************************************************/ // SSD1306 OLED reset pin definition #define SSD1306_RST PIN_B2 // DHT22 sensor connection #define DHT22_PIN PIN_B3 #include <18F4550.h> #device PASS_STRINGS = IN_RAM #fuses NOMCLR, INTRC_IO, NOWDT, NOPROTECT, NOLVP #use delay(clock = 8MHz) #use fast_io(B) #use I2C(MASTER, I2C1, FAST = 400000, stream = SSD1306_STREAM) // Initialize I2C // Include SSD1306 OLED driver source code #include <SSD1306OLED.c> char Temperature[] = " 00.0 C"; char Humidity[] = " 00.0 %"; int8 T_Byte1, T_Byte2, RH_Byte1, RH_Byte2, CheckSum; int16 Temp, RH; // 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_oscillator(OSC_8MHZ); // Set internal oscillator to 8MHz 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_Begin(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS); // Clear the buffer SSD1306_ClearDisplay(); SSD1306_Display(); SSD1306_DrawFastHLine(0, 31, SSD1306_LCDWIDTH); // Draw a horizontal line SSD1306_DrawText(10, 0, "DHT22 TEMPERATURE:"); SSD1306_DrawText(19, 37, "DHT22 HUMIDITY:"); SSD1306_Display(); 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 data on the display SSD1306_DrawText(24, 12, temperature, 2); // Print temperature with text size = 2 SSD1306_DrawCircle(88, 13, 2); // Put degree symbol ( ° ) SSD1306_DrawText(24, 49, humidity, 2); // Print humidity with text size = 2 SSD1306_Display(); delay_ms(1000); // Wait 1 second between readings } } // End of code. |
I made a simulation of the project using Proteus software, simulation file can be downloaded from the link below, use Proteus version 8.6 or higher to open it:
PIC18F4550 + SSD1306 + DHT22
simulation result is shown in this video: