ESP8266 - Temperature Sensor - LCD

This tutorial instructs you how to use ESP8266 to acquire the temperature from a DS18B20 sensor and display it on an LCD I2C.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×LCD I2C
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Buy Note: Many DS18B20 sensors available in the market are unreliable. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of Temperature Sensor and LCD

If you are not familiar with temperature sensor and LCD (pinout, how it works, how to program ...), the following tutorials can provide you with more information:

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Temperature Sensor LCD

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

We recommend buying a DS18B20 sensor along with its accompanying wiring adapter for a seamless setup. This adapter includes an integrated resistor, removing the need for an additional resistor in the wiring.

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-temperature-sensor-lcd */ #include <OneWire.h> #include <DallasTemperature.h> #include <LiquidCrystal_I2C.h> #define SENSOR_PIN D7 // The ESP8266 pin connected to DS18B20 sensor's DQ pin OneWire oneWire(SENSOR_PIN); DallasTemperature DS18B20(&oneWire); LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows float temperature_C; // temperature in Celsius float temperature_F; // temperature in Fahrenheit void setup() { DS18B20.begin(); // initialize the sensor lcd.init(); // Initialize the LCD I2C display lcd.backlight(); // open the backlight } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures temperature_C = DS18B20.getTempCByIndex(0); // read temperature in Celsius temperature_F = temperature_C * 9 / 5 + 32; // convert Celsius to Fahrenheit lcd.clear(); lcd.setCursor(0, 0); // display position lcd.print(temperature_C); // display the temperature in Celsius lcd.print((char)223); // display ° character lcd.print("C"); lcd.setCursor(0, 1); // display position lcd.print(temperature_F); // display the temperature in Fahrenheit lcd.print((char)223); // display ° character lcd.print("F"); delay(500); }

※ NOTE THAT:

The address of the LCD may differ depending on the manufacturer. In our code, we have used 0x27 as specified by DIYables.

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Plug the USB cable into the ESP8266 and the PC.
  • Launch the Arduino IDE, choose the correct board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “Dallas” and locate the DallasTemperature library created by Miles Burton.
  • Press the Install button to add the library.
ESP8266 NodeMCU Dallas Temperature library
  • You will be asked to install the dependency. Click Install All button to install OneWire library.
ESP8266 NodeMCU onewire library
  • Search for “LiquidCrystal I2C” and then locate the LiquidCrystal_I2C library by Frank de Brabander.
  • Press the Install button to install the LiquidCrystal_I2C library.
ESP8266 NodeMCU LiquidCrystal I2C library
  • Copy the code and open it with the Arduino IDE.
  • Then, press the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
Arduino IDE Upload Code
  • Put the sensor to both hot and cold water, or hold it in your hand.
  • Check the LCD display.

If the LCD display is not showing anything, check out Troubleshooting on LCD I2C for help.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial