Git Good @ Tech

ESP32 Arduino DS18B20 Temperature sensor.

Here are couple of quick ways to set up temperature sensing with ESP32 using DS18B20 temperature sensors. Why DS18B20 you may ask. Well here is why:

  1. Measures Temperatures from -55°C to +125°C (-67°F to +257°F)
  2. ±0.5°C Accuracy from -10°C to +85°C
  3. Programmable Resolution from 9 Bits to 12 Bits
  4. Unique 1-Wire® Interface Requires Only One Port Pin for Communication

So its accurate sensor with nice range. Plus its it doesn’t need analog pins because it uses 1-wire digital interface.

So lets get started…




 

The sensor works with the method of 1-Wire communication. It requires only the data pin connected to the microcontroller with a pull up resistor and the other two pins are used for power.

DS18B20 datasheet: https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf or archived here: DS18B20

We also need OneWire library: https://github.com/stickbreaker/OneWire  or archived here: OneWire-master .
Also temperature sensor library: https://github.com/milesburton/Arduino-Temperature-Control-Library or archived here: Arduino-Temperature-Control-Library-master

We will use GPIO 15 And connect the 4.4k resistor between GIPO 15 and 3.3V.

Sesnsor wiring:

Sensor ESP32
Red – VCC 3.3V
Black – GND GND
Yellow – Data GPIO 15

Code without temperature sensor library:

include <OneWire.h>
 
// OneWire DS18S20, DS18B20, DS1822 Temperature Example
 
OneWire ds(15); // on pin D4 (a 4.7K resistor is necessary)
 
void setup(void){
    Serial.begin(9600);
}
 
void loop(void){
    byte i;
    byte present = 0;
    byte type_s;
    byte data[12];
    byte addr[8];
    float celsius, fahrenheit;
 
    if ( !ds.search(addr)){
        ds.reset_search();
        delay(250);
        return;
    }
 
    if (OneWire::crc8(addr, 7) != addr[7]){
        Serial.println("CRC is not valid!");
        return;
    }

//Serial.println();
 
// the first ROM byte indicates which chip
    switch (addr[0]){
       case 0x10:
           type_s = 1;
           break;
       case 0x28:
           type_s = 0;
           break;
       case 0x22:
           type_s = 0;
           break;
       default:
           Serial.println("Device is not a DS18x20 family device.");
           return;
       }
 
    ds.reset();
    ds.select(addr);
    ds.write(0x44, 1); // start conversion, with parasite power on at the end
    delay(1000);
    present = ds.reset();
    ds.select(addr);
    ds.write(0xBE); // Read Scratchpad
 
    for ( i = 0; i < 9; i++){
        data[i] = ds.read();
    }
 
// Convert the data to actual temperature
   int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10){
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  }
  else{
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
 	}
    celsius = (float)raw / 16.0;
    fahrenheit = celsius * 1.8 + 32.0;
    Serial.print("Temperature = ");
    Serial.print(celsius);
    Serial.print(" Celsius, ");
    Serial.print(fahrenheit);
    Serial.println(" Fahrenheit");
}

And now example with temperature sensor library:

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 15

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

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

/*
 * The setup function. We only start the sensors here
 */
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}

/*
 * Main function, get and show the temperature
 */
void loop(void)
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));  
}

 

 

3 Comments

  1. Tom

    Thanks for a good samples and explanations on how to connect a DS18D20 sensor to a ESP32 Board.

  2. Kunal Shyamadas Burman

    Hello, how to read the same data to SD card and save it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024 JakeMakes

Theme by Anders NorenUp ↑