0

I am using the below code to check temperatures from 36 DS18B20 sensors using Arduino Mega 2560. If any one of them go above 50 deg. celsius, then it sets the output pin to HIGH.

I am trying to run the code on Proteus 8.13, but during the same run, sometimes the virtual terminal prints -127 deg. celsius for all, sometimes right for some sensors and wrong for the other ones (usually the wrong value is also 85 deg. celsius). Is it something wrong with the simulator, or my code?

    #include <OneWire.h>
    #include <DallasTemperature.h>
    
    #define NUM_SENSORS 36
    #define START_PIN 14
    #define OUTPUT_PIN 2
    
    OneWire oneWire[NUM_SENSORS];
    DallasTemperature sensors[NUM_SENSORS];
    
    void setup() {
      Serial.begin(9600);
    
      for (int i = 0; i < NUM_SENSORS; i++) {
        oneWire[i] = OneWire(START_PIN + i);
        sensors[i] = DallasTemperature(&oneWire[i]);
        sensors[i].begin();
      }
    
      pinMode(OUTPUT_PIN, OUTPUT);
      digitalWrite(OUTPUT_PIN, LOW);
    }
    
    void loop() {
      float celsius[NUM_SENSORS];
    
      for (int i = 0; i < NUM_SENSORS; i++) {
        sensors[i].requestTemperatures();
        celsius[i] = sensors[i].getTempCByIndex(0);
      }
    
      bool aboveThreshold = false;
    
      for (int i = 0; i < NUM_SENSORS; i++) {
        if (celsius[i] > 50.0) {
          aboveThreshold = true;
          break; 
        }
      }
    
      digitalWrite(OUTPUT_PIN, aboveThreshold);
    
      printSensorData(celsius);
    
      delay(1000);
    }
    
    void printSensorData(float celsius[]) {
      for (int i = 0; i < NUM_SENSORS; i++) {
        Serial.print("Sensor ");
        Serial.print(i + 1);
        Serial.print(": ");
        Serial.print(celsius[i]);
        Serial.print(" C  ");
        Serial.print(sensors[i].toFahrenheit(celsius[i]));
        Serial.println(" F");
      }
    }
1
  • +85 degC is the value returned by these sensors after a power-on/reset (of that sensor) - maybe not so coincidentally. Could the device(s) that returned that value have had a (simulated) power interruption?
    – JRobert
    May 13 at 21:35

1 Answer 1

0

The -127 indicates a fault on the hardware side, generally an open circuit. Also it sounds like your pull up resistor is not installed or way out of range.For the Mega. UNO and other 5V units 4.7K is a good size and 3.3K for the 3V3 systems. Note the resistor goes from VCC to the sensor output.

2
  • 1
    DS18B20 is using one-wire protocol, no SDA, nor SCL.
    – hcheung
    Aug 16, 2023 at 12:11
  • It appears that each sensor is on a separate pin - 36 1-Wire busses, as it were. If that is correct, is there a reason you haven't put the sensors on just a few 1-Wire busses? It would require many fewer pullup resistors and there will be many fewer mechanical connections to fail. There is an helpful application-note, "Guidelines for Reliable 1-Wire Networks" that discusses how to make reliable large networks and what the limits are.
    – JRobert
    Aug 19, 2023 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.