This PIC16F877A project shows how build a small weather station for measuring and displaying temperature and relative humidity. The PIC16F877A microcontroller reads the values of the temperature and humidity from DHT11 sensor and display the results on SSD1306 OLED display (128×64 Pixel).
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 DHT11 sensor with PIC16F877A – CCS C compiler
Hardware Required:
- PIC16F877A microcontroller
- SSD1306 OLED display (128×64 Pixel)
- DHT11 temperature and humidity sensor
- 8MHz crystal oscillator
- 2 x 22pF ceramic capacitor
- 10k ohm resistor
- 4.7k ohm resistor
- 5V power source
- Breadboard
- Jumper wires
PIC16F877A with DHT11 sensor and SSD1306 OLED circuit:
The following image shows project circuit schematic diagram (hardware circuit).
(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 DHT11 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 | /****************************************************************************** PIC16F877A microcontroller with SSD1306 OLED (128x64 Pixel) and DHT11 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 // DHT11 sensor connection #define DHT11_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; char degree[] = {0, 7, 5, 7, 0}; // degree symbol char // Send start signal to the sensor void Start_Signal(void) { output_drive(DHT11_PIN); // Configure connection pin as output output_low(DHT11_PIN); // Connection pin output low delay_ms(25); // Wait 25 ms output_high(DHT11_PIN); // Connection pin output high delay_us(30); // Wait 30 us output_float(DHT11_PIN); // Configure connection pin as input } // Check sensor response int1 Check_Response(void) { set_timer1(0); // Set Timer1 value to 0 while(!input(DHT11_PIN) && get_timer1() < 100); // Wait until DHT11_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(DHT11_PIN) && get_timer1() < 100); // Wait until DHT11_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(DHT11_PIN)) // Wait until DHT11_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(DHT11_PIN)) // Wait until DHT11_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("DHT11 TEMPERATURE:"); SSD1306_GotoXY(5, 6); SSD1306_PutC("DHT11 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 DHT11 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)) { temperature[0] = T_Byte1 / 10 + '0'; temperature[1] = T_Byte1 % 10 + '0'; temperature[3] = T_Byte2 + '0'; humidity[0] = RH_Byte1 / 10 + '0'; humidity[1] = RH_Byte1 % 10 + '0'; humidity[3] = RH_Byte2 + '0'; } // Checksum error else { temperature[0] = temperature[1] = temperature[3] = 'E'; humidity[0] = humidity[1] = humidity[3] = 'E'; } } // Sensor response error (connection error) else { temperature[0] = temperature[1] = temperature[3] = 'E'; humidity[0] = humidity[1] = humidity[3] = 'E'; } // Display the temperature SSD1306_GotoXY(9, 3); printf(SSD1306_PutC, temperature); SSD1306_GotoXY(13, 3); SSD1306_PutCustomC(degree); // degree symbol ( ° ) // Display the relative humidity SSD1306_GotoXY(9, 8); printf(SSD1306_PutC, humidity); delay_ms(1000); // Wait 1 second between readings } } // End of code. |
PIC16F877A + SSD1306 OLED + DHT11 sensor Proteus simulation:
Proteus simulation file download link is below, use it with version 8.6 or higher:
PIC16F877A + SSD1306 OLED + DHT11
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.
I am puzzled as how do you determine the actual location for the OLED display, below is your codes for displaying of
“DHT11 TEMPERATURE:” and “DHT11 HUMIDITY:” at Location XY(3, 1) and XY(5, 6) respectively:
// clear the whole display
SSD1306_ClearDisplay();
SSD1306_GotoXY(3, 1);
SSD1306_PutC(“DHT11 TEMPERATURE:”);
SSD1306_GotoXY(5, 6);
SSD1306_PutC(“DHT11 HUMIDITY:”);
Based on 128 x 64 resolution of SSD1306 OLED Display module, the display location of “DHT11 HUMIDITY:” line should be XY(20, 40) as shown in the picture of your website.
How do you determine the actual location for displaying of the message based on the 128 x 64 resolution?
Thanks,
Fu DF.