Go Down

Topic: Stumped reading mulitple DS18B20's (Read 1 time) previous topic - next topic


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?

Code: [Select]

#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 ()
     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 ()
  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:
Code: [Select]

     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.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


Dec 11, 2012, 09:46 am Last Edit: Dec 11, 2012, 09:55 am by point5 Reason: 1
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 :-)

Code: [Select]

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

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131