Interfacing Multiple DS18B20

Has anyone used the OneWire bus? I am trying to read two sensors on the same bus and I’m not getting expected results. The link below is an example I’m following and the first issue I have with this is that it only shows one temperature on one line of the LCD.

I’m attempting to make sense sense of the OneWire code and the datasheet (attached) for this sensor. The datasheet mentions that reading from both sensors at the same time could cause data clash. I am not sure how this OneWire library work and how it differientiates the device. It looks like it search for all addresses/serial numbers on the bus but where/how does it stores these and access them?

Interfacing Multiple DS18B20

DS18B20.pdf (390 KB)

rmknepp:
Has anyone used the OneWire bus? ..............the first issue I have with this is that it only shows one temperature on one line of the LCD.

Almost everybody uses the OneWire bus and almost everybody, except the clown in your link, uses the Dallas temperature library as well. I suggest you follow them.
http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
Your problem may actually be down to slack code for the LCD.

rmknepp:
I'm attempting to make sense sense of the OneWire code and the datasheet (attached) for this sensor. The datasheet mentions that reading from both sensors at the same time could cause data clash. I am not sure how this OneWire library work and how it differientiates the device. It looks like it search for all addresses/serial numbers on the bus but where/how does it stores these and access them?

1. Setup connection using 2 DS18B20 sensors.
ds18b20-2x.png
Figure-1: Connection diagram using 2xDS18B20 sensors and I2C LCD

2. The Structure of Scratchpad Memory and EEPROM of DS18B20


Figure-2: Structure of Scratchpad Memory

3. Every sensor has its own 64-bit (8-byte) code called address (consisting of Serial Number, family Code, and CRC) stored in ROM. The structure of the 64-bit ROMCode is:


Figure-3: Structure of ROM Code

4. We may declare/execute the following codes to collect the ROMCode of DS1 (Fig-1) and store the value in an 8-byte array.

#include<OneWire.h>
OneWire ds(2);         //Bus Pin = DPin-2
byte addr1[8];         //to hold 64-bit ROM Codes of DS1

void setup()
{
    Serial.begin(9600);
    //--------------------
 
    ds.reset();
    ds.search(addr1);  //collect 64-bit ROM code from sensor (DS1)
  
    Serial.print("Address of DS-1: ");
    for (int i=0; i<8; i++)
    {
       if(add1[i] < 0x10)
       {
           Serial.print('0');    //print leading zero
       }
       Serial.print(addr1[i], HEX);   //show 8-byte ROMCode
    }
}

void loop()
{

}

5. By the similar codes of Step-4, the ROMCode of DS2 could be collected and stored in the an array named adr2.

6. Now, we can use the address of DS1 (DS2) to its Temperature value by declaring/executing the following codes. There would be no clash/conflict while reading signals from the two sensors as the reading is sequential (one-after-another).

byte data1[9];        //buffer to hold data coming from DS18B20-1; why 9-byte? See, Fig-2.
//-----------------------------------------------------------------------------------------------------
 ds.reset();       //bring 1-Wire into idle state
 ds.select(addr1); //slect with DS-1 with address addr1
 ds.write(0x44);    //conversion command
 delay(1000);   //data ready withinh DS18B20 or poll status word
 //--------------------------------------------------------------------------------------

 ds.reset();
 ds.select(addr1);  //selectimg the desired DS18B20-1
 ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
 ds.read_bytes(data1, 9); //data comes from DS- and are saved into buffer data1[9]
 //--------------------------------------------------------------------------------------

BTW: Which one is physically DS-1? To know it, we have to check address of one sensor at a time and then label it.

ds18b20-2x.png

Nick_Pyner:
Almost everybody uses the OneWire bus and almost everybody, except the clown in your link, uses the Dallas temperature library as well. I suggest you follow them.
http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
Your problem may actually be down to slack code for the LCD.

Thanks for the additional example, it helped a bit. However, they use the DallasTemperature library as well.

GolamMostafa:
4. We may declare/execute the following codes to collect the ROMCode of DS1 (Fig-1) and store the value in an 8-byte array.

This was the piece I needed (the other example eluded to this as well). Basically get the address from the individual device and hard code them. I had actually tried to do this before my original post but was unsuccessful; I’m sure I wasn’t doing something right. The “search” function in the OneWire library makes you think that it’s searching for all devices on the bus and storing their addresses; this is misleading.

The thing about my first example’s code was it was written incorrectly. The if(!type) and else statements only work if you have a [18B20 or 1822] AND [18S20]. If you have two 18B20’s, for example, the values get overwritten at the same place in the LCD.

Thank you both for your help.

I’ve attached my final working code with hard coded addresses.

2tempsens2.ino (2.75 KB)

rmknepp:
Thanks for the additional example, it helped a bit. However, they use the DallasTemperature library as well.

Indeed they do, so why the "However"? You clearly didn't read my reply properly. The tutorial has two programmes - one to get the addresses, and the other to use them. There are several ways to use the DS18B20 but, pardon my bombast, I have never seen a convincing explanation as to why you would use the others.

If you have two 18B20's, for example, the values get overwritten at the same place in the LCD.

This is nonsense. The code for writing to the LCD has nothing to do with the specific type of temp sensor, but properly coding the cursor control will probably help quite a lot. I only mentioned the LCD in the first place because, while your sensor code is antiquated junk that does not merit perusal, it might actually work, and the reason why you have a problem is because you simply haven't moved the LCD cursor to the next line. Indeed, I am beginning to suspect that that is the case, and you are moving the blame to the sensor - which is entirely innocent.

rmknepp:
The "search" function in the OneWire library makes you think that it's searching for all devices on the bus and storing their addresses; this is misleading.

Is this really misleading...?

You declare 2 arrays named: byte ds1adr[8]; and byte ds2Adr[8];. Now, you execute these 2 instructions: ds.search(ds1Adr); and ds.search(ds2Adr);. The acquired values (2x8-byte) will be correctly saved in two different array variables. If you have an array of sensors, you can use loop structures.