By Abdul Rehman
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.
According to the ds18b20 datasheet the main features of this sensor are as follows
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.
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.
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); | |
} |
20 November 2022
13 November 2022
07 September 2022
27 July 2022
28 February 2022
FYP Solutions Powered By Impressive Business WordPress Theme