This post shows how to implement a simple temperature measurement station using PIC18F46K22 microcontroller and DS18B20 digital temperature sensor.
The PIC18F46K22 MCU reads temperature (in °C) from the DS18B20 sensor and print its value 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
- DS18B20 temperature sensor —-> datasheet
- 4.7k ohm resistor
- 5V source
- Breadboard
- Jumper wires
PIC18F46K22 with SSD1306 OLED and DS18B20 sensor circuit:
The image below shows project circuit diagram.
All the grounded terminals are connected together.
The DS18B20 temperature sensor has 3 pins (from left to right): GND, data and VCC where:
GND is connected to circuit ground (0V)
Data pin is connected to pin RD5 (#28) of the PIC18F46K22
VCC is connected to +5V.
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 DS18B20 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 reset pin of the SSD1306 OLED display is defined in the C code as shown below:
1 2 3 | // SSD1306 OLED reset pin definition (if available) #define SSD1306_RST RD4_bit #define SSD1306_RST_DIR TRISD4_bit |
Function used in the C code:
unsigned int ds18b20_read(): reads temperature from the DS18B20 sensor, returns the value of the temperature as unsigned 16-bit number.
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 | /************************************************************************************** Interfacing PIC18F46K22 microcontroller with SSD1306 OLED (128x64 Pixel) and DS18B20 temperature 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/ ***************************************************************************************/ // 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 // read raw temperature from DS18B20 sensor unsigned int ds18b20_read() { unsigned int _temp; Ow_Reset(&PORTD, 5); // onewire reset signal Ow_Write(&PORTD, 5, 0xCC); // issue command SKIP_ROM Ow_Write(&PORTD, 5, 0x44); // issue command CONVERT_T while (Ow_Read(&PORTD, 5) == 0) ; Ow_Reset(&PORTD, 5); // onewire reset signal Ow_Write(&PORTD, 5, 0xCC); // issue command SKIP_ROM Ow_Write(&PORTD, 5, 0xBE); // issue command READ_SCRATCHPAD _temp = Ow_Read(&PORTD, 5); // read temperature LSB byte _temp |= (Ow_Read(&PORTD, 5) << 8); // read temperature MSB byte return _temp; } // 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 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(0, 0); SSD1306_Print("PIC18F46K22 + DS18B20"); SSD1306_GotoXY(28, 14); SSD1306_Print("SSD1306 OLED"); SSD1306_GotoXY(29, 33); SSD1306_Print("TEMPERATURE:"); SSD1306_Display(); SSD1306_TextSize(2); // text size = 2 while(1) { unsigned int ds18b20_temp; char buffer[11]; ds18b20_temp = ds18b20_read(); // read temperature from DS18B20 sensor if (ds18b20_temp & 0x8000) // if temperature < 0 { ds18b20_temp = ~ds18b20_temp + 1; // change temperature value to positive form sprinti(buffer, "-%02u.%04u C", (ds18b20_temp/16) % 100, (ds18b20_temp & 0x0F) * 625); } else { // otherwise (temperature >= 0) if (ds18b20_temp/16 > 100) // if temperature >= 100 °C sprinti(buffer, "%03u.%04u C", ds18b20_temp/16, (ds18b20_temp & 0x0F) * 625); else // otherwise ( 0 <= temperature < 100) sprinti(buffer, " %02u.%04u C", ds18b20_temp/16, (ds18b20_temp & 0x0F) * 625); } SSD1306_GotoXY(1, 50); SSD1306_Print(buffer); // print degree symblos ( ° ) SSD1306_Color = true; // set screen color SSD1306_DrawCircle(103, 53, 2); // draw 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.