DS18B20 sensor[s] on ESP

Does anyone have experience getting DS18B20 sensors to work on ESPs?

I have tried Tasmota on a Wemos D1 Mini and a Tenstar Robot C3 Supermini.
When I connect the DSB1820 and 4.7K pull-up resistor, the ESP fails to boot, or at least it no longer connects to wifi, nor does it start it's own access point.
Connecting the pull-up resistor but no temperature sensor, the ESP starts up so it does seem to be something specific to the sensor.

To verify that the DS17B20s are good, I connected them to a Raspberry Pi GPIO4 and they work, accessed by command line rather than by a contrib node.:

Any ideas why the ESP won't start up?
Do you have DS18B20s working on ESP32 using diifferent firmware?

How exactly have you connected it?

@Colin has it right, you need to validate the wiring. You should review the following...

Confirm that the default Tasmota interface sees and reports the sensor data. You have to load the 'sensors' Tasmota firmware variant typically.

I tend of use i2c based sensors, but single wire is comparable, is simpler to implement, but I find harder to debug/resolve issues.

Once you have the ESP via Tasmota working correctly reporting sensor data. Then I would use NR MQTT support to 'talk' to the ESP, i.e. receive data, send commands to the ESP via MQTT, which is well documented via Tasmota references.

First I used a perfboard and soldered the connections

ESP 3.3V    => DS18B20 orange wire
               4K7 resistor between orange and yellow
ESP GPIO 9  => DS18B20 yellow wire
ESP GND     => DS18B20 black wire

Having little faith in my soldering skill I switched to using a 3 way connector block. This is the current connection, to the Pi. I don't think it's a terribly smart way of doing it, but it does work for the Pi.

Not sure I know how to do that?
Using the Tasmota web installer I can select the device (My PC does see a device connected to USB)
Instead of flashing again, I can open the console but it gives no output until I press the reset button. It never shows sensor data.

Did you look at the link I provided about how Tasmota documentation states the wiring should be done? You should see 'something' coming in on the GPIOx pin you are using for input.

I have indeed pored over that page.

I tried various GPIO pins, no difference. Also 5 different sensors. Some with leads, some in transistor style packaging.

The Wemos D1 Mini is flashed with tasmota-sensors, which is not explicitly listed on the DS18x20 page. It was v12, I tried upgrading to v15.

The Blakadder supported devices page says to flash the ESP32-C3 Super Mini with tasmota32c3-cdc. That option is not offered by the web installer so I used "Unofficial: Tasmota32-C3 2M (English)" which does work, until I connect the sensor.
This firmware is not listed on the page you linked either.

With no sensors connected but the pull-up in place, this is the web console output

And the web installer console when I reset

ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0xf (SPI_FAST_FLASH_BOOT)
Saved PC:0x42063e86
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5820,len:0x98
load:0x403cc710,len:0x824
load:0x403ce710,len:0x2204
SHA-256 comparison failed:
Calculated: 2d2da256600f8197a1e023a984a8a04d9f4bff674c6fe424c23f3c4a08f3a513
Expected: 20a97ffec3706e960069d93e80a000d0dacf7624e7f9baf65a320b4f487a7902
Attempting to boot anyway...
entry 0x403cc710

00:00:00.001 CMD: Using USB CDC
00:00:00.001 HDW: ESP32-C3 v0.4 
00:00:00.016 UFS: FlashFS mounted with 2224 kB free
00:00:00.024 CFG: Loaded from File, Count 35
00:00:00.026 SER: Set to 8N1 115200 bit/s
00:00:00.026 SER: HWCDC supports 115200 bit/s only
00:00:00.030 QPC: Count 1
00:00:00.062 BRY: Berry initialized, RAM used 3889 bytes
00:00:00.073 Project tasmota - Tasmota Version 15.2.0.4(0f16270-tasmota32_2M)-3.3.6(2026-01-24T17:18:16)
00:00:01.001 WIF: Connecting to AP1 GreyWagtail561 Channel 6 BSSId B4:FB:E4:62:00:67 in mode HT20 as esp32c3-1-2888...
00:00:03.359 WIF: Connected
00:00:03.566 HTP: Web server active on esp32c3-1-2888 with IP address 192.168.1.100
20:42:06.539 MQT: Attempting connection...
20:42:06.571 MQT: Connected

And this is the console with the sensors connected

ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0x7 (DOWNLOAD(USB/UART0/1))
Saved PC:0x40047ab6
waiting for download

ps I just reflashed the C3 using a firmware that is explicitly listed on the Tasmota guide page. Identical results..
All the evidence seems to be that these two ESPs cannot boot with the DS18B20 sensor connected, which is why I asked if anyone had had success with them, perhaps with different firmware.
I think @waldbaer's post is about using a different firmware, but hardware too?

I just developed in the last months a open source 1-Wire interface which can be accessed via ethernet/ MQTT protocol. It supports also the DS18B20 temperature sensor.

The board is based on the WT32-ETH01 using an ESP32.

1 Like

When I had these I used the Arduino IDE etc. using this Guide for DS18B20 Temperature Sensor with Arduino | Random Nerd Tutorials as a guide. (bog standard ESP32s not Arduino) Not sure if this will work for an ESP32C3

That looks like the ESP is being set to flash mode, and not booting up.

So probably not an issue with the sensor as such, but more likely to do with which pins are being held high/low.

I have a few of those sensors connected to D1 minis, not sure I have any on an ESP32 as yet, but do plan to do so.

Check which pins you are using, on the ESP not all are suitable.

Although not considered de rigueur these days, in situations like this I would always build a simple few lines of firmware from bare metal. That way you can reduce as many variables as possible.

Good point. Yes, it does look like the board is stuck in flash mode.
I did take some care over which gpio pin to use, and the resistor is set to pull up, not down.
I wonder if the initial current through the sensor is enough to pull down the voltage overall and thus start up in flash mode?

I have now experimented with the Arduino ide and a larger development board, the Adafruit Feather32. It works fine,

I think that's outside of my skill set!

Personally, I use ESPHome rather than Tasmota these days. I've found it a lot more reliable and it is so easy to modularise sensor config so that you can reuse standard configs across many different board types. The modularisation also makes it easy to turn off/on different sensors which can help isolate issues. It has enormous hardware support and yet it still lets you easily dip into C++ should you need something custom. Configuration/build for any board is simply some YAML, no code required. MQTT is simple and customisable if you want it. An admin web interface is also available.

Objection - Hearsay and Subjective Evidence are not admissible
:rofl:

How about tangible, real-world evidence then?!

I used to get regular device firmware crashes (requiring turning off and back on again) when using Tasmota, never had a single crash with ESPHome. Of course, this might no longer be the case, perhaps Tasmota has improved since I last tried it some years ago. And maybe newer ESP devices are less prone to crashing.

But even without that, I much prefer the specific configuration with ESPHome, I find it infinitely easier to work with and I get a lot of flexibility.

Still, each to their own of course.

That's my point really.

Absolutely true, and that's why I would advocate for Tasmota.

The berry scripting is very powerful, and combined with LVGL / OpenHasp compatibility, its not too hard to create a complete "product" with its own touch screen interface and logic.

Initial setup Comparison

Feature Tasmota (Sensors Binary) ESPHome (YAML Config)
Initial Setup Easier (Flash once, use Web UI) Harder (Write YAML, then compile)
Adding Sensors Point-and-click in browser Edit YAML and reflash

:winking_face_with_tongue:

I took a brief look at ESP Home a while back. Didn't get much further than the phrase "tightly integrated with Home Assistant".
However, I like the thought that it permits changing devices connected to the ESP without reprogramming (as Tasmota does too of course)

I have a BME680 sensor on Tasmota which has sailed through a couple of years' power cuts and regular reboots of the wireless router.

But I went back to Arduino for this particular project and now I clearly recall why I abandoned it. Five minutes plus for each compilation, and that's only achievable after turning off my antivirus.

I do now have the ESP-C3 super mini working with two DS18B20s connected so I shall put it in a project box and stop struggling with Tasmota.

Thanks for all the suggestions. :smiley:

We can agree to differ. :smiley:

ESPHome also supports some none-ESP MC's.

ESPHome has documented support for nearly 600 peripherals, Tasmota has around 150 or so as best I can tell.

So yes, your initial platform setup is slightly more complex but then you are mostly creating a small device YAML and including any peripheral YAML from your ever-growing standard config files, most of which are simply copy/paste from the documentation.

ESPHome uses standard C++ embedded either as a header file or via a simple lambda statement, no need to learn another script language (only useful if you know a bit of C++ though I suppose). You very rarely need any scripting anyway in ESPHome as interactions between components is mostly already catered for.

The other advantage for ESPhome - coming back to my original comments. You only include what you absolutely need for each device so memory usage can be considerably lower.

So, use ESPhome for maximum flexibility and device efficiency. Use Tasmota for ease of initial use.

It might be helpful to get to the bottom of this, as I do need to swap out a D1 for an ESP32, more likely to be an S2 mini, but will depend on price and availability I guess.

Can you tell me what gpios you used and exactly how it was wired up ?

Yes of course.

Board

The device I used is an ESP32-C3 Super Mini. It's the cheapest of the ESP32 boards in this postage stamp size. I paid £1.40 each from Aliexpress. It has a USB C connector. They also do a dedicated "expansion board" with three dupont wire friendly pins for each of the board pins.

Wiring

Each DS18B20 has a unique id and you can connect multiple sensors, all wired to the same GPIO. There also has to be a single 4.7kOhm pull-up resistor. I connected two DS18B20s, using 3.3V & Gnd pins and GPIO 9

Soldering less than ideal! Should have put the sensor wires through the holes and the resistor legs in the castellations.

Arduino code

In order to flash the board over USB from the Arduino IDE I had to enable the option USB CDC On Boot, as explained at https://docs.espressif.com/projects/arduino-esp32/en/latest/tutorials/cdc_dfu_flash.html

Apparently this is not required for the S2 or S3 variant.

Temperature readings are sent via MQTT every 5 seconds.

The code was developed with the assistance of chatgpt and undoubtedly needs a good deal of fettling.

Click to see the current state of the code, not yet cleaned up
#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoJson.h> 

// ================= WiFi =================
const char* ssid     = "wifissid";
const char* password = "password1";

// ================= MQTT =================
const char* mqttServer = "192.168.1.11";
const int   mqttPort   = 1883;
const char* mqttUser   = "mqttuser";
const char* mqttPass   = "password1";
// topic "esp32c3/ds18b20" should be declared here

// ================= DS18B20 =================
#define ONE_WIRE_BUS 9   // GPIO 9

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

DeviceAddress sensor1;
DeviceAddress sensor2;

// ================= MQTT Client =================
WiFiClient espClient;
PubSubClient client(espClient);

// ---------- WiFi ----------
void connectToWiFi() {
  Serial.print("Connecting to WiFi...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("\nConnected to WiFi");
}

// ---------- MQTT ----------
void connectToMQTT() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESP32C3-" + WiFi.macAddress();

    if (client.connect(clientId.c_str(), mqttUser, mqttPass)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.println(client.state());
      delay(5000);
    }
  }
}

void mqttCallback(char* topic, byte* payload, unsigned int length) {
  // Not used
}

// ---------- Helper: print sensor address ----------
String addressToString(DeviceAddress deviceAddress) {
  String addr = "";
  for (uint8_t i = 0; i < 8; i++) {
    if (deviceAddress[i] < 16) addr += "0";
    addr += String(deviceAddress[i], HEX);
  }
  return addr;
}

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("BOOTING");

  // WiFi
  connectToWiFi();

  // MQTT
  client.setServer(mqttServer, mqttPort);
  client.setCallback(mqttCallback);
  connectToMQTT();

  // DS18B20
  sensors.begin();

  int deviceCount = sensors.getDeviceCount();
  Serial.print("DS18B20 devices found: ");
  Serial.println(deviceCount);

  if (deviceCount < 2) {
    Serial.println("ERROR: Less than 2 DS18B20 sensors detected!");
  }

  // Get sensor addresses
  if (sensors.getAddress(sensor1, 0)) {
    Serial.print("Sensor 1 ID: ");
    Serial.println(addressToString(sensor1));
  }

  if (sensors.getAddress(sensor2, 1)) {
    Serial.print("Sensor 2 ID: ");
    Serial.println(addressToString(sensor2));
  }

  sensors.setResolution(12); // 12-bit resolution
}

void loop() {
  if (!client.connected()) {
    connectToMQTT();
  }
  client.loop();

  static unsigned long lastPublish = 0;
  if (millis() - lastPublish >= 5000) {
    lastPublish = millis();

    sensors.requestTemperatures();

    float temp1 = sensors.getTempC(sensor1);
    float temp2 = sensors.getTempC(sensor2);

    // Create a JSON document
    DynamicJsonDocument doc(1024);

    // Create an array in the JSON document
    JsonArray sensorData = doc.createNestedArray("sensors");

    // Add sensor data to the JSON array
    JsonObject sensor1Data = sensorData.createNestedObject();
    sensor1Data["sensor"] = addressToString(sensor1);
    sensor1Data["temperature"] = temp1;
    sensor1Data["millis"] = lastPublish;

    JsonObject sensor2Data = sensorData.createNestedObject();
    sensor2Data["sensor"] = addressToString(sensor2);
    sensor2Data["temperature"] = temp2;
    sensor2Data["millis"] = lastPublish;

    // Serialize JSON document to a string
    String output;
    serializeJson(doc, output);

    // Publish the JSON data
    if (output != "[]") {
      client.publish("esp32c3/ds18b20", output.c_str(), true);
      Serial.println("Published sensor data:");
      Serial.println(output);
    } else {
      Serial.println("No valid data to publish.");
    }
  }
}

1 Like

OK that explains the problem, a bad choice of GPIO :grinning:

GPIO9 Input Mode
Low/GND ROM serial bootloader for esptool
High/VCC Normal execution mode

The DS18B20 waits 15–60µs and then pulls the line low for 60–240µs to indicate it is present on the bus, this is likely why you get stuck in flash mode when its connected.

The best pins to use are GPIOs 0, 1, 3, 4, 5, 6, 7, and 10 for general-purpose use, while pins GPIO2, GPIO8, GPIO9, GPIO20, and GPIO21 should be used with caution or avoided due to their roles as strapping pins, onboard components, or default UART communication

2 Likes