No reading from DS18B20 probe with R4 minima

Hi,

I'm trying a very simple PID-based temperature controller project.
The project relies on a solid-state relay, and DS18B20 temperature probe, and a heating pad.
I use the popular OneWire & DallasTemperature libraries.
I cannot get any reading from the probe - the program outputs -127, which means no data.
I've tried: different ports for the probe; different data-wire pull-up resistors (same resistance value, different resistors, as I thought I had fried them); different probes (all DS18B20, of course); different bread boards; and finally a probe that came with a plug-in terminal that includes the resistor. Nothing worked.
Here is the code I use - is there anything wrong with it? Thank you.

// For PID regulation
#include <PID_v1.h>
// For DS18B20 sensor
#include <OneWire.h>
#include <DallasTemperature.h>
// DS18B20 sensor is on PIN 2 of the Arduino
#define ONE_WIRE_BUS 2
// Solid state relay is on PIN 4 of the Arduino
#define RELAY_PIN 4
// Sensor objects
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// PID variables
double Setpoint, Input, Output;
int WindowSize = 5000;
unsigned long windowStartTime, lastPrintTime;
// PID tuning parameters
double Kp=65, Ki=0.71, Kd=0;
// PID object
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
// Function that controls activation or deactivation
// of the relay. The LED built into the Arduino is on when
// the relay is active, otherwise it is off.
void relay_state(bool state) {
    digitalWrite(RELAY_PIN, state);
    digitalWrite(LED_BUILTIN, state);
}
// Function that print temperature each second
void printTemperatureEverySec(double temp) {
  if ((millis()-lastPrintTime) > 1000) {
    lastPrintTime = millis();
    Serial.println(temp);
  }
}
// Setup function that runs once at startup
void setup()
{
  // Define relay and led pins as output
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
     
  // Set the relay to low state
  relay_state(LOW);
  Serial.begin(115200);
  // Store current time in variables used by PID
  // and display functions
  windowStartTime = millis();
  lastPrintTime = millis();
   
  // Setpoint for the PID temperature control 40 degrees Celcius
  Setpoint = 40;
  // Tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);
  // Turn the PID on
  myPID.SetMode(AUTOMATIC);
}
// Main loop function
void loop()
{
  // Get the temperature from the sensor
  sensors.requestTemperatures();
  Input = sensors.getTempCByIndex(0);
  printTemperatureEverySec(Input);
  // Compute the PID
  myPID.Compute();
  /************************************************
   * turn the output pin on/off based on pid output
   ************************************************/
  if (millis() - windowStartTime > WindowSize)
  {
    //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if ((Output < (millis() - windowStartTime)) || (Input < 0)) {
    relay_state(LOW);
  }
  else
    relay_state(HIGH);
}

Can you upload this program instead

https://github.com/PaulStoffregen/OneWire/blob/master/examples/DS18x20_Temperature/DS18x20_Temperature.ino

But change

OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)

to

OneWire  ds(2);  // on pin 2 (a 4.7K resistor is necessary)

instead. This bypasses a layer of complexity and strips out any of the PID stuff, and gives more direct access and output other than "failed".

There's also https://github.com/PaulStoffregen/OneWire/issues/129 reported but it hasn't got much attention or confirmation.

1 Like

Thank you, that was very helpful!
With the first sketch you pointed to, I do get readings. Must be a bug somewhere in the code I posted (which came from a tutorial website).
Thanks again for confirming that my probes are fine :smiley:

Great!

I've also read that when you give the sensor VCC = 3.3V, you should chose a smaller pullup resistor to avoid CRC errors.

1 Like

I'm wondering though why at no point in time the function

sensors.begin();

is being called?

Did that code really come from a verified tutorial?

You should definitely verify that example DallasTemperature code like

https://github.com/milesburton/Arduino-Temperature-Control-Library/blob/master/examples/Simple/Simple.ino

works.

Thanks again! I use 5V, so I should be fine with the 4.7 kO pull-up.
I did get the code from a tutorial website - but you are right, sensors.begin() was missing!
After adding it to the original code I posted, I do get readings and the relay reacts accordingly.
Thanks a lot!!!