Interfacing PIC18F46K22 with LM35 sensor and SSD1306 OLED display

This tutorial shows how to interface PIC18F46K22 microcontroller with SSD1306 OLED display and LM35 analog temperature sensor.
In this project the SSD1306 OLED display (128×64 pixel) is used to display environment temperature in degree Celsius, Kelvin and degree Fahrenheit.
CCS C Compiler is used in this project.

LM35 SSD1396 OLED PIC18F46K22 circuit

The LM35 temperature sensor is a three pin device (VCC, OUT and GND) with an output voltage linearly related to Centigrade temperature. Since the LM35 output varies with dependent to the temperature, we need an ADC (Analog-to-Digital Converter) module to measure this voltage. The PIC18F46K22 microcontroller has one ADC module with 10-bit resolution.

The LM35 output has linear +10mV/°C scale factor means the following:
If the output voltage =   10mV —> temperature =   1°C
If the output voltage = 100mV —> temperature = 10°C
If the output voltage = 200mV —> temperature = 20°C
If the output voltage = 370mV —> temperature = 37°C
and so on.

LM35 Futures (from datasheet):

  • Calibrated Directly in ° Celsius (Centigrade)
  • Linear + 10 mV/°C Scale Factor
  • 0.5°C Ensured Accuracy (at +25°C)
  • Rated for Full −55°C to +150°C Range
  • Suitable for Remote Applications
  • Low Cost Due to Wafer-Level Trimming
  • Operates from 4 to 30 V
  • Less than 60-μA Current Drain
  • Low Self-Heating, 0.08°C in Still Air
  • Nonlinearity Only ±¼°C Typical
  • Low Impedance Output, 0.1 Ω for 1 mA Load

The ADC module converts analog data into digital data. The PIC18F46K22 MCU has a 10-bit ADC module and a built-in fixed voltage reference (FVR) module which makes it a good choice for this application. With the fixed voltage reference we get a very good results.
Normally negative and positive references of the ADC module are VSS and VDD respectively, but VDD is not exactly equal to 5.00V, hence we should use the fixed voltage reference as a positive reference of the ADC module.

The PIC18F46K22 has 3 fixed voltage references: 1.024V, 2.048V and 4.096V. For example if we set the fixed voltage reference to 4.096V and the ADC module is configured so that the negative and the positive references are VSS and FVR respectively, in this case the equivalent 10-bit digital value of 4.096 is 1023 and 3.00V is 3.00 * 1023/4.096 = 749 , and so on.
In this project I used FVR = 1.024V because the LM35 output is generally less than 1V and also it gave me better result (let’s say higher resolution). Now the ADC module works in the interval between 0 and 1.024V.

To see how to interface PIC microcontroller with SSD1306 OLED display using CCS C compiler, visit this post:
Interfacing PIC18F4550 with SSD1306 OLED

Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • SSD1306 OLED display
  • LM35 temperature sensor   —->   datasheet
  • 5V source
  • Breadboard
  • Jumper wires

PIC18F46K22 MCU with SSD1306 OLED and LM35 sensor circuit:
The image below shows project circuit diagram.

The LM35 sensor has 3 pins (from left to right):
Pin 1 is power supply pin, connected to circuit +5V,
Pin 2: output pin,
Pin 3: GND (ground), connected to circuit ground.

The output pin of the LM35 sensor is connected to pin RA0 which is analog channel 0 (AN0).

SSD1306 OLED LM35 PIC18F46K22 microcontroller circuit

All the grounded terminals are connected together.

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. If the DC pin is connected to ground (GND) then the I2C slave address becomes 0x78.

In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 16 MHz, MCLR pin is configured as an input pin.

PIC18F46K22 MCU with SSD1306 OLED and LM35 sensor C 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 full name (with extension) 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.

Programming hints:
The internal fixed voltage reference (FVR) of the PIC18F46K22 microcontroller is set to 1.024V using the following line:

The ADC module is configured so that it uses its internal clock, voltage references (negative and positive) are set to VSS and FVR respectively where the FVR is the one previously set to 1.024V.
The LM35 output pin is connected to AN0 channel. All these configurations are done in the code as shown below:

PIC18F46K22 ADC module is used with 10-bit resolution which means the digital value of the input analog voltage varies between 0 (0V) and 1023 (1.024V).
The digital value represents the temperature in tenths °Celsius (output value of “274” equals 27.4 °Celsius).
The temperature in tenths degree Fahrenheit =  (tenth °Celsius) x 9/5 +320 (because: °F = °Cx9/5 + 32) and the temperature in tenths Kelvin = (tenth °Celsius) + 2732 (because: K = °C + 273.16).

To get the actual value of each quantity we’ve to divide it by 10. The line below shows an example for temperature in Kelvin:

We get the first 3 digits by dividing the tenths value by 10, and the tenths number (number after the decimal point) of the actual temperature value is equal to the reminder of that division (tenths value % 10).

The resolution of this thermometer is 0.1 °C.

CCS C Code:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.