Two ds18b20 sensors on different pins

hello, I have been working on a code in order to use two ds18b20 sensors by their address on different pins but the problem I have is, that code shows its true temperature and -127 together. I attach the code and the result
(-127 as you can see in the code is used whenever the sensor cant show true temperature.)
Capture132444444444444444444444444444444444

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS_1 2
#define ONE_WIRE_BUS_2 4

OneWire oneWire_in(ONE_WIRE_BUS_1);
OneWire oneWire_out(ONE_WIRE_BUS_2);

DallasTemperature sensor_inhouse(&oneWire_in);
DallasTemperature sensor_outhouse(&oneWire_out);

//uint8_t sensor1[8] = { 0x28, 0xF5, 0x0A, 0x56, 0xB5, 0x01, 0x3C, 0xBB };
uint8_t sensor2[8] = { 0x28, 0x7D, 0xC6, 0x56, 0xB5, 0x01, 0x3C, 0xF3 };
uint8_t sensor3[8] = { 0x28, 0x99, 0x34, 0x79, 0xA2, 0x00, 0x03, 0xC2};
void setup(void)
{
Serial.begin(9600);
Serial.println("Dallas Temperature Control Library Demo - TwoPin_DS18B20");

sensor_inhouse.begin();
sensor_outhouse.begin();

}

void loop(void)
{

Serial.print("Requesting temperatures...");
sensor_inhouse.requestTemperatures();
sensor_outhouse.requestTemperatures();
Serial.println(" done");

// Serial.print("Sensor 1: ");
// printTemperature(sensor1);

Serial.print("Sensor 2: ");
printTemperature(sensor2);

Serial.print("Sensor 3: ");
printTemperature(sensor3);

Serial.println();

delay(3000);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC1=sensor_inhouse.getTempC(deviceAddress);
float tempC2=sensor_outhouse.getTempC(deviceAddress);

Serial.print(tempC1);

Serial.println(tempC2);

}

Any particular reason you are using 2x 1-wire busses?

Have you got the pull up resistors on the DS18B20 data line?

Your printTemperature routine asks both oneWire busses to read a temperature for the same device address. Since the addresses are unique, only one bus can have the sensor you're looking for. The other call will fail and apparently returns -127 as a way to say "I can't find it".

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC1 = sensor_inhouse.getTempC(deviceAddress);
  float tempC2 = sensor_outhouse.getTempC(deviceAddress);

  if (tempC1 > -126.0)
    Serial.print(tempC1);

  if (tempC2 > -126.0)
    Serial.print(tempC2);

  Serial.println();
}

thanks for your reply.
Actually, this code is preliminary.
I have a project in that I have to use 12 sensors I tried using one pin and I couldn't read all temperatures so I am going to use 6 sensors on each pin (I tried 6 sensors on one pin and I could read all temperatures in the same time.)
and about your second point, yeah I did that.

thanks for your reply.
what should I do?

Reading back my post it didn't come out quite like I thought. When using several DS28B20 sensors on one bus/pin, you only need a single 4K7 pullup resistor.

You may have come across this information:

which might help.

There are several things. You could figure out the problem with your wiring that's limiting you to six sensors - how long is the bus?

You could use John's suggestion and check for -127 whenever you attempt to read.

You could keep arrays which tell you which sensors are on each bus.

thanks,I will try it.

thanks for your reply. I will try the solutions.

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