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

// Data wire is connected to GPIO 4 on the ESP32
#define ONE_WIRE_BUS 4

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass the oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(112500);
  // Start the DS18B20 sensor communication
  sensors.begin();
}

void loop() {
  // Call sensors.requestTemperatures() to issue a temperature request and store the result
  sensors.requestTemperatures();

  // Read temperature in Fahrenheit
  float temperatureF = sensors.getTempFByIndex(0);
  
  // Print the temperature in Fahrenheit to the serial monitor
  Serial.print("Temperature: ");
  Serial.print(temperatureF);
  Serial.println(" F");

  delay(1000); // wait 1 second before taking the next temperature reading
}
Loading
ds18b20