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.
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.
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:
1 2 | #include <Adafruit_GFX.h> // Core graphics library #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789 |
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):
1 2 3 4 5 6 7 8 | // ST7789 TFT module connections #define TFT_DC D3 // TFT DC pin is connected to NodeMCU pin D3 (GPIO0) #define TFT_RST D4 // TFT RST pin is connected to NodeMCU pin D4 (GPIO2) #define TFT_CS D8 // TFT CS pin is connected to NodeMCU pin D8 (GPIO15) // initialize ST7789 TFT library with hardware SPI module // SCK (CLK) ---> NodeMCU pin D5 (GPIO14) // MOSI(DIN) ---> NodeMCU pin D7 (GPIO13) Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); |
And the TFT display is initialized using the following command:
1 2 | // if the display has CS pin try with SPI_MODE0 tft.init(240, 240, SPI_MODE2); // init ST7789 display 240x240 pixel |
The DS18B20 sensor data pin is connected to ESP8266 NodeMCU board pin D5, it’s defined in the code as:
1 2 | // define DS18B20 data pin #define DS18B20_PIN D2 // DS18B20 data pin is connected to NodeMCU pin D2 (GPIO4) |
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:
1 | int32_t f_temp = (int32_t)c_temp * 90/5 + 5120; // 5120 = 32 x 16 x 10 |
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):
1 | tft.printf(" %02u.%04u", c_temp/16, (c_temp & 0x0F) * 625); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | /************************************************************************ * * ESP8266 NodeMCU Interface with ST7789 TFT display and DS18B20 digital * temperature sensor. * This is a free software with NO WARRANTY. * http://simple-circuit.com/ * ************************************************************************/ #include <Adafruit_GFX.h> // Adafruit core graphics library #include <Adafruit_ST7789.h> // Adafruit hardware-specific library for ST7789 // ST7789 TFT module connections #define TFT_DC D3 // TFT DC pin is connected to NodeMCU pin D3 (GPIO0) #define TFT_RST D4 // TFT RST pin is connected to NodeMCU pin D4 (GPIO2) #define TFT_CS D8 // TFT CS pin is connected to NodeMCU pin D8 (GPIO15) // initialize ST7789 TFT library with hardware SPI module // SCK (CLK) ---> NodeMCU pin D5 (GPIO14) // MOSI(DIN) ---> NodeMCU pin D7 (GPIO13) Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); // define DS18B20 data pin #define DS18B20_PIN D2 // DS18B20 data pin is connected to NodeMCU pin D2 (GPIO4) void setup(void) { // initialize the ST7789 display (240x240 pixel) // if the display has CS pin try with SPI_MODE0 tft.init(240, 240, SPI_MODE2); // if the screen is flipped, remove this command tft.setRotation(2); // fill the screen with black color tft.fillScreen(ST77XX_BLACK); tft.setTextWrap(false); // turn off text wrap option tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK); // set text color to green and black background tft.setTextSize(3); // text size = 3 tft.setCursor(17, 41); // move cursor to position (15, 27) pixel tft.print("TEMPERATURE:"); tft.setTextSize(4); // text size = 4 } // main loop void loop() { delay(1000); // wait a second // get temperature in °C ( actual temperature in °C = c_temp/16) int c_temp; if( ds18b20_read( &c_temp ) ) { // read from DS18B20 sensor OK // calculate temperature in °F (actual temperature in °F = f_temp/160) // °F = °C x 9/5 + 32 int32_t f_temp = (int32_t)c_temp * 90/5 + 5120; // 5120 = 32 x 16 x 10 // print temperature in °C tft.setTextColor(ST77XX_RED, ST77XX_BLACK); // set text color to red and black background tft.setCursor(0, 102); if(c_temp < 0) { // if temperature < 0 °C c_temp = abs(c_temp); // absolute value tft.printf("-%02u.%04u", c_temp/16, (c_temp & 0x0F) * 625); } else { if (c_temp/16 >= 100) // if temperature >= 100.0 °C tft.printf("%03u.%04u", c_temp/16, (c_temp & 0x0F) * 625); else tft.printf(" %02u.%04u", c_temp/16, (c_temp & 0x0F) * 625); } // print "°C" tft.drawCircle(201, 108, 4, ST77XX_RED); // print degree symbol ( ° ) tft.drawCircle(201, 108, 5, ST77XX_RED); tft.setCursor(210, 102); tft.print("C"); // print temperature in °F tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK); // set text color to yellow and black background tft.setCursor(0, 170); if(f_temp < 0) { // if temperature < 0 °F f_temp = abs(f_temp); // absolute value tft.printf("-%02u.%04u", (uint16_t)f_temp/160, (uint16_t)(f_temp*1000/16 % 10000)); } else { if (f_temp/160 >= 100) // if temperature >= 100.0 °F tft.printf("%03u.%04u", (uint16_t)f_temp/160, (uint16_t)(f_temp*1000/16 % 10000)); else tft.printf(" %02u.%04u", (uint16_t)f_temp/160, (uint16_t)(f_temp*1000/16 % 10000)); } // print "°F" tft.drawCircle(201, 176, 4, ST77XX_YELLOW); // print degree symbol ( ° ) tft.drawCircle(201, 176, 5, ST77XX_YELLOW); tft.setCursor(210, 170); tft.print("F"); } else { // read from DS18B20 sensor ERROR! tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); // set text color to red and black background tft.setCursor(0, 102); tft.print(" Error "); tft.fillRect(0, 170, tft.width(), 28, ST77XX_BLACK); } } bool ds18b20_start() { bool ret = 0; digitalWrite(DS18B20_PIN, LOW); // send reset pulse to the DS18B20 sensor pinMode(DS18B20_PIN, OUTPUT); delayMicroseconds(500); // wait 500 us pinMode(DS18B20_PIN, INPUT); delayMicroseconds(100); // wait to read the DS18B20 sensor response if (!digitalRead(DS18B20_PIN)) { ret = 1; // DS18B20 sensor is present delayMicroseconds(400); // wait 400 us } return(ret); } void ds18b20_write_bit(bool value) { digitalWrite(DS18B20_PIN, LOW); pinMode(DS18B20_PIN, OUTPUT); delayMicroseconds(2); digitalWrite(DS18B20_PIN, value); delayMicroseconds(80); pinMode(DS18B20_PIN, INPUT); delayMicroseconds(2); } void ds18b20_write_byte(byte value) { byte i; for(i = 0; i < 8; i++) ds18b20_write_bit(bitRead(value, i)); } bool ds18b20_read_bit(void) { bool value; digitalWrite(DS18B20_PIN, LOW); pinMode(DS18B20_PIN, OUTPUT); delayMicroseconds(2); pinMode(DS18B20_PIN, INPUT); delayMicroseconds(5); value = digitalRead(DS18B20_PIN); delayMicroseconds(100); return value; } byte ds18b20_read_byte(void) { byte i, value; for(i = 0; i < 8; i++) bitWrite(value, i, ds18b20_read_bit()); return value; } bool ds18b20_read(int *raw_temp_value) { if (!ds18b20_start()) // send start pulse return(0); ds18b20_write_byte(0xCC); // send skip ROM command ds18b20_write_byte(0x44); // send start conversion command while(ds18b20_read_byte() == 0); // wait for conversion complete if (!ds18b20_start()) // send start pulse return(0); // return 0 if error ds18b20_write_byte(0xCC); // send skip ROM command ds18b20_write_byte(0xBE); // send read command // read temperature LSB byte and store it on raw_temp_value LSB byte *raw_temp_value = ds18b20_read_byte(); // read temperature MSB byte and store it on raw_temp_value MSB byte *raw_temp_value |= (unsigned int)(ds18b20_read_byte() << 8); return(1); // OK --> return 1 } // end of code. |
The picture below shows my protoboad circuit:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.