Unable to get DS18B20 to read on Nano ESP32

Hi everyone,
I decided to upgrade to Nano ESP32 to take advantage of ESP-Now capabilities. Unfortunately, the Dallas Thermistors do not respond once connected to the new board. Sketch works fine on Nano Every, but all I get is -196.6 for each thermistor on the Nano ESP32. Has anyone else seen this issue and do you have a work around to solve this problem? Thanks your insight is much appreciated.

Hi @kmaher4

welcome to the arduino-forum

after registering you have been presented a link to the
how to get the best out of this forum

You should take 20 minutes of your precious time to read it.
It will make you finish your project a minimum of 200 minutes faster.

You are working on an informatic project. And what is most needed in an informatic project is: information
If you look up the datasheet of the Arduino Nano ESP32

to what IO-pin did you connect the DS18B20-sensors?

You should post your complete sketch as a code-section like described here

best regards Stefan

1 Like

Hello kmaher4

Welcome to the worldbest Arduino forum ever.

Do the logic voltages used for the hardware components match?

Have a nice day and enjoy coding in C++.

I also had a problem with the DS18B20 and fixed it using the following which adds IRAM_ATTR to read_bit() write_bit() to solve timing issues caused by a cache miss.

1 Like

Thank you Stefan! Your post was extremely helpful.
I am have been using arduino hardware and environment for the past three years and have learned a lot. This was my first time posting tot he forum

To the community, I apologize for leading you into the mine without a flashlight or a map. Here is detailed information on how I am using the Nano ESP32 board along with the Sketch I am using successfully on a Nano Every.

  • Current Board -- Nano ESP32. Powered by USB and connected to computer. I was able to swap out Every for ESP32 on the bread board and maintain pin config.
  • two DS18B20's powered by 3.3v Pin with data line attached to Pin D2
    along with the requisite 4.7K resistor bridged to the 3.3 v power
  • OLED display - powered by 3.3v pin and with SCL and SDA attached to pins A4 and A5,respectively
  • Two Led indicator lights on D11 and D12
  • Library versions:
    • OneWire.h ver 2.3.7
    • DallasTemperature ver 3.9.0

Everything works as before except the values from each of the thermistors read -196.6

Here is my code below:

//Set up libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h> // for display
#include <Adafruit_SSD1306.h> // for display
#include <OneWire.h>
#include <DallasTemperature.h>


#define ONE_WIRE_BUS 2  // Data wire is plugged into digital pin 2 
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire);// Pass oneWire reference to DallasTemperature library

#define WIRE Wire  //configure I2C wire communications 

Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &WIRE); //configure OLED display

//configure Flow meter  
 #define HzPerLiter 5.5       //avg 5.5 pulses per liter 
 #define Pulses2Liter 330.0     //pulses per liter total
 #define interval 5000.0     // interval for reporting in ms. divide by 1000 to get seconds  
 
 //global variables 
  const byte intrPin = 3;       // define interupt pin as #3
        
 float tempSensor1, tempSensor2; //declare temp sensors. ID to be read 
 by index in Get Temperatures routine
  int Caution_YellowLED = 12; 
  int Warning_RedLED = 11;
  float SetPointYellow = 37.5; //degrees F 
  float SetPointRed = 36; // degrees F
 
  bool run_flag = false;       // used to flash on board LED
  unsigned long FlowTime = millis(); // variable to track time for flow meter 
  float LPerMin = 0 ;  //liters per min variable
  float GPM = 0 ;      //gallons per min variable
  volatile int rate_cnt = 0;   //RATE of pulses(Hz)  --> L/min  resets every cycle
  unsigned long total_cnt = 0; //Total Counts 
  unsigned long total_sec = 0;  //total seconds since start +=interval/1000
  unsigned long total_min = 0;  //total minutes since start =total sec/60
  float TotLit = 0;    //total Liters  = total count/pulse 2 liter
  float TotGal = 0;    //total gallons
  float BTUexch = 0;   //BTU exchanged
  float Tot_Btu=0;     //total Btu's exchanged    
  int header = 0;      // counter for flag to print headers
  char tbp[65];
 
void setup() {
 sensors.begin();       // Start up the library for the thermistors 
 Serial.begin (9600);   // initialize serial port
  
// PRINT FILE INFO loaded on board  
  delay(5000); // 5 second delay to allow time to open monitor to catch file name  
  Serial.println("Sketch Information   "); Serial.println(F(__FILE__ "    " __DATE__ "   " __TIME__));
  Serial.println();

  pinMode(Caution_YellowLED, OUTPUT);
  pinMode(Warning_RedLED, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT); // output mode for on board LED
  pinMode(intrPin, INPUT_PULLUP);  //set interrupt pin and internal resistor
  attachInterrupt(digitalPinToInterrupt(intrPin),Pulse_cnt,FALLING);  // call subroutine 

//set up and test display
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
  Serial.println("OLED begun");
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen internally, this will display the splashscreen.
  display.display();
  delay(1000);
  // Clear the buffer.
  display.clearDisplay();
  display.display();
  // set OLED text font to display results 
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  
  //flash LED to verify working 
  digitalWrite(Warning_RedLED, HIGH); // Initialize to blink LED on and off  
  delay (500);
  digitalWrite(Warning_RedLED, LOW);
  digitalWrite(Caution_YellowLED, HIGH);
  delay (500);
  digitalWrite(Caution_YellowLED, LOW); // Initialize LED to off
  
  Serial.print("Water Flow - Approximate (Counts Exits)   ");
  sprintf(tbp, "Reports every %d seconds", round((interval+10000)/1000)); // format comment
  Serial.println(tbp);         // Print formatted comment 
  Serial.println();
  }

  void Pulse_cnt() {       //count pulses for Hall Effect 
    rate_cnt++;           //pulses per time 
    total_cnt++;          // total pulses 
    run_flag = true;      // On for LED 13 if meter turning 
 }

 void GetTemperatures(){ //request temperatures from probes 
  sensors.requestTemperatures(); // ordered according to Hex address 
  tempSensor1 = sensors.getTempCByIndex(0)*1.8+32; // Gets temperature value thermistor based on index, converts to F 
  tempSensor2 = sensors.getTempCByIndex(1)*1.8+32; // Gets temperature value thermistor based on index, converts to F 
 }
 
void Write_2_Serial(){ // write to serial monitor with new header at 20 lines 
   if(header == 0) {
  Serial.println ("   EWT     LWT     Instant Flow    BTU exchanged            Totals since Startup    ");
  Serial.println ("    F       F          GPM          Kbtu/hr                   Gallons    Minutes");
  }
  Serial.print("  ");Serial.print(tempSensor1);//EWT
  Serial.print("   ");Serial.print(tempSensor2);//LWT 
  Serial.print("        ");Serial.print(GPM);Serial.print("           ");Serial.print(BTUexch); 
  Serial.print("                     ");Serial.print (TotGal);Serial.print("         ");Serial.println (total_min);
  header++; // increment counter to track when to print header on screen 
  if(header == 20) {
  header = 0; //reset header 
  }
 }

void Write_2_Display () { // light warning LED and write values to OLED Display - 2 pages worth
  //check for conditions to light LED's
  if (tempSensor2 < SetPointRed) {digitalWrite(Warning_RedLED, HIGH);}
  if (tempSensor2 < SetPointYellow && tempSensor2 >= SetPointRed) {digitalWrite(Caution_YellowLED, HIGH);}
  //page 1
  display.print("EWT  ");
  display.println(tempSensor1);
  display.print("LWT  ");
  display.println(tempSensor2);
  display.setCursor(0,0);
  display.display(); // actually display all of the above
  delay (2000);// delay to hold page 
  //page 2
  display.clearDisplay();//clear screen 
  display.display();
  display.print("GPM  ");
  display.println(GPM);
  display.print("BTU  ");
  display.println(BTUexch);
  display.println(tempSensor2);
  display.setCursor(0,0);
  display.display(); // Display page 2
  // clear display
  delay(2000); //refresh every 5 seconds
  display.clearDisplay();//clear screen 
  display.display(); 
  digitalWrite(Warning_RedLED, LOW);digitalWrite(Caution_YellowLED, LOW); // turn off LEDs
 }

void loop () {
  GetTemperatures(); // Send command to the sensors to retrieve data and do temperature conversion
  
  rate_cnt = 0;      //  reset flow rate counter every interval cycle
   while(millis() - FlowTime <= interval){ // wait loop for interval cycle timing      
    //can add programming stuff in here 
    digitalWrite(LED_BUILTIN, LOW); // set run LED to OFF
    if(run_flag){
      digitalWrite(LED_BUILTIN, HIGH); // set run LED to ON
      run_flag = false;
    }
  }
  //Calculate flow values 
  //digitalWrite= (LED_BUILTIN, LOW); // set run LED to OFF
  total_sec += interval/1000;         // increment total seconds
  total_min = round(total_sec/60);    // calculate total minutes 
  LPerMin = (rate_cnt/(HzPerLiter*(interval/1000)));   // pulses divided by meter parameters
  TotLit = total_cnt/Pulses2Liter;                   //calc total liters 
  GPM = LPerMin * 0.264172; //GPM 
  TotGal = TotLit * 0.264172; 
  BTUexch = (tempSensor2-tempSensor1)*GPM*500/1000; // calculate instantaneous KBtu

  Write_2_Serial(); // send data to Serial Monitor
  Write_2_Display(); // send data to OLED 
  
  FlowTime = millis(); // get current "time" to start new cycle for flow meter
 }type or paste code here

I suspect it might have something to do with the libraries not updated for the Nano ESP32?? I have spent hours searching for answers to no avail. Thank you to the community for your insight and your help is much appreciated!

1 Like

The IO-pin numeration on ESP32's is odd.
In the line above you define as IO-pin number 2.
But D2 ist not GPIO2

If you take a look at the PIN-Out


The IO-Pin named D2 is GPIO 5
So you either name it

#define ONE_WIRE_BUS D2

or

#define ONE_WIRE_BUS 5

best regards Stefan

1 Like

No way.
The "pin" with the red outline (above) is, simply 2 (it is not D2 or GPIO5).

Can you please explain why the official Arduino-Store website provides a datasheet that shows the pinout as I have posted it and all the IO-pin-numbering is wrong???
image

First page of the datasheet at

https://docs.arduino.cc/resources/datasheets/ABX00083-datasheet.pdf

page 14 of the document at

https://docs.arduino.cc/resources/datasheets/ABX00083-datasheet.pdf

??

I didn't say that's wrong.
The "D#" and/or "GPIO#" dumpster fire belongs to the NodeMCU world - it's not part of the Nano ESP32 program.
Just like with a 'classic nano' you do not digitalWrite(D2, HIGH) - you don't do that here either.

So at least this line of code should work

Assuming the connection is made at the pin marked D5 then I would expect as much.

image

Thank You Stefan. Changing it to 5 as in reference to GPIO5 worked! Problem solved.

I have to say it's a bit confusing. My references for the LED's at 11 and 12 work fine.., Not sure why the OneWire is requiring the GPIO # ?
I am grateful for the help. Thank you

Where exactly is the connection to??

I have the One wire Bus connected to Physical D2 on the board, but in the sketch, it needs to be referenced by GPIO 5 in order to work.

Meanwhile, I also have LED's physically connected to D11 and D12, and they are quite happy being referenced as 11 and 12 in the sketch. A bit confusing, but I am now back on track. Thanks!

1 Like

@lburelli
Comments?

I think you guys have pretty much figured out what the original problem was but I think it is worth clarifying for anyone else that has the same issue and comes across this thread for help.

The digital pin out posted by StefanL38 is color coded for two different programming languages, orange for c/c+ and yellow for micropython and the two cannot be mixed. When you use a digital IO pin in your program you must remove the D when programming in c and remove the letters GPIO when programming with micropython

@sumguy
Well the chap says that he has a combination of situations.

Seems fishy.

@runaway_pancake yes it does seem confusing but the way the Nano cheat sheet describes it is how it works for me using micropython.

EDIT I just had a look through some examples, it's hard to cover every scenario but I did find a micropython example that used GPIO5 (which for c would be D2) and it works just fine, the example assigns GPIO5 to chip select in a SDCard script.

sd=machine.SDCard(slot=2,cs=5)

@sumguy
Does your micropython do 11, 12 or GPIO 38,47?
Again, our friend here says he has LEDs on D11 and 12 that he digitalWrites to as 11, 12. But for some reason the DS18B20 wants to be connected to "D2" but programmed as 5. (His posted sketch doesn't look like micropython.)

When I digitalWrite to 5, then "D5" goes on/off as directed.

For clarification, I am using Arduino IDE 2.2.1. It doesn't make any sense to me why physical D2 would have to be called using the GPIO 05 designation for the OneWire. Its above my ability to understand what's going on , but Stefan's suggestion of using 5 worked.