Interfacing ESP8266 NodeMCU with DS18B20 Sensor and ST7789 TFT

This tutorial shows how to implement a simple digital thermometer using ESP8266 NodeMCU board (ESP-12E) and DS18B20 digital temperature sensor.
In this project the NodeMCU board reads temperature values from the DS18B20 sensor and prints them (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. This display 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, this means it has 57600 pixels. This module works with 3.3V only and it does not support 5V.

TFT: Thin-Film Transistor.
SPI: Serial Peripheral Interface.
IPS: In-Plane Switching.

To see how to interface the ESP8266 NodeMCU board with ST7789 TFT display, visit this post:
Interfacing ESP8266 NodeMCU with ST7789 TFT Display

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:

  • ESP8266 NodeMCU board
  • ST7789 TFT display module
  • DS18B20 temperature sensor    —->   datasheet
  • 4.7k ohms resistor
  • Micro USB cable (for programming and powering the whole circuit)
  • Breadboard
  • Jumper wires

ESP8266 NodeMCU with DS18B20 sensor and ST7789 TFT circuit:
Project circuit schematic diagram is shown below.

The DS18B20 sensor has 3 pins (from right to left), it is connected to the ESP8266 NodeMCU board as follows:
VCC (VDD): sensor power supply pin, connected to NodeMCU 3V3 pin,
data pin: connected to NodeMCU pin D2,
GND: connected to NodeMCU GND pin.

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

NodeMCU ESP8266 DS18B20 sensor and ST7789 display

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).

The ST7789 TFT display module is connected to the NodeMCU board as follows:
GND is connected to pin GND of the NodeMCU board,
VCC and BL are connected to pin 3V3,
SCL pin is connected to D5 (ESP8266EX GPIO14),
SDA pin is connected to D7 (ESP8266EX GPIO13),
RES pin is connected to D4 (ESP8266EX GPIO2),
DC pin is connected to D3 (ESP8266EX GPIO0).

If the display module has a CS pin (Chip Select) then it should be connected to NodeMCU pin D8 (GPIO15).

Connecting the BLK pin is optional. The back light turns off when the BLK pin connected to the ground (GND).

Pins D5 (GPIO14) and D7 (GPIO13) are hardware SPI module pins of the ESP8266EX microcontroller respectively for SCK (serial clock) and MOSI (master-out slave-in).

ESP8266 NodeMCU with DS18B20 sensor and ST7789 TFT code:
The following Arduino code requires two 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 two libraries can be installed manually, first download them from the following 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 the 2nd library file.

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

The connection of ST7789 TFT display with the NodeMCU is as shown below where the display is connected to hardware SPI module of the NodeMCU (pins: SCK and MOSI):

And the TFT display is initialized using the following command:

The DS18B20 sensor data pin is connected to ESP8266 NodeMCU board pin D5, 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 NodeMCU can’t connect the DS18B20 sensor due to for example, bad connection, the display will show “Error”.

Full Arduino code:

The picture below shows my protoboad circuit:

NodeMCU with ST7789 TFT and DS18B20 sensor


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

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