Go Down

Topic: 1-Wire, DS18B20... How do I search the bus? (Read 25376 times) previous topic - next topic

DingbatCA

I have 5 DS18B20's hooked up to my Arduino.  I am trying to scan the bus once, store all the addresses, then move on in the code. I cant even seem to get the basic scan to give me the 5 addresses. It works fine with up to 3 devices, any more and it endless loops.  It never finishes.  Any ideas?
Code: [Select]


void loop(void)
{
 byte i;
 byte present = 0;
 byte done = 0;
 byte data[12];
 byte addr[8];

 while ( !done )
 {
   if ( ds.search(addr) != 1)
   {
     Serial.print("No more addresses.\n");
     ds.reset_search();
     done = 1;
     return;
   }
   else
   {
     Serial.print("Unique ID=");
     for( i = 1; i < 7; i++)
     {
       Serial.print(addr[i], HEX);
       Serial.print(":");
     }
     Serial.println();    
   }
   delay(100);
 }
}


From the console out:
Code: [Select]

Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:
Unique ID=DE:E3:F6:1:0:0:
Unique ID=9E:C1:F6:1:0:0:

Waffle

There is a bug in the OneWire library that creates the problem you are experiencing. The best way around this is to find the addresses of all the sensors one by one, and directly use these addresses to interface with your sensors.


Waffle

I played around with the library to see I could find the bug, but I don't have enough 1-Wire devices to actually reproduce this problem. So I'll need your help. Could you see if the following change fixes it? Just open the library's OneWire.cpp file, and comment out line 268.

blalor

What's the bug?  Where's it detailed?  I ran into a problem where I was unable to resolve more than three devices on the bus, but I thought that was due to a poor wiring job on my part (I have DS18B20s running all over my house with some hefty wiring runs).  I was getting CRC errors when scanning the bus.  Is that what's happening here?  I see that the sample code above isn't performing a checksum...

Waffle

To my knowledge nobody has ever posted what the bug is exactly. I've made a best guess by "simulating" a bunch of 1-Wire devices in C# and ported the OneWire library search code across and debugged it there. In my simulation commenting out that line makes the search algorithm work (otherwise it loops between two devices in the same way shown in the first post of this thread).

I don't believe this bug has anything to do with CRC errors. That must be a different issue. Maybe you need try a different pull-up resistor? Try a 2k or 1.5k resistor (don't go lower than this).

blalor

I got around the problem by using two 1-Wire buses. :)

Ward

I have experienced the same problem with search not terminating when there are more than four devices on the wire. Last night I put together a test system, demonstrated it has the same problem, studied the signaling with a scope, and tracked the problem to what appears to be a misplaced statement, line 268.

The logic is complex enough that I want to go through it again with a clear head. I would like to know more about Waffle's simulation. What search tree configurations did you simulate? I ask because I suspect some missing flag initialization which removing line 268 does not address.

Kudos to Waffle, and to my friend Wayne, for their assistance tracking down this problem.

Rusty in Texas

#8
Apr 27, 2009, 02:10 am Last Edit: Apr 27, 2009, 02:11 am by Fe2o3Fish Reason: 1
FWIW, I currently have 4 1-wire devices hooked up to my Arduino,
2 1820's and 2 18B20's and have not had a single problem although
the distance between the Arduino and my sensors is about 6-inches (15-cm).
 [Yeah, I know, not very useful 'cept for testing the sensors]

Longer runs will probably require a stronger pullup resistor (lower
resistance) on the data line (I'm using the standard 4K7-ohm resistor)
and probably low-capacitive cabling like CAT-5 or 5e.  Longer runs
may possibly preclude the use of parasitic power and require Vcc to
be brought down the cable as well.

Dallas Semi (now part of Maxim IC) has a number of documents with
regards to longer 1-wire network cabling if that's what you're dealing
with.

Ward

Rusty -- Can you hook up a couple more devices and see if search still works? I found that the OneWire search code would fail when adding the 5th or 4th device, depending on which devices were already on the bus. I assume this is because the particular id bit patterns lead the search algorithm toward or away from the faulty logic. Perhaps having two different device codes is protecting you from the faulty logic too. -- Ward

Rusty in Texas

#10
Apr 28, 2009, 03:11 am Last Edit: Apr 28, 2009, 03:15 am by Fe2o3Fish Reason: 1
Ward,
   I've dumped 4 1820's and 2 18B20's onto my Arduino.  I'm having no
problems with the search algorithm although search() is only called
after a system reset/powerup.  Parasitic power is NOT used and the
data line has a 4k7 ohm resistor pulling it up to +5v.

Ward

I have now commented out line 268 as suggested by Waffle, deleted the OneWire.o file to cause a recompile, and tested the revised code with seven devices. It works.

For those who might want to duplicate this fix, here is a print out of the area in question with the commented line in place.

 263                } else {                            // we are blazing new tree, take the 0
 264                  a = 0;
 265                  searchJunction = i;
 266                  done = 0;
 267                }
 268                // lastJunction = i;
 269            }
 270            if ( a) address[ ibyte] |= ibit;
 271            else address[ ibyte] &= ~ibit;

Rx

I had the same problem as soon as I added a 6th device to my prototype DS18B20 network (parasite powered). Only 2 devices were affected; as soon as I removed either of them, the search function worked properly. The symptom of the problem was a search that endlessly cycled between device 28 C6 and 28 5E

My devices (all values in hex):

28 78 09 06 02 00 00 92
28 1C 23 02 02 00 00 C6
28 C6 0D 06 02 00 00 99 <-- remove either device 28 C6
28 5E FB 05 02 00 00 3B <-- or device 28 5E for search to work
28 01 47 02 02 00 00 D9
28 D9 FF 05 02 00 00 4B

Once I commented out line 268 and recompiled the OneWire library, all devices work fine.

Rx

pwillard


beduino

This is great, I was stuck with the same problem: I could connect the AAG weather station (3 sensors) but when i tried to add one more ds18s20 the search program got in a loop.
Thank you for the detailed solution.

Go Up