PIC18F46K22 with LM335 sensor and SSD1306 OLED | mikroC Projects

This post shows how to build a temperature measurement station using PIC18F46K22 microcontroller and LM335 analog temperature sensor.
SSD1306 OLED display (128×64 pixel) is used to display environment temperature in degree Celsius, Kelvin and degree Fahrenheit.
MikroC PRO for PIC compiler is used in this project.

The LM335 sensor is a 3-pin analog device which can measure temperature (converts temperature to analog voltage). This sensor requires an ADC module to convert the analog voltage into digital data. The PIC18F46K22 microcontroller has one ADC module with 10-bit resolution.
ADC: Analog-to-Digital Converter.

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 microcontroller with LM335 sensor, visit the following post:
Interfacing PIC18F46K22 with LM335 sensor and 7-segment display

And to see how to interface PIC18F46K22 MCU with SSD1306 OLED display, take a look at the following project:
Interfacing PIC18F46K22 with SSD1306 OLED display | mikroC Projects

Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • SSD1306 OLED display
  • LM335 temperature sensor      —->   datasheet
  • 2.2k ohm resistor
  • 5V source
  • Breadboard
  • Jumper wires

PIC18F46K22 with LM335 and SSD1306 OLED circuit:
The image below shows project circuit diagram.

PIC18F46K22 LM335 sensor and SSD1306 OLED circuit

All the grounded terminals are connected together.

The LM335 sensor has 3 pins (from left to right):
Pin 1 for calibration, not used in this example.
Pin 2: output.
Pin 3: GND (ground).

The output pin of the LM335 sensor is connected to pin RA0 which is analog channel 0 (AN0). I chose the 2.2k ohm because as written in the datasheet for optimum accuracy the current flows through the LM335 should be 1mA. For example if the temperature = 27°C, the output will be 3.00V and assume the supply voltage is exactly 5.00V that means the current flows through the sensor is ( 5 – 3)/2.2 = 0.90mA which is good enough. Also the value 2.2k is a standard value and well used.

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 LM335 and SSD1306 OLED 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 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 set to 4.096V:

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 (4.096V). By multiplying the digital value by 4 we get the temperature in tenths Kelvin (4 = 1000*4.096/1024). The temperature in tenths degree Celsius = tenths Kelvin – 2732 (because: 1 K = 273.16 °C), and in tenths degree Fahrenheit =  tenth Kelvin x 9/5 – 4597 (because: 1 K = 459.7 °F).
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).

Full mikroC code:

LM335 PIC18F46K22 MCU and SSD1306 OLED 128x64 pixel display

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top