Use multiple ds18b20 sensors and calculate average

Hello,

I'am quite new to this scene. And the last week I digged into an arduino project.
I would like to use two ds18b20 sensors and calculate an average of 21 degrees C.
If it's any higher it should turn on a relay.

However for now I came this far that I hooked up one sensor and this one is working.
This is the code I puzzled together...

But for adding a second sensor I am confused what to do.
Apparently you can distinguish the sensors by address or just by number.
should I use a float TempC?
In this code I followed an example from somebody that is using a threshold and calculated the bits.. every single C was a 16 step.

I hope there is somebody who can tweak this one arround...

Greetings Bart

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
LiquidCrystal_I2C lcd(0x27, 16, 2);

OneWire  ds(2); 
#define RELAY1 8
DallasTemperature sensors(&oneWire);
int threshold = 336; //336=equals 21degrees 21=336
int temp;

void setup(void)
{
  sensors.begin();
  Serial.begin(9600);
  // lcd.begin();
  lcd.init ();
  lcd.clear();
  lcd.backlight();

  // setup Relay
  pinMode(RELAY1, OUTPUT);
  digitalWrite(RELAY1, LOW);
  
}

void loop(void)
{
  sensors.requestTemperatures();
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temperature:");
  lcd.setCursor(0, 1);
  lcd.print("C:");
  lcd.setCursor(2, 1);
  lcd.print(sensors.getTempCByIndex(0));

//digitalWrite(relay, HIGH);
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  
  if (!ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }
  
  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  for ( i = 0; i < 9; i++) {           
    // we need 9 bytes
    data[i] = ds.read();
  }
  temp=(data[1]*256)+data[0];
  if (temp>threshold) {
    digitalWrite(RELAY1, HIGH);
    Serial.println("Relay Pin High");
    lcd.setCursor(9, 1);
    lcd.print("ON");
  } else {
    digitalWrite(RELAY1, LOW);
    lcd.setCursor(9, 1);
    lcd.print("OFF");
  }
  
  delay(2000);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

Here is an example of reading from 2 DS18B20 sensors by their index.

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 4
#define TEMPERATURE_PRECISION 9

// 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);


void setup(void)
{
   // start serial port
   Serial.begin(115200);
   Serial.println("Dallas Temperature IC Control Library Demo");

   // Start up the library discover sensors
   sensors.begin();
   //pinMode(2, INPUT_PULLUP);
}


void loop(void)
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      // call sensors.requestTemperatures() to issue a global temperature
      // request to all devices on the bus
      Serial.print("Requesting temperatures...");
      sensors.requestTemperatures(); // Send the command to get temperatures
      Serial.println("DONE");
      // After we got the temperatures, we can print them here.
      // We use the function ByIndex, and as an example get the temperature from the first two only.
      float tempAC = sensors.getTempCByIndex(0);
      float tempBC = sensors.getTempCByIndex(1);

      // Check if reading was successful
      if (tempAC != DEVICE_DISCONNECTED_C && tempBC != DEVICE_DISCONNECTED_C )
      {
         Serial.print("Temperature for the device 1 (index 0) is: ");
         Serial.println(tempAC,1);
         Serial.print("Temperature for the device 2 (index 1) is: ");
         Serial.println(tempBC,1);
      }
      else
      {
         Serial.println("Error: Could not read temperature data");
      }      
   }
}
1 Like

No No No

I don't get it :slight_smile:
I managed to find the individual addresses off the sensors.
but I am to much of a noob to write the right commands.

I puzzled my code together .. but honestly I don't quite get wat is happening after the digitalwrite of the relay. ... I thought I used bits instead of degrees.. or using bits do calculate the degrees. But what happens with this piece after adding a second sensor.

I think I make it to difficult

I reckon you are dead right about that - and the other guy muddied your waters with technobabble. The comments in the code rather suggests he just fartarsing around.

Degrees are just numbers and you can take the average of two just like I assume you did at school. The example for using 2xDS18B20 in reply #3 is all you need, and probably more. I'm very suss about the whole idea of having two ds18B20s doing the same job and suspect the result you will get from averaging them is no more reliable than a single sensor.

@b-art86

The following revised sketch of yours could be helpful! (LCD related codes are deleted for simplicity.)

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);

OneWire  ds(2);
#define RELAY1 8

DallasTemperature sensors(&oneWire);


void setup()
{
  sensors.begin();
  Serial.begin(9600);

  pinMode(RELAY1, OUTPUT);
  digitalWrite(RELAY1, LOW);
}

void loop()
{
  sensors.requestTemperatures();
  float temp1C = sensors.getTempCByIndex(0);
  float temp2C = sensors.getTempCByIndex(1);
  int tempC = (int)(temp1C + temp2C) / 2;
  Serial.println("==================");
  Serial.println(tempC);
  //-----------------
  if (tempC > 21)
  {
    digitalWrite(RELAY1, HIGH);
  }
  delay(2000);
}
1 Like

Why is there "void" as argument in the setup() function while the IDE gives the following?

void setup() {

In that statement the void is assumed. Empty parenthesis or void means that the function takes no arguments. I guess that somewhere in the Arduino compilation process the void is inserted if missing. The example code that I modified was written that way. I normally do not include the void if the function takes no arguments.

1 Like

@GolamMostafa & @groundFungus - Hello (I think sir :smile: ) I want to thank you for helping me out. Youre code is sufficient and works!! I just tried it!
I added the Lcd again and added an else statement, because the Relay had to switch off again too.
Great! This code is just clean and easy to follow. Now I can move on!

@Nick_Pyner - I do need 2 probes, because in my backyard pond I want to be able to meassure on different spots and debts the actual temp. Fore the trout need to swim in water below 23degrees C. Otherwise there won't be enough O2 in the water.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.