ds18b20 esp8266DS18b20 interfacing with esp8266

DS18b20 is a temperature Sensor module which works on single wire protocol. The reason to choose DS18b20 as a temperature sensor is because, we can interface many DS18b20 sensors on single wire. So we do not need extra pins for interfacing as well as we also do not need analog pins. Because ESP8266 Nodemcu module has only single analog pin, so it is not wise to use it for such sensors which could easily be replaced with the digital output like DS18b20.

DS18b20 Main Features

According to the ds18b20 datasheet the main features of this sensor are as follows

  • Power range is 3.0v to 5.5v
  • It can measure temperature from -55°C to +125°C
  • ±0.5°C accuracy from -10°C to +85°C
  • Thermometer resolution is programmable from 9 to 12 bits

Required Hardware

  • ESP8266 NodeMCU module
  • DS18b20 Temperature Sensor
  • Breadboard
  • 4.7K Resistor
  • Male to male jumpers or 0R resistors
  • Serial Terminal Software for viewing the output

Wiring Diagram

DS18b20 is a 3 pin sensor. Two pins are for power supply and middle pin for reading the output temperature via one wire protocol. Here is the wiring diagram for DS18b20 with ESP8266 Nodemcu board.

DS18b20 Interfacing with ESP8266

as you can see we had used only nodemcu and ds18b20 interfacing with esp8266 module. We do not need extra hardware for viewing the code.

Required Libraries and Code

There are two main libraries which we use for reading the DS18b20 with esp8266. Same libraries and code is also valid for other Arduino variants. The libraries you need for this code are <DallasTemperature.h> and <OneWire.h>.

The complete code is as follows

/*
Here is the complete tutorial for this code
https://www.fypsolutions.com/examples/ds18b20-interfacing-with-esp8266-nodemcu-board/
*/
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS D1 //D2 pin of nodemcu
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature ds18b20(&oneWire); // Pass the oneWire reference to Dallas Temperature.
void setup(void)
{
Serial.begin(9600);
ds18b20.begin();
}
void loop(void)
{
ds18b20.requestTemperatures(); // Send the command to get temperatures
Serial.print("Temp: ");
Serial.println(ds18b20.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
delay(500);
}
DS18b20 interfacing with ESP8266
DS18b20 esp8266 code

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

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.