Arduino with DS18B20 sensor and SSD1306 OLED display

This post shows how to implement a simple temperature measurement station using Arduino UNO board and DS18B20 digital temperature sensor.
The Arduino reads temperature (in °C) from the DS18B20 sensor and print its value on SSD1306 OLED display (128×64 pixel).

The DS18B20 temperature sensor is a 3-pin electronic component (like a simple transistor) from Maxim (formerly Dallas) which uses 1-wire protocol to communicate with master device (microprocessor, microcontroller ….). Each DS18B20 device has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-wire bus and controlled with one master device.
The DS18B20 sensor provides 9-bit to 12-bit Celsius temperature measurement resolution (programmable resolution).

To see how to interface Arduino with SSD1306 OLED display, visit the following post:
Interfacing Arduino with SSD1306 OLED display

And to see how to interface Arduino with DS18B20 sensor for the first time, take a look at this post:
Digital thermometer using Arduino and DS18B20 sensor

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.

Hardware Required:

  • Arduino board
  • SSD1306 OLED display
  • DS18B20 temperature sensor    —->   datasheet
  • 4.7k ohm resistor
  • Breadboard
  • Jumper wires

Arduino with DS18B20 sensor and SSD1306 OLED display circuit:
The image below shows project circuit diagram.

The DS18B20 sensor has 3 pins (from right to left): VCC (or VDD), data and GND where:
VCC (VDD): sensor power supply pin, connected to Arduino 5V pin,
data pin: connected to Arduino analog pin 0 (A0) and
GND: connected to Arduino GND pin.

A pull-up resistor of 4.7k ohm is required because the DS18B20 output is open drain.

Arduino DS18B20 SSD1306 OLED temperature station circuit

The SSD1306 OLED display is connected to the Arduino UNO board as follows:
SSD1306 OLED GND goes to Arduino GND (ground),
SSD1306 OLED VDD to Arduino 5V,
SSD1306 OLED SDA pin (serial data) to pin A4 (Arduino UNO hardware I2C SDA pin),
SSD1306 OLED SCK pin (serial clock) to pin A5 (Arduino UNO hardware I2C SCL pin),
SSD1306 OLED RES pin (reset) to Arduino pin 4.

The SSD1306 OLED display DC pin is connected to VDD which means I2C slave address of the device is 0x3D. If the DC pin is connected to ground (GND) then the I2C slave address will be 0x3C.

Arduino with DS18B20 sensor and SSD1306 OLED display code:
The Arduino code below doesn’t use any library for the DS18B20 sensor.

The following Arduino code requires 2 libraries from Adafruit Industries:
The first library is a driver for the SSD1306 OLED display which can be installed from Arduino IDE library manager (Sketch —> Include Library —> Manage Libraries …, in the search box write “ssd1306” and install the one from Adafruit).

The second library is Adafruit graphics library which can be installed also from Arduino IDE library manager.

The previous 2 libraries can also be installed manually, download links are below:
Adafruit SSD1306 OLED driver     —->  direct link
Adafruit graphics library             —->  direct link

After the download, go to Arduino IDE —> Sketch —> Include Library —> Add .ZIP Library … and browse for the .zip file (previously downloaded).
The same thing for the other library file.

In the code there are total of 3 libraries, they’re included in the code as follows:

The SSD1306 OLED display reset pin and DS18B20 temperature sensor pin connections are defined in the code as shown below:

Functions used in the code:
bool ds18b20_start(): used to know if the DS18B20 sensor is correctly connected to the circuit, returns 1 if OK and 0 if error.
ds18b20_write_bit(bool value): writes (sends) 1 bit to the DS18B20 sensor, the bit is ‘value‘ which may be 1 or 0.
ds18b20_write_byte(byte value): writes 1 byte (8 bits) to the DS18B20 sensor, this function is based on the previous function. This function writes LSB first.
bool ds18b20_read_bit(void): reads 1 bit from the DS18B20 sensor, returns the read value (1 or 0).
byte ds18b20_read_byte(void): reads 1 byte from the DS18B20 sensor, this function is based on the previous function. This function reads LSB first.
bool ds18b20_read(int *raw_temp_value): reads the temperature raw data which is 16-bit long (two 8-bit registers), the data is stored in the variable raw_temp_value, returns 1 if OK and 0 if error.

The value of the temperature in degree Celsius is equal to the raw value divided by 16 (in case of 12-bit resolution). The default resolution of the DS18B20 is 12 bits.

Full Arduino code:

The following picture shows a protoboard circuit of the project:

Arduino UNO with SSD1306 OLED display and DS18B20 temperature sensor

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