Page 1 of 1

ESP8266 NodeMCU v3 and DS18B20: Sensors not detected

Posted: Wed May 08, 2024 11:36 am
by TomaszFilipek
Hello,

I have a WiFi module ESP8266 + NodeMCU v3 to which I have connected two DS18B20 sensors to pin D7.

For some reason, the sensors are not being detected.
Please advise why.

  1. #include <Arduino.h>
  2. #include "TemperatureSensorController.cpp"
  3.  
  4. TemperatureSensorController temperatureSensorController;
  5.  
  6. void setup()
  7. {
  8.   Serial.begin(115200);
  9.  
  10.   temperatureSensorController = TemperatureSensorController(D7);
  11. }
  12.  
  13. void loop()
  14. {
  15.   temperatureSensorController.printTemperatures();
  16.  
  17.   delay(1000);
  18. }
  1. #include <DallasTemperature.h>
  2. #ifndef MYCLASS_H
  3. #define MYCLASS_H
  4.  
  5. class TemperatureSensorController
  6. {
  7. public:
  8.     TemperatureSensorController()
  9.     {
  10.     }
  11.  
  12.     TemperatureSensorController(uint8_t oneWireBus) : TemperatureSensorController()
  13.     {
  14.         _oneWire = OneWire(oneWireBus);
  15.         _sensors = DallasTemperature(&_oneWire);
  16.     }
  17.  
  18.     void printTemperatures()
  19.     {  
  20.         Serial.println("Print Temperatures");
  21.         Serial.println("");
  22.  
  23.         delay(100);
  24.  
  25.         _sensors.begin();
  26.  
  27.         delay(100);
  28.  
  29.         _sensors.requestTemperatures();
  30.  
  31.         Serial.print("Device count: ");
  32.         Serial.println(_sensors.getDeviceCount());
  33.         Serial.println("");
  34.  
  35.         for (int i = 0; i < _sensors.getDeviceCount(); i++)
  36.         {
  37.             Serial.print("Device no. ");
  38.             Serial.print(i + 1);
  39.             Serial.print(" Temperature: ");
  40.             Serial.print(_sensors.getTempCByIndex(i));
  41.             Serial.println(" °C");
  42.             delay(100);
  43.         }
  44.  
  45.         Serial.println("");
  46.         Serial.println("-----------------------");
  47.         Serial.println("");
  48.     }
  49.  
  50. public:
  51.     OneWire _oneWire;
  52.     DallasTemperature _sensors;
  53. };
  54.  
  55. #endif
Output of the program:
X.jpg
X.jpg (17.3 KiB) Viewed 2320 times


The physical connections are fine - running the code where everything is in the main.cpp file, the readings are done correctly.

  1. #include <Arduino.h>
  2. #include "TemperatureSensorController.cpp"
  3.  
  4. OneWire oneWire = OneWire(D7);
  5. DallasTemperature sensors = DallasTemperature(&oneWire);
  6.  
  7. void setup()
  8. {
  9.   Serial.begin(115200);
  10.  
  11.   sensors.begin();
  12. }
  13.  
  14. void loop()
  15. {
  16.   Serial.println("Print Temperatures");
  17.   Serial.println("");
  18.  
  19.   delay(100);
  20.  
  21.   sensors.begin();
  22.  
  23.   delay(100);
  24.  
  25.   sensors.requestTemperatures();
  26.  
  27.   Serial.print("Device count: ");
  28.   Serial.println(sensors.getDeviceCount());
  29.   Serial.println("");
  30.  
  31.   for (int i = 0; i < sensors.getDeviceCount(); i++)
  32.   {
  33.     Serial.print("Device no. ");
  34.     Serial.print(i + 1);
  35.     Serial.print(" Temperature: ");
  36.     Serial.print(sensors.getTempCByIndex(i));
  37.     Serial.println(" °C");
  38.     delay(100);
  39.   }
  40.  
  41.   Serial.println("");
  42.   Serial.println("-----------------------");
  43.   Serial.println("");
  44.  
  45.   delay(5000);
  46. }
Output:
V.jpg
V.jpg (38.08 KiB) Viewed 2320 times
Thank you in advance for your help.