EMB4: 1-Wire Temperature Sensor

The temperature-only sensor DS18B20 uses a 1-wire protocol. “1-wire” applies only to the bidirectional bus; power and GND are on separate pins. The sensor is connected to the MCU using GPIO 6 only. Many devices can be connected on a single 1-wire bus, each with a unique ID. DS18B20 also has a water-proof metal enclosure version (but here, in our lab, we use a plastic one) that enables easy monitoring of the liquid's temperature.


To handle operations with DS18B20, we will use a dedicated library that uses a 1-wire library on a low level:

  lib_deps = 

Sensor readings can be sent over the network or presented on one of the node's displays (e.g. LCD), so understanding how to handle at least one of the displays is essential:

A good understanding of the hardware timers is essential if you plan to use asynchronous programming (see note below). Consider getting familiar with the following:

Suggested Readings and Knowledge Resources

Hands-on Lab Scenario

In this scenario, we present how to interface the 1-wire sensor, DS18B20 (temperature sensor).

Task to be implemented

Read the temperature from the sensor and present it on the display of your choice. Show the reading in C. Note that the scenario below presents only how to use the DS18B20 sensor. How to display the data is present in other scenarios, as listed above. We suggest using an LCD (scenario EMB5: Using LCD Display).

Update reading every 10s. Too frequent readings may cause incorrect readings or faulty communication with the sensor. Remember, the remote video channel has its limits, even if the sensor can be read much more frequently.


Check if your display of choice is visible in the FOV of the camera once the device is booked.


The steps below present only interaction with the sensor. Those steps should be supplied to present the data (or send it over the network) using other scenarios accordingly.

Step 1

Include Dallas sensor library and 1-Wire protocol implementation library:

#include <OneWire.h>
#include <DallasTemperature.h>
Also, remember to add the LCD handling library, as present in the EMB9 scenario, unless you decide to use another output device.

Step 2

Declare the 1-Wire GPIO bus pin, 1-Wire communication handling object, sensor proxy and a variable to store readings:

#define ONE_WIRE_BUS 6
static OneWire oneWire(ONE_WIRE_BUS);
static DallasTemperature sensors(&oneWire);
static float tempDS;
Note, the sensors class represents a list of all sensors available on the 1-Wire bus. Obviously, there is just a single one in each of our laboratory nodes.

Step 3

Initialise sensors' proxy class:


Step 4

[pczekalski][✓ ktokarz, 2024-04-22] check if you don't need to call sensors.requestTemperatures(); before the code below Read the data:

  if (sensors.getDeviceCount()>0)
    tempDS = sensors.getTempCByIndex(0);
    // Sensors not present (broken?) or 1-wire bus error

Remember not to read the sensor too frequently. 10s between consecutive readings is just fine.
Devices in the 1-Wire bus are addressable either by index (as in the example above) or by their address.
Some useful functions are present below:

  • DallasTemperature::toFahrenheit(tempDS) - converts temperature in C to F,
  • sensors.getAddress(sensorAddress, index) - reads device address given by uint8_t index and stores it in DeviceAddress sensorAddress.

The library can make non-blocking calls, which can also be implemented using timers, as presented in the scenario ADV1: Using timers to execute code asynchronously.

Result validation

The observable temperature is usually within the range of 19-24C. If you find the temperature much higher, check your code, and if that is okay, please contact our administrator to inform us about the faulty AC.

Project information

This Intellectual Output was implemented under the Erasmus+ KA2.
Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, companies and labour force in the global IoT market.
Project number: 2022-1-PL01-KA220-HED-000085090.

Erasmus+ Disclaimer
This project has been funded with support from the European Commission.
This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein.

Copyright Notice
This content was created by the IOT-OPEN.EU Reloaded consortium, 2022,2024.
The content is Copyrighted and distributed under CC BY-NC Creative Commons Licence, free for Non-Commercial use.

en/iot-open/practical/hardware/sut/esp32/emb4_1.txt · Last modified: 2024/04/22 17:55 by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0