Pages: [1] 2   Go Down
Author Topic: Multiple DS18B20 Temperature Sensors on one bus  (Read 17342 times)
0 Members and 2 Guests are viewing this topic.
Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 32
Posts: 1866
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Everyone,

There is a lot around the web on this subject, but I have recently pulled a lot together and written clear example code. You can see the updated page on the http://ArduinoInfo.Info WIKI here:
http://arduino-info.wikispaces.com/Brick-Temperature-DS18B20

I have a utility sketch that you can use to get the internal serial number (Device Address) on DS18B20's that you have.  Then you can use those addresses in the code for multiple temperature sensors on one wire. This is important so you can know "which sensor is which" in a bus environment. You can see that info here:
http://arduino-info.wikispaces.com/Brick-Temperature-DS18B20#mult

I will have an example running in a day or two which displays and graphs inside and outside temperatures on KSduino.org
« Last Edit: January 18, 2013, 08:27:10 am by terryking228 » Logged

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great tutorial. I could setup my arduino to read temperatures from two sensors easily.

Thanks!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

G'Day Gang,
I seem to be having trouble finding the addresses of 2 DS18B20 temp probes. I have them set up on a bread board with a 4.7k pull up
resister.
I am using this sketch.

Code:
/* YourDuino Example: Find Address of a DS18B20 Temperature Sensor
 Cut and paste the address to a text file for later use.
 V1.1 01/17/2013
 Questions: terry@yourduino.com
 
 Connections:
 DS18B20 Pinout (Left to Right, pins down, flat side toward you)
 - Left   = Ground
 - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
 - Right  = +5 or +3.3 V  
 This sketch looks for 1-wire devices and  prints their addresses (serial number)
 to the Serial Monitor in a format that is useful in Arduino sketches.
 Based on example at:
 http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
 */

/*-----( Import needed libraries )-----*/
#include <OneWire.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define SENSOR_PIN 2  // Any pin 2 to 12 (not 13) and A0 to A5

/*-----( Declare objects )-----*/
OneWire  ourBus(SENSOR_PIN);  // Create a 1-wire object

void setup()  /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
 
  discoverOneWireDevices();  // Everything happens here!
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  // Nothing happening here
}

/*-----( Declare User-written Functions )-----*/
void discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  Serial.print("Looking for 1-Wire devices...\n\r");// "\n\r" is NewLine
  while(ourBus.search(addr)) {
    Serial.print("\n\r\n\rFound \'1-Wire\' device with address:\n\r");
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n\r");
      return;
    }
  }
  Serial.println();
  Serial.print("Done");
  ourBus.reset_search();
  return;
}

//*********( THE END )***********

The sketch compiles ok and uploads fine.
When I open the serial monitor I get

"Looking for 1-Wire devices...


Done
"

and no device addresses.
I have checked and rechecked pin numbers and connections but still no joy.
Any ideas pls

Cheers
Dennis

moderator added code tags:
« Last Edit: July 07, 2014, 12:53:56 pm by robtillaart » Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2169
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The sketch compiles ok and uploads fine.
When I open the serial monitor I get

"Looking for 1-Wire devices...
Done"

and no device addresses.
I have checked and rechecked pin numbers and connections but still no joy.
Any ideas pls

Cheers
Dennis

It might just be the order of events in the code. This isn't much different, but it works.

Code:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

#include <OneWire.h>

OneWire  ds(3);  // Connect your 1-wire device to pin 3

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

void discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
 
  Serial.print("Looking for 1-Wire devices...\n\r");
  while(ds.search(addr)) {
    Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");

    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      Serial.print(addr[i], HEX);

      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) {
        Serial.print("CRC is not valid!\n");
        return;
    }
  }
  Serial.print("\n\r\n\rThat's it.\r\n");
  ds.reset_search();
  return;
}

void loop(void) {
  // nothing to see here
}
Logged

Detroit, Michigan, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another solution is to repeatedly click the 'reset' button on your Arduino. You normally have to click it at least once for the program to work.

Hello,

I had the same problem recently, using the Hacktronics 1-Wire Address Finder. It ended up that the resistor was not placed in the right spot, connecting power and signal. I used a 10k ohm resistor in place of the 4.7k, as that was all I had at the time. It seems to work alright now, reading the same temperature as my analog manual thermometers. I located the address perfectly but when I loaded the multiple-sensor code, with the correct addresses, it failed. I used the bildr guide which can only use 1 temperature sensor. However, a member posted a code with capability for multiple sensors in the code, which solved my problems.

Following is the 1-Wire Address Finder
Code:
// This sketch looks for 1-wire devices and
// prints their addresses (serial number) to
// the UART, in a format that is useful in Arduino sketches
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

#include <OneWire.h>

OneWire  ds(3);  // Connect your 1-wire device to pin 3

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

void discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
  
  Serial.print("Looking for 1-Wire devices...\n\r");
  while(ds.search(addr)) {
    Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) {
        Serial.print("CRC is not valid!\n");
        return;
    }
  }
  Serial.print("\n\r\n\rThat's it.\r\n");
  ds.reset_search();
  return;
}

void loop(void) {
  // nothing to see here
}


And here is the 'comment' code I am using currently
Code:
#include <OneWire.h>

int DS18S20_Pin = 4; //DS18S20 Signal pin on digital 4

//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 4

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

void loop(void) {
 float temperature = getTemp();
 Serial.print ("The inside temperature in Degrees Celsius is:") ;
 Serial.println(temperature);
 
 delay(1000); //just here to slow down the output so it is easier to read
 
}


float getTemp(){
 //returns the temperature from one DS18S20 in DEG Celsius

 byte data[12];
 byte addr[8];

 if ( !ds.search(addr)) {
   //no more sensors on chain, reset search
   ds.reset_search();
   return -1000;
 }

 if ( OneWire::crc8( addr, 7) != addr[7]) {
   Serial.println("CRC is not valid!");
   return -1000;
 }

 if ( addr[0] != 0x10 && addr[0] != 0x28) {
   Serial.print("Device is not recognized");
   return -1000;
 }

 ds.reset();
 ds.select(addr);
 ds.write(0x44,1); // start conversion, with parasite power on at the end

 byte present = ds.reset();
 ds.select(addr);  
 ds.write(0xBE); // Read Scratchpad

 
 for (int i = 0; i < 9; i++) { // we need 9 bytes
  data[i] = ds.read();
 }
 
 ds.reset_search();
 
 byte MSB = data[1];
 byte LSB = data[0];

 float tempRead = ((MSB << 8) | LSB); //using two's compliment
 float TemperatureSum = tempRead / 16;
 
 return TemperatureSum;
 
}
Cheers,
Mark
« Last Edit: February 01, 2014, 04:37:09 pm by marcusdeans » Logged

Arduino is awesome! smiley

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2169
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I used a 10k ohm resistor in place of the 4.7k,  but when I loaded the multiple-sensor code, with the correct addresses, it failed.

Quite likely due to the wrong resistor. The pullup isn't critical, hence 10k with one DS18B20 is OK, but having twice the recommended resistance working with two is probably stretching the friendship.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi All, please how can I read the MAC address of the DHT22 sensor connected to the arduino uno board?
I want to connect multiple DHT22 and will need to take readings of each at thesame time and I need the MAC address of each sensor to do this. Thank you.

David
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I've just got two of these sensors and I'm having trouble finding their unique address.
I've been using this code as found on hacktronics...

Code:
// This sketch looks for 1-wire devices and
// prints their addresses (serial number) to
// the UART, in a format that is useful in Arduino sketches
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

#include <OneWire.h>

OneWire  ds(3);  // Connect your 1-wire device to pin 3

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

void discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
 
  Serial.print("Looking for 1-Wire devices...");
  while(ds.search(addr)) {
    Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) {
        Serial.print("CRC is not valid!\n");
        return;
    }
  }
  Serial.print("\n\r\n\rThat's it.\r\n");
  ds.reset_search();
  return;
}

void loop(void) {
  // nothing to see here
}

However the response is get is this:

€"Ásip.
ÁHookingfor!-Wire`eviCes..."

If I unplug it and run the sketch then I get a coherent response:

"Looking for 1-Wire devices...


That's it."

Any suggestions?

Thanks
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check your wiring. Have you plugged the bus into pin one by mistake? Try a different pin that's further away - 11 perhaps.
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for quick response. Yeah you're right   smiley-red. I was going off the micro process pin 3 (digital pin 1). Hence my error. Got the addresses for my sensors now!  smiley
Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need to write a function that can read 3 sensors (ds18b20) without knowing their address... i.e. we just know what socket (pins) they are to be plugged but I cant rely on the device address since they can be changed...

I tried looking up how the i2c bus assigns the index number so I could just refer to index number but didnt find any info...

anyway, I thought I could turn ON/OFF the power pin of the sensor thus only that sensor will report in to the bus... Dont know if Im screwing up something so thought I would ask.. Maybe a better way thus avoiding the use of so many resources?

Here is how the function will be called out, (all data wires to the same bus)

Code:

#define ONE_WIRE_BUS 7

#define vss_sensor_1 8
#define vss_sensor_2 10
#define vss_sensor_3 12
#define vcc_sensor_1 9
#define vcc_sensor_2 11
#define vcc_sensor_3 13

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temp_1, temp_2, temp_3;

void temp_check() {
   
  digitalWrite(vss_sensor_1, LOW);
  digitalWrite(vcc_sensor_1, HIGH);
  delay(250);  // allow the sensor time to initialize
  sensors.begin();
  sensors.requestTemperatures();
  temp_1 = sensors.getTempCByIndex(0);
  digitalWrite(vss_sensor_1, LOW);
  digitalWrite(vcc_sensor_1, LOW);
 
  digitalWrite(vss_sensor_2, LOW);
  digitalWrite(vcc_sensor_2, HIGH);
  delay(250);  // allow the sensor time to initialize
  sensors.begin();
  sensors.requestTemperatures();
  temp_2 = sensors.getTempCByIndex(0);
  digitalWrite(vss_sensor_2, LOW);
  digitalWrite(vcc_sensor_2, LOW);
 
  digitalWrite(vss_sensor_3, LOW);
  digitalWrite(vcc_sensor_3, HIGH);
  delay(250);  // allow the sensor time to initialize
  sensors.begin();
  sensors.requestTemperatures();
  temp_3 = sensors.getTempCByIndex(0);
  digitalWrite(vss_sensor_3, LOW);
  digitalWrite(vcc_sensor_3, LOW);
}

I havent tested it yet but it compiles...
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13477
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

give every sensor a pin of their own and the solution is straightforward.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 2
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

K sounds nice! so instead of using the dallastemperature library just use the onewire.h and write the code?

Or how can the dallastemperature handle different pins?
??
Code:
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

If I understand correctly, just use onewire and define the ds pins like this:

Code:
#include <OneWire.h>
OneWire ds_1(10);  //sensor 1 on pin 10
OneWire ds_2(11);  //sensor 2 on pin 11
OneWire ds_3(12);  //sensor 3 on pin 12

//Then search every pin for its address and request temps
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13477
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Code:
#include <OneWire.h>

OneWire ds_1(10);  //sensor 1 on pin 10
OneWire ds_2(11);  //sensor 2 on pin 11
OneWire ds_3(12);  //sensor 3 on pin 12

//Then search every pin for its address and request temps
DallasTemperature sensor_1(&ds_1);
DallasTemperature sensor_2(&ds_2);
DallasTemperature sensor_3(&ds_3);

........

  sensor_1.begin();
  sensor_1.requestTemperatures();
  temp_1 = sensor_1.getTempCByIndex(0);

(3 times)  you get the idea
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 2
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!  smiley-mr-green
Logged

Pages: [1] 2   Go Up
Jump to: