Digital temp sensor not returning correct value when other sensors are read.

I’ve got a Vernier Interface Shield hooked up to an Arduino Mega. The Interface Shield lets me use Vernier science probes with the Arduino.

One of the sensors is a DS18B20 temperature probe. The sensor returns a correct value only when I comment out all of the other working code for the other sensors, but returns an erroneous value when the code isn’t commented out.

Please see the comments in the code for the description of the problem. Not quite sure why the temp probe would be reporting good values only when the rest of the code in loop() is commented out.

#include <OneWire.h>
#include <DallasTemperature.h>
/********************************************************************/
// Data wire for the DS18B20 temp sensor is plugged into pin 37 on the Arduino 
#define ONE_WIRE_BUS 37 
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices  
// (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS); 
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
/********************************************************************/ 

char outBuffer[32];

int temp1 = 37;
int floatSwitch1 = 31;
int floatSwitch2 = 33;
int floatSwitch3 = 35;
int analog0 = A0;
int analog1 = A1;
int analog2 = A2;
int analog3 = A3;
int analog4 = A4;
int analog5 = A5;

void setup() {
  Serial.begin(115200);
  pinMode(temp1, INPUT_PULLUP);
  pinMode(floatSwitch1, INPUT_PULLUP);
  pinMode(floatSwitch2, INPUT_PULLUP);
  pinMode(floatSwitch3, INPUT_PULLUP);
  pinMode(analog0, INPUT);
  pinMode(analog1, INPUT);
  pinMode(analog2, INPUT);
  pinMode(analog3, INPUT);
  pinMode(analog4, INPUT);
  pinMode(analog5, INPUT);
  sensors.begin();
}

void loop() {
     
  // read the value from the temperature sensor:
  sensors.requestTemperatures();
  Serial.println(serialOutput("T1", sensors.getTempFByIndex(0)));
  delay(2000);

  // if I comment out all remaining code below in loop(), the temp sensor above returns the correct value ie. <T1:73.06>
  // if I leave the code as it is, the other sensors return correct values, but the temp sensor does not anymore ie. <T1:-196.60>.
  // even if I unplug the wire from pin 37, the reading still returns as <T1:-196.60>
  Serial.println(serialOutput("A0", analogRead(analog0)));
  delay(2000);
  Serial.println(serialOutput("A1", analogRead(analog1)));
  delay(2000);
  Serial.println(serialOutput("A2", analogRead(analog2)));
  delay(2000);
  Serial.println(serialOutput("A3", analogRead(analog3)));
  delay(2000);
  Serial.println(serialOutput("A4", analogRead(analog4)));
  delay(2000);
  Serial.println(serialOutput("A5", analogRead(analog5)));
  delay(2000);
  Serial.println(serialOutput("FS1", digitalRead(floatSwitch1)));
  delay(2000);
  Serial.println(serialOutput("FS2", digitalRead(floatSwitch2)));
  delay(2000);
  Serial.println(serialOutput("FS3", digitalRead(floatSwitch3)));
  delay(2000);

}

char *serialOutput(const char *sensor, const float reading) {
  char result[5];
  dtostrf(reading, 4, 2, result);
  sprintf(outBuffer, "<%s:%s>", sensor, result);
  return outBuffer;
}
char result[5];

This isn’t big enough for the strings generated by the temperature sensor and perhaps not for the other devices either.
T1:73.06 needs 5 characters PLUS one for the null on the end.
T1:-196.60 needs 7 characters PLUS one for the null on the end.
Change the declaration to:

char result[16];

which will give it plenty of room.

  // even if I unplug the wire from pin 37, the reading still returns as <T1:-196.60>

That’s because with nothing connected, the pin will usually read as all ones which gives a temperature of -127 Celsius which is -196.6 Fahrenheit.

Pete

el_supremo:

char result[5];

This isn’t big enough for the strings generated by the temperature sensor and perhaps not for the other devices either.
T1:73.06 needs 5 characters PLUS one for the null on the end.
T1:-196.60 needs 7 characters PLUS one for the null on the end.
Change the declaration to:

char result[16];

which will give it plenty of room.

  // even if I unplug the wire from pin 37, the reading still returns as <T1:-196.60>

That’s because with nothing connected, the pin will usually read as all ones which gives a temperature of -127 Celsius which is -196.6 Fahrenheit.

Pete

Any idea why commenting out the other sensor code allows the temperature to be read correctly?

Because one of them is writing off the end of the result array and damaging memory that’s related to the temperature sensor. Does it work when you apply Pete’s suggestion?

MorganS:
Because one of them is writing off the end of the result array and damaging memory that's related to the temperature sensor. Does it work when you apply Pete's suggestion?

Interesting. I'm away from the device at the moment. Will try it when I get back.

So what exactly happens when you try and stuff things that are too big to fit into a char array of X length? You said that it can physically damage memory?

Not, not physically damage. It can write into memory that is being used for another purpose - e.g. the return address from a function call or space occupied by another variable which will then have an incorrect value. It is hard to predict exactly what will happen in any given instance but it usually results in a disaster of one sort or another.

Pete