DS18B20 reads -127 with arduino nano ESP32 [SOLVED]

I have attached a DS18B20 temperature sensor to an arduino nano ESP32. I have tried both a 3.3k pullup resistor and a 4.7k pullup resistor, as well as bridged a 10uF ceramic capacitor across VCC and GND of the DS18B20. I've uploaded a basic code to read the temperature which works fine on a regular arduino nano. This is the code:

#include <OneWire.h>

#include <DallasTemperature.h>

#define ONE_WIRE_BUS 9

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

void setup()
{  

  pinMode(LED_BUILTIN, OUTPUT);

  sensors.setWaitForConversion(false);

  Serial.begin(115200);

  sensors.begin();

  Serial.println("DATA FOR DS18B20 TEMPERATURE SENSOR");

  sensors.requestTemperatures();

  delay(1000);

}

 

void loop()
{ 

  digitalWrite(LED_BUILTIN, HIGH);

  Serial.print("Celsius temperature: ");

  Serial.print(sensors.getTempCByIndex(0));

  Serial.println("°");

  sensors.requestTemperatures();

  delay(1000);

 digitalWrite(LED_BUILTIN, LOW);

 delay(1000);

}

That only read -127, as one wire does not work properly with the nano ESP32 (at least I don't think anyway).

I have also tried the DS18B20 ESP32 library example. This is the code:

// https://github.com/htmltiger/esp32-ds18b20

#include "OneWireESP32.h"
const uint8_t MaxDevs = 2;

float currTemp[MaxDevs];

void tempTask(void *pvParameters){
	OneWire32 ds(13, 0, 1, 0); //gpio pin, tx, rx, parasite power
	// There are 8 RMT channels (0-7) available on ESP32 for tx/rx
	
	uint64_t addr[MaxDevs];
	
	//uint64_t addr[] = {
	//	0x183c01f09506f428,
	//	0xf33c01e07683de28,
	//};
	
	//to find addresses
	uint8_t devices = ds.search(addr, MaxDevs);
	for (uint8_t i = 0; i < devices; i += 1) {
		Serial.printf("%d: 0x%llx,\n", i, addr[i]);
		//char buf[20]; snprintf( buf, 20, "0x%llx,", addr[i] ); Serial.println(buf);
	}
	//end

	for(;;){
		ds.request();
		vTaskDelay(750 / portTICK_PERIOD_MS);
		for(byte i = 0; i < MaxDevs; i++){
			uint8_t err = ds.getTemp(addr[i], currTemp[i]);
			if(err){
				const char *errt[] = {"", "CRC", "BAD","DC","DRV"};
				Serial.print(i); Serial.print(": "); Serial.println(errt[err]);
			}else{
				Serial.print(i); Serial.print(": "); Serial.println(currTemp[i]);
			}
		}
		vTaskDelay(3000 / portTICK_PERIOD_MS);
	}
} // tempTask

void setup() {
	delay(1000);
	Serial.begin(115200);
	xTaskCreatePinnedToCore(tempTask, "tempTask", 2048,  NULL,  1,  NULL, 0);
}


void loop() {}

This code puts my board into bootloader mode (the COM port connects and disconnects at one second intervals) so I can't open the serial monitor. It seems like the code is doing weird things to my board.

Does anyone know how I get my DS18B20 working??

EDIT: I have solved it, even though I have selected arduino pin configurations, the DS18B20 only works with legacy pin configurations. I got lucky, I just randomly shoved the data wire into a different GPIO pin and it was the correct one.

serendipity at its finest

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.