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×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×4.7 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) Screw Terminal Expansion Board for ESP8266

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables.

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.
The wiring diagram between ESP8266 NodeMCU and temperature sensor

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.

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

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 DS18B20(&oneWire); // pass oneWire to DallasTemperature library
  • Begin the sensor setup process:
DS18B20.begin(); // initialize the sensor
  • Issue the command to obtain temperatures.
DS18B20.requestTemperatures();
  • Read the temperature in Celsius.
temperature_C = DS18B20.getTempCByIndex(0);
  • Calculate Fahrenheit from Celsius:
temperature_F = temperature_C * 9 / 5 + 32;

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 */ #include <OneWire.h> #include <DallasTemperature.h> #define SENSOR_PIN D7 // The ESP8266 pin connected to DS18B20 sensor's DQ pin OneWire oneWire(SENSOR_PIN); DallasTemperature DS18B20(&oneWire); float temperature_C; // temperature in Celsius float temperature_F; // temperature in Fahrenheit void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. DS18B20.begin(); // initialize the DS18B20 sensor } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures temperature_C = DS18B20.getTempCByIndex(0); // read temperature in °C temperature_F = temperature_C * 9 / 5 + 32; // convert °C to °F Serial.print("Temperature: "); Serial.print(temperature_C); // print the temperature in °C Serial.print("°C"); Serial.print(" ~ "); // separator between °C and °F Serial.print(temperature_F); // 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.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • 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
  • You will be asked to install the dependency. Click Install All button to install OneWire library.
ESP8266 NodeMCU onewire 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.
COM6
Send
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  
Newline  

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!