ESP8266 - Temperature Sensor

This tutorial instructs you how to use the waterproof DS18B20 1-wire temperature sensor with ESP8266. This sensor is cost-effective, simple to use and has an attractive appearance.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Temperature Sensor DS18B20
1×4.7 kΩ resistor
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 One Wire Temperature Sensor - DS18B20

The Temperature Sensor Pinout

The DS18B20 temperature sensor has three pins that need to be connected in a specific way:

  • GND pin: should be connected to ground (0V)
  • VCC pin: should be connected to the voltage source (5V or 3.3V)
  • DATA pin: is the 1-wire data bus and should be connected to a digital pin on the ESP8266 board.

The sensor typically comes in two varieties: TO-92 package (which resembles a transistor) and a waterproof probe. For this tutorial, we will be using the waterproof probe form.

DS18B20 temperature sensor pinout

Connecting a DS18B20 temperature sensor to an Arduino requires a pull-up resistor, which can be a hassle. Fortunately, some manufacturers have made it easier by offering a wiring adapter that has a built-in pull-up resistor and screw terminal block, simplifying the wiring process.

Wiring Diagram

  • Wiring diagram using a breadboard.
ESP8266 NodeMCU temperature sensor wiring diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram using a the wiring adapter (recommended).
ESP8266 NodeMCU DS18B20 wiring diagram

This image is created using Fritzing. Click to enlarge image

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. We also tested it and worked well.

How To Program For DS18B20 Temperature Sensor

  • Include the library in your project.
#include <OneWire.h> #include <DallasTemperature.h>
  • Declare an object of type OneWire, corresponding to the pin connected to the sensor's DATA pin. Additionally, declare an object of type DallasTemperature.
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library
  • Begin the sensor setup process:
sensors.begin(); // initialize the sensor
  • Issue the command to obtain temperatures.
  • Read the temperature in Celsius.
tempCelsius = sensors.getTempCByIndex(0);
  • Calculate Fahrenheit from Celsius:
tempFahrenheit = tempCelsius * 9 / 5 + 32;

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * */ #include <OneWire.h> #include <DallasTemperature.h> #define SENSOR_PIN 21 // ESP8266 pin GPIO21 connected to DS18B20 sensor's DQ pin OneWire oneWire(SENSOR_PIN); DallasTemperature DS18B20(&oneWire); float tempC; // temperature in Celsius float tempF; // temperature in Fahrenheit void setup() { Serial.begin(9600); // initialize serial DS18B20.begin(); // initialize the DS18B20 sensor } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures tempC = DS18B20.getTempCByIndex(0); // read temperature in °C tempF = tempC * 9 / 5 + 32; // convert °C to °F Serial.print("Temperature: "); Serial.print(tempC); // print the temperature in °C Serial.print("°C"); Serial.print(" ~ "); // separator between °C and °F Serial.print(tempF); // print the temperature in °F Serial.println("°F"); delay(500); }

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.
  • Connect the ESP8266 to a computer using a USB cable.
  • Open the Arduino IDE, select the appropriate board and port.
  • In the Arduino IDE, navigate to the Tools menu and select Manage Libraries.
ESP8266 NodeMCU add library
  • Search for “OneWire” and locate the OneWire library created by Paul Stoffregen.
  • Then, press the Install button to complete the installation.
ESP8266 NodeMCU onewire library
  • Search for “Dallas”, then locate the DallasTemperature library created by Miles Burton.
  • Press the Install button to install the DallasTemperature library.
ESP8266 NodeMCU Dallas Temperature library
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to transfer the code to the ESP8266.
  • Place the sensor in hot and cold water or hold it in your hand.
  • Check the results on the Serial Monitor.
Temperature: 26.31°C ~ 79.36°F Temperature: 26.44°C ~ 79.59°F Temperature: 26.50°C ~ 79.70°F Temperature: 26.56°C ~ 79.81°F Temperature: 27.06°C ~ 80.71°F Temperature: 27.75°C ~ 81.95°F Temperature: 28.37°C ~ 83.07°F Temperature: 29.00°C ~ 84.20°F Temperature: 29.56°C ~ 85.21°F Temperature: 30.00°C ~ 86.00°F Temperature: 30.31°C ~ 86.56°F Temperature: 30.62°C ~ 87.12°F Temperature: 30.87°C ~ 87.57°F
Autoscroll Show timestamp
Clear output
9600 baud  

Video Tutorial

Creating videos is a time-consuming task. If you find video tutorials helpful for your learning, please let us know by subscribing to our YouTube channel . If there is high demand for videos, we will work on creating them.