DS18B20 Temperature Sensor with ESP32 DOIT Kit v1

adminESP325 hours ago4 Views

Introduction

The DS18B20 is a digital temperature sensor that provides highly accurate temperature readings over a range of -55°C to +125°C. Unlike analog sensors, it communicates over the OneWire protocol, which means multiple DS18B20 sensors can be connected to a single pin of the ESP32.

Key features include:

  • Digital output (no need for ADC).
  • High accuracy (±0.5°C in most of its range).
  • Wide voltage range (3.0V to 5.5V).
  • Each sensor has a unique 64-bit serial code, allowing multiple sensors on one bus.

In this tutorial, we will:

  • Connect the DS18B20 to the ESP32.
  • Use the OneWire and DallasTemperature libraries to read values.
  • Print temperature readings on the Serial Monitor.

Components Required

  • ESP32 DOIT Kit v1
  • DS18B20 Temperature Sensor (waterproof version works too)
  • 4.7kΩ Pull-up Resistor
  • Breadboard and jumper wires

ESP32 & DS18B20 Pin Connections

DS18B20 PinESP32 DOIT Kit Pin
VCC3.3V
GNDGND
DATAGPIO 26

Important: Connect a 4.7kΩ resistor between DATA and VCC (pull-up resistor).

Install Required Libraries

Before coding, install the following libraries in Arduino IDE:

  1. OneWire by Jim Studt / Paul Stoffregen
  2. DallasTemperature by Miles Burton

Arduino Code

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

// Data wire is connected to GPIO 26
#define ONE_WIRE_BUS 26

// Setup a oneWire instance
OneWire oneWire(ONE_WIRE_BUS);

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

void setup() {
  Serial.begin(115200);
  sensors.begin(); // Start DS18B20 sensor
}

void loop() {
  sensors.requestTemperatures(); // Send command to get temperature
  float temperatureC = sensors.getTempCByIndex(0); // Read temperature in °C

  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.println(" °C");

  delay(1000); // Wait 1 second
}

Step-by-Step Code Explanation

Include Libraries

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

    Adds support for OneWire and DS18B20 communication.

    Define Pin

    #define ONE_WIRE_BUS 26
    

    Data pin of DS18B20 connected to GPIO 26.

    Initialize OneWire & DallasTemperature

    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&amp;oneWire);
    

    Creates communication objects.

    Setup Function

    sensors.begin();
    

    Starts sensor communication.

    Loop Function

    sensors.requestTemperatures();
    float temperatureC = sensors.getTempCByIndex(0);
    

    Requests and reads temperature (sensor at index 0 if only one connected).

    Print Output

    Serial.println("Temperature: ... °C");
    

    Displays temperature on Serial Monitor.

    Real-Life Applications of DS18B20 with ESP32

    • 🌡️ Weather stations (measuring outdoor temperature).
    • 🏭 Industrial monitoring (machines, refrigeration systems).
    • 🏠 Smart home automation (HVAC, heating, cooling).
    • 🐠 Aquarium monitoring (with waterproof DS18B20).
    • 🌱 Agriculture & greenhouses (soil and environment monitoring).

    Troubleshooting

    • No readings → Check pull-up resistor between DATA and VCC.
    • -127°C output → Sensor not detected (check wiring).
    • 85°C output → Sensor not initialized properly (reset ESP32).
    • Multiple sensors → Use getDeviceCount() and unique addresses.

    What is the operating voltage of DS18B20 with ESP32?

    The DS18B20 works between 3.0V to 5.5V, so it’s safe to use with the ESP32’s 3.3V supply.

    Why do I need a 4.7kΩ pull-up resistor?

    The DS18B20 uses the OneWire protocol, and a pull-up resistor ensures stable data communication between the sensor and ESP32.

    What does “-127°C” mean in the Serial Monitor?

    This usually indicates the sensor is not detected. Check wiring, pull-up resistor, and ensure the correct GPIO pin is used.

    Can I connect multiple DS18B20 sensors to the same ESP32 pin?

    Yes ✅, the DS18B20 supports multiple sensors on a single pin since each sensor has a unique 64-bit address.

    What’s the accuracy of the DS18B20 sensor?

    It has a typical accuracy of ±0.5°C in the range of -10°C to +85°C.

    What’s the difference between DS18B20 and DHT22?

    DS18B20 → Only measures temperature (digital, high accuracy).
    DHT22 → Measures both temperature and humidity (slightly less accurate for temperature).

    Can I use DS18B20 in outdoor or wet conditions?

    Yes, the waterproof version of the DS18B20 is designed for outdoor and liquid applications like aquariums, water tanks, and soil monitoring.

    Why does my sensor always show 85°C?

    This is the default reading if the ESP32 fails to properly initialize the DS18B20. Try restarting or checking the wiring.

    Leave a reply

    Previous Post

    Next Post

    Loading Next Post...
    Follow
    Search Trending
    Popular Now
    Loading

    Signing-in 3 seconds...

    Signing-up 3 seconds...