This small topic shows how to connect PIC16F887 microcontroller with SSD1306 OLED display (128×64 pixel) and DHT11 relative humidity and temperature sensor. The OLED display is used to display the values of temperature and humidity that sensed by the DHT11 sensor.
The compiler used in this project is mikroElektronika mikroC PRO for PIC.
A small video shows Proteus simulation of this project at the end of the post.
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 and I2C mode) such as soldering, placing jumpers …
Related Projects:
The following topics may contain some useful information about the current project.
Interfacing PIC microcontroller with SSD1306 OLED | mikroC Projects
Interfacing PIC MCU with DHT11 sensor – mikroC Projects
Hardware Required:
- PIC16F887 microcontroller
- SSD1306 OLED display (128×64 Pixel)
- DHT11 temperature and humidity sensor
- 4.7k ohm resistor
- 5V power source
- Breadboard
- Jumper wires
Interfacing PIC16F887 MCU with SSD1306 OLED and DHT11 sensor circuit:
Project circuit diagram is shown below.
(All grounded terminals are connected together)
The PIC16F887 microcontroller has one hardware I2C module (MSSP 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.
In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.
Interfacing PIC16F887 MCU with SSD1306 OLED and DHT11 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 (& library) for the SSD1306 OLED display is required, it’s full name is SSD1306.C, download link is below:
SSD1306 OLED display driver for mikroC compiler
after the download, add the driver file (SSD1306.C) to project 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.
As shown in the circuit diagram above, the DHT11 data pin is connected to pin RD5, this connection is defined as follows:
1 2 3 | // DHT11 sensor connection (here data pin is connected to pin RD5) #define DHT11_PIN RD5_bit #define DHT11_PIN_DIR TRISD5_bit |
In this project I used Timer1 module to measure signal widths (logic 1 and logic 0 widths), it is configured to increment every 1 us (1 tick per microsecond).
The reset pin of the SSD1306 OLED display module is connected to pin RD4 of the microcontroller, this connection is defined as (there is no need for this connection if the display board has no rest pin):
1 2 3 | // SSD1306 OLED reset pin definition #define SSD1306_RST RD4_bit #define SSD1306_RST_DIR TRISD4_bit |
Full mikroC code:
Configuration words:
CONFIG1 = 0x2CD4
CONFIG2 = 0x0700
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 | /************************************************************************************** Interfacing PIC16F887 microcontroller with SSD1306 OLED (128x64 Pixel) and DHT11 sensor. C Code for mikroC PRO for PIC compiler Internal oscillator used @ 8MHz Configuration words: CONFIG1 = 0x2CD4 CONFIG2 = 0x0700 This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ // SSD1306 OLED reset pin definition #define SSD1306_RST RD4_bit #define SSD1306_RST_DIR TRISD4_bit // DHT11 sensor connection (here data pin is connected to pin RD5) #define DHT11_PIN RD5_bit #define DHT11_PIN_DIR TRISD5_bit #include <SSD1306.c> // include SSD1306 OLED driver source code char *temperature = "00.0 C"; char *humidity = "00.0 %"; unsigned short 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) { DHT11_PIN = 0; // connection pin output low DHT11_PIN_DIR = 0; // configure connection pin as output delay_ms(25); // wait 25 ms DHT11_PIN = 1; // connection pin output high delay_us(25); // wait 25 us DHT11_PIN_DIR = 1; // configure connection pin as input } // check sensor response unsigned short Check_Response() { TMR1H = TMR1L = 0; // reset Timer1 TMR1ON_bit = 1; // enable Timer1 module // wait until DHT11_PIN becomes high (checking of 80µs low time response) while(!DHT11_PIN && TMR1L < 100) ; if(TMR1L >= 100) // if response time >= 100µS ==> response error return 0; // return 0 (device has a problem with response) else { TMR1H = TMR1L = 0; // reset Timer1 // wait until DHT11_PIN becomes low (checking of 80µs high time response) while(DHT11_PIN && TMR1L < 100) ; if(TMR1L >= 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 unsigned short Read_Data(unsigned short* dht_data) { short i; *dht_data = 0; for(i = 0; i < 8; i++) { TMR1H = TMR1L = 0; // reset Timer1 while(!DHT11_PIN) // wait until DHT11_PIN becomes high { if(TMR1L > 100) // if low time > 100 ==> Time out error (Normally it takes 50µs) return 1; } TMR1H = TMR1L = 0; // reset Timer1 while(DHT11_PIN) // wait until DHT11_PIN becomes low if(TMR1L > 100) // if high time > 100 ==> Time out error (Normally it takes 26-28µs for 0 and 70µs for 1) return 1; // return 1 (timeout error) if(TMR1L > 50) // if high time > 50 ==> sensor sent 1 *dht_data |= (1 << (7 - i)); // set bit (7 - i) } return 0; // return 0 (data read OK) } // main function void main() { OSCCON = 0x70; // set internal oscillator to 8MHz T1CON = 0x10; // set Timer1 clock source to internal with 1:2 prescaler (Timer1 clock = 1MHz) TMR1H = TMR1L = 0; // reset Timer1 delay_ms(1000); // wait half a second I2C1_Init(400000); // initialize I2C communication with clock frequency of 400 KHz // 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_Print("DHT11 TEMPERATURE:"); SSD1306_GotoXY(5, 6); SSD1306_Print("DHT11 HUMIDITY:"); while(1) { 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 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] = '1'; 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 temperature SSD1306_GotoXY(9, 3); SSD1306_Print(temperature); SSD1306_GotoXY(13, 3); SSD1306_PutCustomC(degree); // degree symbol ( ° ) // Display relative humidity SSD1306_GotoXY(9, 8); SSD1306_Print(humidity); TMR1ON_bit = 0; // disable Timer1 module delay_ms(1000); // wait 1 second between readings } } // end of code. |
The simulation of this project using Proteus should be as shown in the following video where PIC16F877A microcontroller is used:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
could you send compiled HEX file for pic16f77
How to display decimal part?