ESP32 NodeMCU Interface DS1B20
The DS18B20 is a digital temperature sensor that communicates over a 1-Wire interface, meaning multiple sensors can be connected to the same data pin. It provides 9–12 bit temperature readings in Celsius, with a range of -55°C to +125°C and ±0.5°C accuracy.
Key Features:
- Digital output – No analog-to-digital conversion required.
- Unique 64-bit address – Allows multiple sensors on the same bus.
- Power options – Can operate with external VCC or parasite power (powered through data line).
- High resolution – 9–12 bits configurable (0.5°C to 0.0625°C per step).
- Low cost and widely available
Procedure
- Initialize 1-Wire bus.
- Search for DS18B20 sensors.
- Verify CRC and family code.
- Start temperature conversion (0x44).
- Read scratchpad (0xBE).
- Convert raw data to Celsius and print.
DS18B20 is popular in embedded systems due to its simplicity, accuracy, and ability to connect multiple sensors. Common applications include:
Applications
- Home Automation & IoT
- Smart thermostats
- Room temperature monitoring
- Automated HVAC systems
- Industrial Monitoring
- Process temperature control
- Refrigeration units and cold storage monitoring
- Greenhouse temperature tracking
- Embedded Projects
- Arduino/ESP32 based weather stations
- Temperature logging with SD card or cloud
- Multi-sensor arrays using one data pin
- Consumer Electronics
- Water heaters and coffee machines
- Appliances requiring precise temperature feedback
Code of ESP32 NodeMCU Interface DS1B20
Onewire – DS18bB20 Sensor interface
pin = 23
ow.setup(pin)
count = 0
repeat
count = count + 1
addr = ow.reset_search(pin)
addr = ow.search(pin)
until (addr ~= nil) or (count > 100)
if addr == nil then
print("No more addresses.")
else
print(addr:byte(1,8))
crc = ow.crc8(string.sub(addr,1,7))
if crc == addr:byte(8) then
if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then
print("Device is a DS18S20 family device.")
repeat
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, 0x44, 1)
-- tmr.delay(1000000)
present = ow.reset(pin)
ow.select(pin, addr)
ow.write(pin,0xBE,1)
print("P="..present)
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
print(data:byte(1,9))
crc = ow.crc8(string.sub(data,1,8))
print("CRC="..crc)
if crc == data:byte(9) then
t = (data:byte(1) + data:byte(2) * 256) * 625
t1 = t / 10000
t2 = t % 10000
print("Temperature="..t1.."."..t2.."Centigrade")
end
until false
else
print("Device family is not recognized.")
end
else
print("CRC is not valid!")
end
end
Setup
pin = 23 ow.setup(pin)
pin = 23→ GPIO 23 is used for the DS18B20 data line.ow.setup(pin)→ Initializes the 1-Wire bus on that pin.
Searching for 1-Wire Devices
count = 0 repeat count = count + 1 addr = ow.reset_search(pin) addr = ow.search(pin) until (addr ~= nil) or (count > 100)
- Searches for devices connected to the 1-Wire bus.
- Loops up to 100 times until a device address is found.
if addr == nil then
print("No more addresses.")
else
print(addr:byte(1,8))
- Prints the 8-byte address of the device if found.
CRC Check
crc = ow.crc8(string.sub(addr,1,7)) if crc == addr:byte(8) then
- Calculates CRC of the first 7 bytes of the address.
- Validates if the device address is correct (prevents communication errors).
Device Type Check for ESP32 NodeMCU Interface DS1B20
if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then
print("Device is a DS18S20 family device.")
- DS18S20 family uses 0x10 or 0x28 as family codes.
Reading Temperature
ow.reset(pin) ow.select(pin, addr) ow.write(pin, 0x44, 1) -- Start temperature conversion
- Sends Convert T command (0x44) to the sensor.
- Sensor measures temperature (takes ~750ms for 12-bit resolution).
present = ow.reset(pin) ow.select(pin, addr) ow.write(pin,0xBE,1) -- Read scratchpad
- Sends Read Scratchpad command (0xBE) to read temperature data.
data = string.char(ow.read(pin)) for i = 1, 8 do data = data .. string.char(ow.read(pin)) end
- Reads 9 bytes from the sensor’s scratchpad (includes temperature and CRC).
Temperature Conversion
t = (data:byte(1) + data:byte(2) * 256) * 625
t1 = t / 10000
t2 = t % 10000
print("Temperature="..t1.."."..t2.."Centigrade")
- Combines LSB and MSB to get raw temperature.
- Multiplies by 0.0625 (represented here as 625 / 10000) to convert to Celsius.