Interfacing Arduino with DS18B20 Sensor and ST7789 TFT Display

This tutorial shows how to implement a simple digital thermometer using Arduino UNO board and DS18B20 temperature sensor.
The Arduino microcontroller (for example ATmega328P) reads temperature value from the DS18B20 sensor and prints it (in °C and °F) on ST7789 TFT display.

The ST7789 TFT module contains a display controller with the same name: ST7789. It’s a color display that uses SPI interface protocol and requires 3, 4 or 5 control pins, it’s low cost and easy to use.
The ST7789 TFT is an IPS display, it comes in different sizes (1.3″, 1.54″ …) but all of them should have the same resolution of 240×240 pixel (some may come with 320×240 pixel), this means it has 57600 pixels. This module works with 3.3V only and it doesn’t support 5V (not 5V tolerant).

TFT: Thin-Film Transistor.
SPI: Serial Peripheral Interface.
IPS: In-Plane Switching.
HVAC: Heating, Ventilation and Air Conditioning.

To see how to interface Arduino with ST7789 TFT display, visit this post:
Interfacing Arduino with ST7789 TFT Display – Graphics Test Example

About the DS18B20 sensor (from datasheet):
The DS18B20 digital thermometer from Maxim Integrated provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply. Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems.

Benefits and features:

  • Unique 1-Wire® interface requires only one port pin for communication,
  • Reduce component count with integrated temperature sensor and EEPROM,
  • Measures temperatures from -55°C to +125°C (-67°F to +257°F),
  • ±0.5°C Accuracy from -10°C to +85°C,
  • Programmable resolution from 9 bits to 12 bits,
  • No external components required,
  • Parasitic power mode requires only 2 pins for operation (DQ and GND),
  • Simplifies distributed temperature-sensing applications with multidrop capability,
  • Each device has a unique 64-bit serial code stored in on-board ROM,
  • Flexible user-definable nonvolatile (NV) alarm settings with alarm search command identifies devices with temperatures outside programmed limits,
  • Available in 8-pin SO (150 mils), 8-pin μSOP, and 3-pin TO-92 packages.

DS18B20 pin configuration

Hardware Required:

  • Arduino uno board
  • ST7789 TFT display module (1.3″, 1.54″ …)
  • DS18B20 temperature sensor    —->   datasheet
  • 4 x 3.3k ohm resistor (+1 if the display module has CS pin)
  • 4 x 2.2k ohm resistor (+1 if the display module has CS pin)
  • 4.7k ohm resistor
  • Breadboard
  • Jumper wires

Arduino with DS18B20 sensor and ST7789 TFT circuit:
The image below shows project circuit diagram.

The DS18B20 sensor has 3 pins (from right to left): VCC (or VDD), data (DQ) and GND where:
VCC (VDD): sensor power supply pin, connected to Arduino 5V pin,
data (DQ) 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.

ST7789 TFT DS18B20 Arduino thermometer circuit

The ST7789 display module shown in project circuit diagram has 7 pins: (from right to left): GND (ground), VCC, SCL (serial clock), SDA (serial data), RES (reset), DC (or D/C: data/command) and BLK (back light).
Connecting the BLK pin is optional. The back light turns off when the BLK pin connected to the ground (GND).

As mentioned above, the ST7789 TFT display controller works with 3.3V only (power supply and control lines). The display module is supplied with 3.3V (between VCC and GND) which comes from the Arduino board.

All Arduino UNO board output pins are 5V, connecting a 5V pin to the ST7789 TFT display may damage its controller.
To connect the Arduino to the display module, I used voltage divider for each line which means there are 4 voltage dividers. Each voltage divider consists of 2.2k and 3.3k resistors, this drops the 5V into 3V which is sufficient.

If the display module has a CS pin (Chip Select) then it should be connected to Arduino digital pin 10 through another voltage divider.

So, the ST7789 TFT display is connected to the Arduino board as follows (each one through voltage divider):
RST pin is connected to Arduino digital pin 8,
DC pin is connected to Arduino digital pin 9,
SDA pin is connected to Arduino digital pin 11,
SCL pin is connected to Arduino digital pin 13.

Other pins are connected as follows:
VCC pin is connected to Arduino 3V3 pin,
GND pin is connected to Arduino GND pin,
BL (LED) pin is connected to Arduino 3V3 pin (optional).

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

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

The 2 libraries can be installed manually, first download them from the following 2 links:
Adafruit ST7789 TFT library    —->  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 other library file.

Hints:
The libraries are included in the main code as follows:

The ST7789 TFT module pins (CS, RST and DC) connections are defined as shown below (even the display module has no CS pin but its definition is required by the Adafruit ST7789 library):

The other display pins (SDA and SCL) are connected to Arduino hardware SPI module pins (digital pin 11 and digital pin 13 respectively for MOSI and SCLK).

The Adafruit ST7789 library is initialized with this line:

And the TFT display is initialized using the following command:

The DS18B20 sensor data pin is connected to Arduino analog pin 0 (A0), it’s defined in the code as:

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 variable raw_temp_value, is signed 2-byte number which is actual temperature value (in °C) multiplied by 16 (why 16? —> because we’re working with 12-bit resolution).
For example if the returned valued equals to 209 means actual temperature is equal to 209/16 = 13.0625 °C.

To get the value of temperature in degrees Fahrenheit (°F), I used the function below:

This function is just from the common one: °F = °C x 9/5 + 32.
Since the temperature in °C is multiplied by 16 the 32 also needs to be multiplied by 16.
To get the °F temperature with no floating number, I multiplied every thing by 10.
This function gives the actual value of the temperature in °F multiplied by 160 (actual °F = f_temp/160).

Since the DS18B20 sensor is used with 12-bit resolution, we get the temperature in °C as shown below (example):

Again, why 16 and 625? —> Because we are working with 12-bit resolution (increment step = 0.0625).

The resolution of this thermometer is 0.0625°C (0.1125°F).

If the Arduino can’t connect the DS18B20 sensor due to for example, bad connection, the display will show “Error”.

Full Arduino code:

Arduino with ST7789 TFT and DS18B20 sensor


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

1 thought on “Interfacing Arduino with DS18B20 Sensor and ST7789 TFT Display”

  1. Joseph Sullivan

    Thank you a billion for your code. I have a different TFT, an ILI9163C. It doesn’t work with the Onewire/Dallastemperature libraries, as long as the TFT is initialized, the libraries fail to read the sensor.

    I scoured the internet. I tried EVERY DS18B20 library there was. I tried editing the TFT library. I tried using its sleep modes. I tried changing GPIO pins, resistors. Nothing worked. -127.000, every single time.

    Until I found your code, on this page. It was the only one that worked, and also the only one that didn’t use any library, not even Onewire.h.

    I improved it – edited the readbyte command from i<8 to i<12 to read the full 12bit resolution:

    uint16_t ds18b20_read_byte(void)
    {
    byte i;
    uint16_t value=0;
    for(i = 0; i < 12 ; i++)
    bitWrite(value, i, ds18b20_read_bit());
    //terminal.print(value);
    return value;
    }

    And now I can display it on my TFT without issue. Thank you so much for your code.

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