NodeMCU & DS18B20 Sensor

This is a follow-up of my previous project using the Raspberry Pi to monitor my fridge. Now with the NodeMCU, you get a cheaper and more compact way of doing the same job.

This project combines the DS18B20 Temperature sensor with the NodeMCU. The components are as follows:

  1. NodeMCU
  2. DS18B20 Sensor – I got mine from Ubitap or you can get it from AliExpress
  3. 4.7KΩ resistor

The DS18B20 Sensor needs to have a 4.7KΩ resistor between the VDD(3.3v) and the Data lead. So I wired it as such:

 

Notice the resistor in between the RED(3.3v) and YELLOW(Data) heads

Software

If you don’t know how to install the libraries, click here for the guide to install the arduino libraries

 


 

Install the sensor

The DS18B20 sensor may come in wire color combination. Check yours set here.

I have the Red (3.3v), Black (GND) and Yellow (Data).

Wiring is as follows :

  • Red – 3.3v
  • Black – GND
  • Yellow – D2

(Make sure you have the 4.7KΩ resistor between the Red / Yellow wires)

Code

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin D2 on NodeMCU
#define sensorPin D2

// Setup the oneWire Sensor
OneWire oneWire(sensorPin);

// Pass reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup(void)
{
// start serial port
Serial.begin(115200);
sensors.begin();
}

void loop(void)
{
// Get temperature
sensors.requestTemperatures();

Serial.print(“Temperature for DS18B20 is: “);
Serial.println(sensors.getTempCByIndex(0));
// 0 refers to the first IC on the wire
// accomodates more than one sensor
}

Up Next: Connecting to your WiFi network

3 Comments Add yours

Leave a comment