echo '' ;

ESP32 ArduinoCore Interface – OW (DS18B20)

The ESP32 ArduinoCore Interface for OneWire (OW) communication protocol is a crucial aspect of interfacing with digital temperature sensors like the DS18B20. This interface allows the ESP32 microcontroller to communicate with one or more DS18B20 temperature sensors using the OneWire protocol.

Read more: ESP32 ArduinoCore Interface – OW (DS18B20)

Components

ComponentDescription
ESP32 MicrocontrollerA powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities.
DS18B20 Temperature SensorsDigital temperature sensors manufactured by Maxim Integrated, known for high accuracy readings.
OneWire LibraryProvides functions for communication over the OneWire bus, essential for interfacing with DS18B20 sensors.
DallasTemperature LibrarySimplifies communication with DS18B20 sensors by providing high-level functions and features.

Code

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

#define ONE_WIRE_BUS 22
#define TEMPERATURE_PRECISION 12 // Lower resolution

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

int numberOfDevices;
DeviceAddress tempDeviceAddress;

void setup(void) {
  Serial.begin(115200);
  Serial.println("Dallas Temperature IC Control Library Demo");
  sensors.begin();
  numberOfDevices = sensors.getDeviceCount();
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
  for(int i=0;i<numberOfDevices; i++) {
    if(sensors.getAddress(tempDeviceAddress, i)) {
      Serial.print("Found device ");
      Serial.print(i, DEC);
      Serial.print(" with address: ");
      printAddress(tempDeviceAddress);
      Serial.println();
      Serial.print("Setting resolution to ");
      Serial.println(TEMPERATURE_PRECISION, DEC);
      sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
      Serial.print("Resolution actually set to: ");
      Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
      Serial.println();
    } else {
      Serial.print("Found ghost device at ");
      Serial.print(i, DEC);
      Serial.print(" but could not detect address. Check power and cabling");
    }
  }
  delay(5000);
}

void printTemperature(DeviceAddress deviceAddress) {
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.println(tempC);
  Serial.print("Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}

void loop(void) { 
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures();
  Serial.println("DONE");
  for(int i=0;i<numberOfDevices; i++) {
    if(sensors.getAddress(tempDeviceAddress, i)) {
      Serial.print("Temperature for device: ");
      Serial.println(i,DEC);
      printTemperature(tempDeviceAddress);
      delay(3000);
    }
  }
}

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

Code Explanation

Code SectionExplanation
#include <OneWire.h>Includes the OneWire library for communication with devices using the OneWire protocol.
#include <DallasTemperature.h>Includes the Dallas Temperature library for interfacing with Dallas/Maxim temperature ICs.
#define ONE_WIRE_BUS 22Defines the GPIO pin (pin 22) where the OneWire data wire is connected to the ESP32.
#define TEMPERATURE_PRECISION 12Defines the resolution for temperature readings (12 bits for lower resolution).
OneWire oneWire(ONE_WIRE_BUS);Initializes a OneWire object with the specified GPIO pin.
DallasTemperature sensors(&oneWire);Initializes a DallasTemperature object using the previously created OneWire object.
int numberOfDevices;Declares a variable to store the number of temperature devices found on the bus.
DeviceAddress tempDeviceAddress;Declares a variable to store the address of a found temperature device.
void setup(void)Begins the setup function, which is called once when the program starts. Initializes serial communication and the sensor library.
Serial.begin(115200);Starts serial communication with a baud rate of 115200.
sensors.begin();Initializes the Dallas Temperature library.
numberOfDevices = sensors.getDeviceCount();Retrieves the number of temperature devices found on the OneWire bus.
for(int i=0;i<numberOfDevices; i++)Loops through each temperature device found on the bus.
sensors.getAddress(tempDeviceAddress, i)Retrieves the address of the i-th device and stores it in tempDeviceAddress.
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);Sets the resolution of the temperature device to the defined precision.
void printTemperature(DeviceAddress deviceAddress)Declares a function to print the temperature of a device with the given address.
void loop(void)Begins the loop function, which runs continuously after setup.
sensors.requestTemperatures();Requests temperature readings from all connected devices on the bus.
for(int i=0;i<numberOfDevices; i++)Loops through each temperature device found on the bus.
printTemperature(tempDeviceAddress);Prints the temperature of the i-th device.
void printAddress(DeviceAddress deviceAddress)Declares a function to print the address of a device.

Functionality of

  1. Initialization: The interface initializes the OneWire communication by defining the GPIO pin to which the OneWire data wire is connected. It also initializes the DallasTemperature library, which simplifies communication with DS18B20 sensors.
  2. Device Detection: Upon initialization, the interface detects the number of DS18B20 sensors connected to the OneWire bus. It retrieves the unique address of each sensor and sets their resolution if detected.
  3. Temperature Reading: The interface periodically requests temperature readings from all connected DS18B20 sensors. It then retrieves the temperature data and converts it to Celsius and Fahrenheit scales for further processing or display.

NEXT

Arduino-Core Get Start (Soon)
ESP32 Arduino Core Interface
ArduinoCore Interface Basics
ArduinoCore Interface WiFi
ArduinoCore Interface – LED
ArduinoCore Interface ADC
ArduinoCore Interface DS18B20
ESP32 Arduino Core Projects
ArduinoCore Project – WebServer
Others
Arduino-Core Sitemap
Arduino-Core All Post

Leave a Reply

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

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading