Connecting DS18B20 to ESP32/ESP8266

DS18B20 convenient digital thermal sensor. In industrial operation, sensors are usually used in a waterproof stainless steel capsule. Sensor Characteristics:

  • The Range of measured temperatures is 55 ° ~ + 125 °.
  • Supply Voltage 3.0 V ~ 5.5 V
  • Low Cost (~ 1.3 USD)
  • Single-Wire interface 1Wire.
  • Accuracy ± 0.5 °C in the range of-10 °C to + 85 °C.
  • Programmable resolution from 9 Bits to 12 Bits.
  • Unique 64-bit serial number for each sensor eliminates conflicts on one bus.
  • It Is possible to connect directly to microcontrollers without additional converters.
Pinup DS18B20
Pinup DS18B20

Sensor Outputs in stainless steel: Red (VCC), yellow (DATA), Black (GND)

The wiring Diagram is very simple. The Sensor is connected to the digital input via the resistor 4.7 K on + power supply.

I purchased sensors in metal waterproof capsules wholesale, 10 pcs. This seller on Aliexpress. Price ~ 1.3 USD per piece. With operative delivery to Russia. If the delivery time is not critical, the sensor can be ordered at a price of about 1 USD from many sellers on Aliexpress with free shipping.

Retrieving DS18B20 sensor addresses

#include < OneWire. h.

OneWire DS (15);  Data wire connected to GPIO15

void Setup (void) {
  Serial. Begin (9600);
}

void loop (void) {
  byte I;
  BYTE addr[8];
  
  if (! ds. Search (addr)) {
    Serial. println ("No more addresses.");
    Serial. println ();
    ds. Reset_search ();
    Delay (250);
    Return
  }
  Serial. Print ("ROM =");
  for (i = 0; i < 8; i + +) {
    Serial. Write (' ');
    Serial. Print (add[i]r, HEX);
  }
}

DS18B20 sensor Survey

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

Data wire is connected to GPIO15
#define ONE_WIRE_BUS 15
Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire (ONE_WIRE_BUS);
Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature Sensors (& oneWire);

DeviceAddress Sensor1 = {0x28, 0xB1, 0x13, 0x46, 0x92, 0xD, 0x2, 0xA7};

void Setup (void)
{
  Serial. Begin (115200);
  Sensors. Begin ();
}

void loop (void)
{ 
  Serial. Print ("Requesting temperatures...");
  Sensors. requestTemperatures (); Send the command to get temperatures
  Serial. println ("DONE");
  
  Serial. Print ("Sensor 1:");
  Serial. Print (String (sensors. getTempC (Sensor1)) + "C,"); 
  Serial. println (String (sensors. getTempF (Sensor1)) + "F"); 

  tempSensor. requestTemperaturesByIndex (0);
  Get Single sensor value
  Serial. Print ("Temperature:");
  Serial. Print (sensors. getTempCByIndex (0));
  Serial. Print ("C,");
  Serial. Print (sensors. getTempFByIndex (0));
  Serial. println ("F");
  Delay (1000);
}

If more than one sensor hangs on the same bus, the code will be somewhat more complex. In this case, two DS18B20 sensors connected to the GPIO32 microcontroller ESP32 are tested.

#include "OneWire_Scanner. H"
#include < OneWire. h.
#include < DallasTemperature. h.

#define OneWire_PIN 32

OneWire DS (OneWire_PIN);

Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature Sensors (& DS);

Count of DS18xxx Family devices on bus
uint8_t devices = 0; Reset the number of devices when we enumerate wire devices
uint8_t ds18Count = 0; Reset number of DS18xxx Family devices

void Setup ()
{
  Sensors. Begin ();

  ScanOneWireDevicesAndShowTemp ();

  Output the total number of found devices
  Serial. println ("Foun[" + String(ds18Count) + "]d DS18XXX sensors of[" + String(devices) + "] iWare devices.");
  Serial. Print (sensors. getDeviceCount (), DEC);
  Serial. Print (sensors. getDS18Count (), DEC);

  Check how the power is connected
  Serial. Print ("Parasite power is:");
  Serial. println (sensors. isParasitePowerMode ()? "ON": "OFF");
}

void Loop ()
{
  ScanOneWireDevicesAndShowTemp ();
  Delay (1000);
}

String getSensorModel (uint8_t modelByte)
{
  deviceAddress[0]
  String res = "";
  Switch (modelByte) 
  {
    Case DS18S20MODEL:
      return "DS18S20";
    Case DS18B20MODEL:
      return "DS18B20";
    Case DS1822MODEL:
      return "DS1822";
    Case DS1825MODEL:
      return "DS1825";
    Case DS28EA00MODEL:
      return "DS28EA00";
    default:
      return "Unknown";
  }
}

void ScanOneWireDevicesAndShowTemp ()
{
    DeviceAddress deviceAddress;
  
    while (ds. Search (deviceAddress)) 
    {
      if (sensors. validAddress (deviceAddress)) 
      {
        bitResolution = max (bitResolution, sensors. getResolution (deviceAddress));
        Devices + +;
        if (sensors. validFamily (deviceAddress)) 
        {
          Sensors. requestTemperatures (); Send the command to get temperatures
          Serial. Print ("Sensor");
          printAddress (deviceAddress);
          Serial. println (". Temp: "+ String (sensors. getTempC (deviceAddress)) +" oC with resolution "+ String (sensors. getResolution (deviceAddress)) +" bit. Sensor "+ getSensorModel (deviceAddress[0]) +". "); 
          ds18Count + +;
        }
      }
    }
}

void printAddress (DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i + +)
  {
    if (deviceAddress[i] < 16) Serial. Print ("0");
    Serial. Print (deviceAddres[i]s, HEX);
  }
}

The Result of the DS18B20 Sensor Survey code:

11:42:18.863-> Sensor 28B11346920D02A7. Temp: 24.19 oC with resolution 12 bit. Sensor DS18B20.
11:42:18.999-> Sensor 28FF6C7997090341. Temp: 28.12 oC with resolution 12 bit. Sensor DS18B20.
11:42:20.022-> Found[2] DS18XXX sensors of[2] iWare devices.
11:42:20.022-> Parasite power is: OFF

Useful Links

Spread the love
This entry was posted in IT Recipies. Bookmark the permalink.