DS18B20 multiple sensor detecting question

Hi everyone,
I've got the code for reading the values from bus (connected to digital pin2) and multiple DS18B20 thermopairs with assigned addresses. Is there any possibilities to detect which sensors are connected to the bus and print ONLY their values and nothing more?

Here's my code:

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS_PIN 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.

DeviceAddress Sensor1 = { 0x28, 0xFF, 0x6B, 0xD3, 0x43, 0x16, 0x03, 0xD1 }; 
DeviceAddress Sensor2 = { 0x28, 0xCC, 0x92, 0x40, 0x04, 0x00, 0x00, 0xB6 };
DeviceAddress Sensor3 = { 0x28, 0x4D, 0x8D, 0x40, 0x04, 0x00, 0x00, 0x78 };
DeviceAddress Sensor4 = { 0x28, 0x9A, 0x80, 0x40, 0x04, 0x00, 0x00, 0xD5 };
DeviceAddress Sensor5 = { 0x28, 0xE1, 0xC7, 0x40, 0x04, 0x00, 0x00, 0x0D };


void setup()  
{
  // start serial port to show results
  Serial.begin(9600);
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);
  
  // Initialize the Temperature measurement library
  sensors.begin();
  
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Sensor1, 12);
  sensors.setResolution(Sensor2, 12);
  sensors.setResolution(Sensor3, 12);
  sensors.setResolution(Sensor4, 12);
  sensors.setResolution(Sensor5, 12);

}

void loop()  
{
  delay(1000);
  float a = sensors.getDeviceCount();
  Serial.println();
  Serial.print("Number of DS18B20 found on bus = ");  
  Serial.println(a);   
  
  // Command all devices on bus to read temperature  
  sensors.requestTemperatures();  
  
  Serial.print("Sensor1 temperature is:   ");
  printTemperature(Sensor1);
  Serial.println();

  Serial.print("Sensor2 temperature is:   ");
  printTemperature(Sensor2);
  Serial.println();
 
  Serial.print("Sensor3 temperature is:   ");
  printTemperature(Sensor3);
  Serial.println();
   
  Serial.print("Sensor4 temperature is:   ");
  printTemperature(Sensor4);
  Serial.println();
  
  Serial.print("Sensor5 temperature is:   ");
  printTemperature(Sensor5);
  Serial.println();
   
  
}

// Declare User-written Functions
void printTemperature(DeviceAddress deviceAddress)
{

float tempC = sensors.getTempC(deviceAddress);

   if (tempC == -127.00) 
   {
   Serial.print("Error getting temperature  ");
   } 
   else
   {
   Serial.print("C: ");
   Serial.print(tempC);
   }
}

The example program Tester.pde shows how to detect the devices.

Cheers,
/dev

You don't need that set resolution stuff if you want 12bit, it is there by default.

Your printTemperature function can first check to see if the given device address is on the bus using:

  // attempt to determine if the device at the given address is connected to the bus
  bool isConnected(uint8_t*);

and just exit if it isn't.

But there seems to be one problem. You aren't waiting for the conversions to finish.
You should call sensors.setWaitForConversion(true), otherwise what you are reading from the sensors isn't necessarily the correct value.

Pete

thanks for the all responses guys, working forwards :slight_smile:

void loop() 
{
  delay(1000);
  float a = sensors.ge

I don't think conversion time is your problem, 1000ms is plenty. At 12bit resolution, the DS18B20 requires 750 to do its job. The delay normally goes at the end of the loop but I think it is quite appropriate at the beginning of a small programme, because it then gives the sensors time to scratch their bum on startup, thereby avoiding a false reading. The setup you have is likely to be too short, and a delay for that purpose is normally inserted therein, or accumulated by other means.

I don't see the need to count the sensors in the loop. If somebody has stolen one since the previous round you will get a no connection error -127. That way, you actually get to know which ones have been stolen, rather than merely how many. If you really feel the need to count them, I think it would be better to do that in the setup.

Hi Nick,
Thanks for the suggestions with delays and counting. I don't need to count them so I will delete some lines in the code. The delay I'll use about 3 minutes between reading, it's 1000ms only for testing the code and do this job faster. Thanks.

The other thing I could not figure out is with boolean function, I understand what it does, but cannot figure out where to put "sensors.setWaitForConversion(true)" line to test.

Vicc:
The delay I'll use about 3 minutes between reading, it's 1000ms only for testing the code and do this job faster.

OK, in that event put a delay(1000) in setup in the normal manner, and your loop delay at the end of same, so that you get a reading more or less straight away.

I understand what it does

If that's the case, you are a better man than I, Gunga Din, but I bet it's superfluous. I had never heard of it until a month ago, and I can't really remember what it is about. I think it has to do with minimising the time in the loop, but I submit that anybody who can't wait for a second probably shouldn't be using a DS18B20.

A basic code

/* Basic 2xDS18B20 code for serial monitor, bluetooth, Excel or w.h.y.
 Derived from Hacktronics.
 http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
 Use their address sniffer and substitute your numbers.
 Use Hacktronics connections diagram.
 Stay away from using parasite power
 No resolution command means default to 12 bit
 -127C means bad connection
 85 means you haven't gotten a read yet, probably wrong order of commands
 */

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

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

byte Thermo1[8] = {0x28, 0x39, 0xFD, 0x50, 0x04, 0x00, 0x00, 0X69};
byte Thermo2[8] = {0x28, 0x09, 0xA9, 0xC0, 0x03, 0x00, 0x00, 0x95};

float tempC,Temp1,Temp2; 

void setup(){
  Serial.begin(9600);
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures();  // call readings from the addresses
  Temp1 = sensorValue(Thermo1);
  Temp2 = sensorValue(Thermo2); 

  Serial.print("      Temp1 = ");
  Serial.print(Temp1);
  Serial.print("      Temp2 = ");
  Serial.println(Temp2);

  delay(1000);
}

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

really shows all you need.

It is more or less the same as that in the Hacktronics tutorial. I find it better for integration with other things, but that may be just personal.

Thank you Nick for the big help :slight_smile:

actually I need to print only those sensors values which are connected. In Nick's example code the output is two lines, even one sensor is connected to bus. I want to have assigned addresses with assigned ds18b20 names in code and want that program detect which ones are connected. Boolean function seems to do the job, but I can't figure out where to put "sensors.setWaitForConversion(true)" line (from el_supremo example).

That goes in setup().

Pete