ESP32 – Check temperature with DS18B20

One of my goals when playing with these chips is to enhance elements of smart home. One of features is measurement of home parameters including temperature. Earlier I have integrated it with Fibaro sensor, now I’ll check it with ESP32 and DS18B20.

Application setup

First step is to get empty project as described here and rename it to temperature for example. Update Makefile, set serial port and remove main.c file.

Now we need to add support for DS18B20. On GitHub there is already component which handles it but it will require minor update to compile seamlessly. Each project may contain several components organized in components subdirectory. Components are simply pieces of code which are compiled into static library and then linked into our application. Some of them are available in ESP-IDF but we can create also ours. For purpose of handling DS18B20 I used component from FeelFreeLinux, there are 2 small changes which must be applied.

cd components
git clone --recursive
cd ds18b20
mkdir include
mv ds18b20.h include

Lines 4 and 5 in script above are needed to allow build system to find automatically header files.

Second change is that component’s makefile – file does not require inclusion of So you can comment it out.

Now go back to main dir and prepare the following application (it is also based on example available on FeelFree with minor change.

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "string.h"
#include "esp_system.h"
#include "nvs_flash.h"

// *** Include DS18B20 library
#include "ds18b20.h" 

// *** GPIO pin number for DS18B20
const int DS_PIN = 14; 

// *** Task measuring temperature
void mainTask(void *pvParameters) 

    while (1) 
        printf("Temperature: %0.1f\n",DS_get_temp());
        vTaskDelay(1000 / portTICK_PERIOD_MS);

void app_main()
    xTaskCreatePinnedToCore(&mainTask, "mainTask", 2048, NULL, 5, NULL, 0);

 Hardware setup

Now for the hardware. We need

  • ESP32 board – I use SparkFun ESP32 Thing
  • DS18B20
  • 4.7kΩ resistor
  • a few cables
  • breadboard
    DS18B20 connection diagram

    Connected diagram looks like this

Vusb, GND and GPIO14 used on the ESP32 board
Cables and sensor connected together
Circuit ready

Here we go

So now what left is simply connect circuit using USB cable and execute

make flash monitor

As a result you should see something like

Hash of data verified.

Hard resetting...
--- WARNING: Serial ports accessed as /dev/tty.* will hang gdb if launched.
--- Using /dev/cu.usbserial-DN01DXXH instead...
--- idf_monitor on /dev/cu.usbserial-DN01DXXH 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0ets Jun  8 2016 00:22:57

ets Jun  8 2016 00:22:57

configsip: 0, SPIWP:0x00
mode:DIO, clock div:2
ho 0 tail 12 room 4
0x40080000: _iram_start at ??:?

entry 0x40080034
0x40080034: _iram_start at ??:?

Temperature: 23.4
Temperature: 23.4
Temperature: 23.4

Next steps

What I plan to do next is:

  1. Build class for sensor handling to make it more encapsulated
  2. Test pressure and humidity sensors
  3. Set WiFi and BT connections to send data further



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.