Stumped reading mulitple DS18B20's

Hi, now this should all work as fars as I can tell but all five sensor values are coming out with the same value - ie that of S1..... any idea what daft mistake I have made?

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

float S1;
float S2;
float S3;
float S4;
float S5;

#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

DeviceAddress Sensor1 = { 0x28, 0x64, 0xDB, 0x8B, 0x03, 0x00, 0x00, 0x86 }; 
DeviceAddress Sensor2 = { 0x28, 0x0C, 0x06, 0x8C, 0x03, 0x00, 0x00, 0xD0 }; 
DeviceAddress Sensor3 = { 0x28, 0xCA, 0xD9, 0x8B, 0x03, 0x00, 0x00, 0x4A }; 
DeviceAddress Sensor4 = { 0x28, 0xB6, 0x02, 0x8C, 0x03, 0x00, 0x00, 0x07 }; 
DeviceAddress Sensor5 = { 0x28, 0x11, 0xDA, 0x8B, 0x03, 0x00, 0x00, 0x38 }; 


void setup () 
{        
     sensors.begin();
     
     if (!sensors.getAddress(Sensor1, 0)) error();
     if (!sensors.getAddress(Sensor2, 0)) error();
     if (!sensors.getAddress(Sensor3, 0)) error();
     if (!sensors.getAddress(Sensor4, 0)) error();
     if (!sensors.getAddress(Sensor5, 0)) error();
         
     sensors.setResolution(Sensor1, 12); // set the resolution
     sensors.setResolution(Sensor2, 12); // set the resolution
     sensors.setResolution(Sensor3, 12); // set the resolution
     sensors.setResolution(Sensor4, 12); // set the resolution
     sensors.setResolution(Sensor5, 12); // set the resolution

void loop () 
{
 sensors.requestTemperatures();
  S1 = sensors.getTempC(Sensor1);
  S2 = sensors.getTempC(Sensor2);
  S3 = sensors.getTempC(Sensor3);
  S4 = sensors.getTempC(Sensor4);
  S5 = sensors.getTempC(Sensor5);
}

Where's the code that tells you they're the same - the issue might be there.

Hi Bill, I stripped the code out to make it easy to read, the sensor floats S1 through S5 are simply printed to serial.

This may be your problem:

     if (!sensors.getAddress(Sensor1, 0)) error();
     if (!sensors.getAddress(Sensor2, 0)) error();
     if (!sensors.getAddress(Sensor3, 0)) error();
     if (!sensors.getAddress(Sensor4, 0)) error();
     if (!sensors.getAddress(Sensor5, 0)) error();

I suspect that getAddress gets the address at the given index. Since you've provided the same index in each case, your addresses are being overwritten with the 0th device on the bus. Since you already know the addresses, these calls are superfluous anyway.

Indeed that is the explanation - getAddress re-searches the address space on each call, but skips as many devices as the value of
its second argument - so if you call it with 0 every time it always finds the same device (overwriting the first argument with the
discovered address).

I would comment out all those calls and see what happens.

FAB - thanks folks, you got it.

This seems to work - hadn't twiggged the sensor indexing - I can see it clearly now you have pointed it out :slight_smile:

     if (!sensors.getAddress(Sensor1, 0)) error();
     if (!sensors.getAddress(Sensor2, 1)) error();
     if (!sensors.getAddress(Sensor3, 2)) error();
     if (!sensors.getAddress(Sensor4, 3)) error();
     if (!sensors.getAddress(Sensor5, 4)) error();