[SOLVED] Reading DS18B20 in the correct sequence

madepablo: It seems we are doing the same thing in different places...

I'm working on waste heat recovery. This uses 3xDS18B20 + a water turbine. The ski lodge is a source of very cold water, 1.3C

Yes, the rtc battery is an issue i will try to solve in the future.

There was discussion a few days ago about RTC batteries.

About the header, It is not fundamental for me, but it is a requirement.

OK, enough said!

Ok, after some playing, here is the next step.

I readed the ID of each sensor and ordered them in the sequence i want to be readed. Following the recomendations done in the previous posts, each sequence (of each bus) is saved into an array, latter passed to the prodecure to read each id of the array and request the temperature from that sensor.

Here is the arrays declaration just to illustrate it:

uint8_t airsensors[1][8] = {

uint8_t snowsensors[14][8] = {

uint8_t surfacesensors[1][8] = {

uint8_t groundsensors[10][8]={

and this is the code i added int he loop to read each sensor in that sequence, including some error management, and the data save into a file:

    temp = sensors1.getTempC(airsensors[0]);
    if ((temp == -127)||(temp == 85)){
      e = 6;
    for(int i = 0; i<14; i++){     
      temp = sensors2.getTempC(snowsensors[i]);
      if ((temp == -127)||(temp == 85)){
        e = 7;
    temp = sensors3.getTempC(surfacesensors[0]);
    if ((temp == -127)||(temp == 85)){
      e = 8;
    for(int i = 0; i<10; i++){     
      temp = sensors4.getTempC(groundsensors[i]);
      if ((temp == -127)||(temp == 85)){
        e = 9;

My next steps from here to save memory (my code is 26.5K/30.7k):
1.- To create a more simple loop which sould be able to read the four bus (probably, such as you said, by the use of other array containing the busses IDs).
2.- To pass the sensor IDs array to an eeprom (by the use of other code), and made this loop to read the sensors ID from the eeprom.

So, thank you guys for your great comments!

small tip

for(int i = 0; i<10; i++){ // int takes 2 bytes

for (uint8_t i = 0; i<10; i++){ // takes 1 byte uint8_t == unsigned int of 8 bits

for every loop you win a byte, add’s up :wink:

if you look at the addresses of the sensors you see that only byte 1,2,3,and 7 differ and that the other 4 are constant. You could compress the array size and make an "expand function" to convert the 4 bytes to an 8 byte address.

Thanks so much for the tip! Yes, every Byte is gold! so, thanks for the piece of code! About the second tip, I will try to see how to do it, because you are right, it could be more simple in your way. Thanks!


I am trying to made a simpler code. For that reason, i try to apply your idea of use arrays to call the sensors in the different buses.

For that reason, i added this to the initialization section of the code:

OneWire oneWire1(AirTemp);                // Configure library to read air temperature sensor
DallasTemperature sensors1(&oneWire1);
OneWire oneWire2(SnowTemp);               // Configure library to read snow temperature sensors
DallasTemperature sensors2(&oneWire2);
OneWire oneWire3(SurfaceTemp);            // Configure library to read surface temperature sensor
DallasTemperature sensors3(&oneWire3);
OneWire oneWire4(GroundTemp);             // Configure library to read ground temperature sensors
DallasTemperature sensors4(&oneWire4);
DallasTemperature bus[4] = {sensors1, sensors2, sensors3, sensors4};  // Sensors buses in the device
const int sensorsnumber[4] = {1,14,1,10}; // Number of sensors in each bus

and this to the setup in order to check each bus have the addecuate number of sensors:

  for (int i=0; i=3; i++){   // Check if all the sensors in each bus are woking properly
    if ((bus[j].getDeviceCount())<(sensorsnumber[j]));
      Serial.print("error on bus ");

The program compile correctly, but the problem it return infinite messages of “error on bus 30” in the serial port…

If i write this:


but it return 0 for each bus…
So, i think there is something wrong in the bus declaration…

i also wrote this in the initialization section:

DallasTemperature bus[4] = {sensors1(&oneWire1), sensors2(&oneWire2), sensors3(&oneWire3), sensors4(&oneWire4)};

but it does not compile…

So, any idea about where could be the problem will be wellcome!

One small step:

By changing the definition in the loop the sequence error seems to be solved:

for (uint8_t i=0; i<4; i++){  
    if ((bus[i].getDeviceCount())<(sensorsnumber[i])){
      Serial.print("error on bus ");

Now i still have error on getting device count (0) in each bus, but the infinite error call is solved.

After studing the code, i think that the problem is located how the array of buses is defined:

DallasTemperature bus[4] = {sensors1, sensors2, sensors3, sensors4};

I don´t know how to define in other way what could be correct… i tryied this:

DallasTemperature bus[4] = {sensors1(&oneWire1), sensors2(&oneWire2), sensors3(&oneWire3), sensors4(&oneWire4};

But it is not recognized by arduino IDE:

no match for call to ‘(DallasTemperature) (OneWire*)’

So, still working on this.