DS18B20 Problem

Hi, everyone. Is it possible If someone can tell me if there is an error in my code?

I have an ESP32 with a DHT11 and DS18B20 sensor and a Relay. The DHT11 and Relay are working 100%. But I am not getting any data from the DS18B20 sensor.

#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>


char auth[] = "";

char ssid[] = "";  // type your wifi name
char pass[] = "";  // type your wifi password

BlynkTimer timer;

//DHT11 LIB
#include "DHT.h"
#define DHTPIN 14 
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float t, h;

//DS18B20 LIB
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float soiltemp;

//Relay Pin
#define RelayPin 13

void sendSensor()
{
  //DHT11 Air Temperature and Humidity
  h = dht.readHumidity();
  t = dht.readTemperature();  
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);

  if (isnan(h) || isnan(t)) {
    Serial.println("No Reading from DHT sensor!");
    return;
  }

  //Soil Tempreture
  DS18B20.requestTemperatures();
  soiltemp = DS18B20.getTempCByIndex(0);
  Serial.println(soiltemp); 
  Blynk.virtualWrite(V2, soiltemp);

  //Heat Mat relay on and off with DS18B20
  pinMode(RelayPin, OUTPUT);
  //Relay ON
  if(soiltemp < 21){
    digitalWrite(RelayPin, HIGH);
    Serial.println("Heat Mat ON!");
    Blynk.logEvent("heat_mat_on", "Soil Temperature is under 21°C, Heat Mat is ON!");
  }

  //Relay OFF
  if(soiltemp > 26){
    digitalWrite(RelayPin, LOW);
    Serial.println("Heat Mat OFF!");
    Blynk.logEvent("heat_mat_off", "Soil Temperature is over 26°C, Heat Mat is OFF!");
  }
  
}

void setup()
{   
  
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  DS18B20.begin();
  delay(2000);
  timer.setInterval(1000L, sendSensor);
 
  }

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
}

@Jean.K21 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Thank you Pete. I have done so.

I am getting a -127.00°C reading on the DS18B20 sensor. Can it be a faulty sensor?

-127 is the default reading if the sensor isn’t found or isn’t responding.

I’d suggest that you try a different et pin, and maybe add a pullup resistor to the sensor’s data pin.

Pete.

Thank you. I will give it a try. Much appreciated.

Definitely need a pullup on the 18B20. Between 1k and 4k. If the wire is longer than 3 or 4 feet, go to the lower value.

Thank you ScottB. Much appreciated

2022-05-29_202211

Is the DS18B20 Probe Temperature sensor working in the same way?

Yes.
IMG_20220529_230039
You need to add 4.7k pull-up resistor between the signal and power pin to keep the data transfer stable.

Thank you John93

It’s the same sensor

Please check

https://github.com/milesburton/Arduino-Temperature-Control-Library/issues/219 

It’s something to be aware of, to look forward to.
Cheers

And a related Gotcha. If using a DSP18B20 in normal (not parasitic) mode, as shown above, which requires the pullup resistor from the data line to VCC and an ESP32 processor do not use GPIO12. Pulling GPIO12 High can inhibit the firmware load and throw an error along the lines of “rst:0x10 (RTCWDT_RTC_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT) | flash read err, 1000”

Others to avoid are GPIO6 through to GPIO11.
Forewarned etc
Cheers

@Argus.Tuft have you noticed anything odd about your posts compared to those from other people?

Triple backticks (that’s the ``` characters that you are starting and ending every post with) are used to make C++ code display correctly, and things like serial output and compiler error messages more readable.

They are not required for normal conversational posts, and as you can see, they make your posts much harder to read.
I’d suggest that you just use triple backticks where they are needed.

Pete.

Sorry Mate, I’m new to this game.
I didn’t read your note to @Jean.K21 carefully enough.
One’s never done lerning eh?
Cheers