Header Ads

Header ADS

ESP32 getting temperature from DS18B20 sensor

In this tutorial we will learn how to get temperature measurements from a Maxim Integrated DS18B20 temperature sensor, using the ESP32 and the Arduino core.
In our introductory example, we will learn how to interact with the sensor and fetch temperature measurements periodically.

The DS18B20 is a digital thermometer that allows to get 9-bit to 12-bit Celsius temperature measurements (programmable resolution). The temperature conversion time depends on the resolution used. For a 9-bit resolution it takes at most 93.75 ms and for a 12-bit resolution it takes at most 750 ms.

The device is able to measure temperatures from -55°C to +125°C and has a ±0.5°C accuracy in the range from -10°C to +85°C .

Additionally, it has an alarm functionality with programmable upper and lower temperature trigger points. These thresholds are stored internally in non-volatile memory, which means they are kept even if the device is powered off .

The sensor communicates using the OneWire protocol, which means it only requires a pin from a microcontroller to be connected to it. Furthermore, each sensor has a unique 64-bit serial code, allowing multiple DS18B20 devices to function on the same OneWire bus.

In terms of power supply, the device can operate with a voltage between 3.0 V and 5.5 V, which means it can operate with the same voltage of the ESP32 without the need for level conversion.

Arduino library

Diagram

Program



#include "OneWire.h"

#include "DallasTemperature.h"

OneWire oneWire(22);

DallasTemperature tempSensor(&oneWire);

void setup(void)

{
Serial.begin(115200);
tempSensor.begin();
}

void loop(void)

{
tempSensor.requestTemperaturesByIndex(0);

  Serial.print("Temperature: ");

  Serial.print(tempSensor.getTempCByIndex(0));

  Serial.println(" C");

  delay(2000);

}


Output




No comments

Powered by Blogger.