In this tutorial we are going to learn how to monitor the temperature using NodeMCU Esp8266.

Hardware Required

  1. NodeMCU Esp8266 - 1
  2. DS18B20 temperature sensor - 1
  3. USB Cable - 1
  4. Jumper Wires - 3
  5. Resistor 4.7k - 1

The DS18B20 temperature sensor is a very popular 1-wire device which requires only one data line for communication with NodeMCU, and here we'll be showing how to use the onewire module to read from it and monitor temperature.

Circuit Diagram



Step 1: Connect NodeMCU Esp8266 to your device using USB Cable for power source and to dump the code to the NodeMCU.

Step 2: To use the DS18B20 temperature sensor, we need to connect it's data line to GPIO05 of the node. After connecting sensor's data line, connect it's negative end to ground and positive to power of NodeMCU using jumper wires. After completing the circuit, you can proceed to next step.

Step 3: Make a file, put the following block of code in it. Then Transfer it to your NodeMCU Esp8266 using ampy

import time
import machine
import onewire, ds18x20

# connecting device to GPIO05
dat = machine.Pin(05)

# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))

# scan for devices on the bus
devices = ds.scan()
print('found devices:', devices)

# loop 20 times and print all temperatures
for i in range(20):
    print('temperatures:', end=' ')
    for device in devices:
        print(ds.read_temp(device), end=' ')

Step 4: After transferring the file to NodeMCU run the file using ampy

ampy --port /dev/ttyUSB0 run

or by importing the file in REPL

picocom /dev/ttyUSB0 -b115200
>>> import temp




Please do let us know if we are missing any topic or you want us to elaborate any topic, comment below and subscribe to be updated.