Multiple OneWire Buses

Hi, I’m trying to use two onewire buses. I plan to connect 16 ds18b20 sensors using those two buses. For this, initially, I’m experimenting with three sensors by connecting two sensors to one bus and the other sensor to another bus. The code gets uploaded but it shows improper value like -127 degree celcius for the first bus, where the two sensors are connected, which is Onewire A according to my code. Can you guys please help me?

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

LiquidCrystal_I2C lcd(0x27, 20, 4);

// Data wire is plugged into digital pin 2 on the Arduino
OneWire oneWireA (2) ;
OneWire oneWireB (3) ;

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

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensorsA (&oneWireA) ;
DallasTemperature sensorsB (&oneWireB) ;

// Addresses of 3 DS18B20s
uint8_t sensor1[8] = { 0x28, 0xF2, 0x00, 0x07, 0xD6, 0x01, 0x3C, 0x95 };
uint8_t sensor2[8] = { 0x28, 0x35, 0xAB, 0x07, 0xD6, 0x01, 0x3C, 0xCC };
uint8_t sensor3[8] = { 0x28, 0xE4, 0xB9, 0x07, 0xD6, 0x01, 0x3C, 0xC0 };

void setup(void)
{
  Serial.begin(9600);
  sensorsA.begin();
  sensorsB.begin(); 
  lcd.begin();
  lcd.backlight();
}

void loop(void)
{
  sensorsA.requestTemperatures();
  sensorsB.requestTemperatures();
  lcd.setCursor(0,0);
  lcd.print("SENSOR1: ");
  Serial.println("SENSOR1: ");
  printTemperature(sensor1);
  
  lcd.setCursor(0,1);
  lcd.print("SENSOR2: ");
  Serial.println("SENSOR2: ");
  printTemperature(sensor2);

  delay(2000);

  /*for (int positionCounter = 0; positionCounter < 20; positionCounter++) {
   // scroll one position left:
   lcd.autoscroll();
   // wait a bit:
   delay(300);
   }*/

  lcd.clear();
  
  lcd.setCursor(0,2);
  lcd.print("SENSOR3: ");
  Serial.println("SENSOR3: ");
  printTemperature(sensor3);
  delay(1000);
}

  void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensorsA.getTempC(deviceAddress);
  float tempD = sensorsB.getTempC(deviceAddress);
 Serial.println(tempC);
 Serial.println(tempD);
  lcd.print(tempC);
  lcd.print((char)223);
  lcd.print("C");
  lcd.print(tempD);
  lcd.print((char)223);
  lcd.print("C");
  
}

Do you have a pullup resistor for each bus?

Yes, I used a 4.7k ohm resistor for each of the buses. I guess there is something wrong with the code, which I'm unable to find.

What are you doing in printTemperature()?

void printTemperature(DeviceAddress deviceAddress)
{
    float tempC = sensorsA.getTempC(deviceAddress);
    float tempD = sensorsB.getTempC(deviceAddress);
    Serial.println(tempC);
    Serial.println(tempD);
    lcd.print(tempC);
    lcd.print((char)223);
    lcd.print("C");
    lcd.print(tempD);
    lcd.print((char)223);
    lcd.print("C");
 
}

You enter with a device address array (e.g. sensor1) and then put that request on both buses?

What does getTempC() return if there's no device on a bus whose address corresponds with sensor1?

Maybe during setup you can do a search of each bus for all unique devices and store that info in an array so you know which devices are on which bus, and then only address those for that bus.

I do not know the DallasTemperature library real well, but I did make a program that works. I was not able to get it to work with sensor addresses, but it does work with indexes.

I set up my Uno with a 20x4 LCD and 3 SD18B20 sensors on 2 one wire buses. I use the hd44780 library for LCDs as it is a much better library than any of the (old) LiquidCrystalI2C libraries. The hd44780 library is available via the IDE library manager.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

const byte oneWireAPin = 2;
const byte oneWireBPin = 3;

// Data wire is plugged into digital pin 2 on the Arduino
OneWire oneWireA (oneWireAPin) ;
OneWire oneWireB (oneWireBPin) ;

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensorsA (&oneWireA) ;
DallasTemperature sensorsB (&oneWireB) ;

hd44780_I2Cexp lcd;

void setup(void)
{
   Serial.begin(115200);
   sensorsA.begin();
   sensorsB.begin();
   lcd.begin(20, 4);
   lcd.backlight();
   lcd.setCursor(0, 0);
   lcd.print("SENSOR1: ");
   lcd.setCursor(0, 1);
   lcd.print("SENSOR2: ");
   lcd.setCursor(0, 2);
   lcd.print("SENSOR3: ");
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();

      sensorsA.requestTemperatures();
      sensorsB.requestTemperatures();

      float tempC1 = sensorsA.getTempCByIndex(0);
      lcd.setCursor(10, 0);
      lcd.print("        ");
      lcd.setCursor(10, 0);
      lcd.print(tempC1);
      lcd.print((char)223);
      lcd.print("C");
      Serial.print("SENSOR1: ");
      Serial.println(tempC1);

      float tempC2 = sensorsB.getTempCByIndex(0);
      lcd.setCursor(10, 1);
      lcd.print("        ");
      lcd.setCursor(10, 1);
      lcd.print(tempC2);
      lcd.print((char)223);
      lcd.print("C");
      Serial.print("SENSOR2: ");
      Serial.println(tempC2);

      float tempC3 = sensorsB.getTempCByIndex(1);
      lcd.setCursor(10, 2);
      lcd.print("        ");
      lcd.setCursor(10, 2);
      lcd.print(tempC3);
      lcd.print((char)223);
      lcd.print("C");
      Serial.print("SENSOR3: ");
      Serial.println(tempC3);
      Serial.println();
   }
}

-127 simply means no connection. Do you really need two buses?

groundFungus:
I do not know the DallasTemperature library real well, but I did make a program that works. I was not able to get it to work with sensor addresses, but it does work with indexes.

I set up my Uno with a 20x4 LCD and 3 SD18B20 sensors on 2 one wire buses. I use the hd44780 library for LCDs as it is a much better library than any of the (old) LiquidCrystalI2C libraries. The hd44780 library is available via the IDE library manager.

#include <OneWire.h>

#include <DallasTemperature.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

const byte oneWireAPin = 2;
const byte oneWireBPin = 3;

// Data wire is plugged into digital pin 2 on the Arduino
OneWire oneWireA (oneWireAPin) ;
OneWire oneWireB (oneWireBPin) ;

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensorsA (&oneWireA) ;
DallasTemperature sensorsB (&oneWireB) ;

hd44780_I2Cexp lcd;

void setup(void)
{
  Serial.begin(115200);
  sensorsA.begin();
  sensorsB.begin();
  lcd.begin(20, 4);
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("SENSOR1: ");
  lcd.setCursor(0, 1);
  lcd.print("SENSOR2: ");
  lcd.setCursor(0, 2);
  lcd.print("SENSOR3: ");
}

void loop()
{
  static unsigned long timer = 0;
  unsigned long interval = 1000;
  if (millis() - timer >= interval)
  {
     timer = millis();

sensorsA.requestTemperatures();
     sensorsB.requestTemperatures();

float tempC1 = sensorsA.getTempCByIndex(0);
     lcd.setCursor(10, 0);
     lcd.print("        ");
     lcd.setCursor(10, 0);
     lcd.print(tempC1);
     lcd.print((char)223);
     lcd.print(“C”);
     Serial.print("SENSOR1: ");
     Serial.println(tempC1);

float tempC2 = sensorsB.getTempCByIndex(0);
     lcd.setCursor(10, 1);
     lcd.print("        ");
     lcd.setCursor(10, 1);
     lcd.print(tempC2);
     lcd.print((char)223);
     lcd.print(“C”);
     Serial.print("SENSOR2: ");
     Serial.println(tempC2);

float tempC3 = sensorsB.getTempCByIndex(1);
     lcd.setCursor(10, 2);
     lcd.print("        ");
     lcd.setCursor(10, 2);
     lcd.print(tempC3);
     lcd.print((char)223);
     lcd.print(“C”);
     Serial.print("SENSOR3: ");
     Serial.println(tempC3);
     Serial.println();
  }
}

groundFungus:
I do not know the DallasTemperature library real well, but I did make a program that works. I was not able to get it to work with sensor addresses, but it does work with indexes.

I set up my Uno with a 20x4 LCD and 3 SD18B20 sensors on 2 one wire buses. I use the hd44780 library for LCDs as it is a much better library than any of the (old) LiquidCrystalI2C libraries. The hd44780 library is available via the IDE library manager.

#include <OneWire.h>

#include <DallasTemperature.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

const byte oneWireAPin = 2;
const byte oneWireBPin = 3;

// Data wire is plugged into digital pin 2 on the Arduino
OneWire oneWireA (oneWireAPin) ;
OneWire oneWireB (oneWireBPin) ;

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensorsA (&oneWireA) ;
DallasTemperature sensorsB (&oneWireB) ;

hd44780_I2Cexp lcd;

void setup(void)
{
  Serial.begin(115200);
  sensorsA.begin();
  sensorsB.begin();
  lcd.begin(20, 4);
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("SENSOR1: ");
  lcd.setCursor(0, 1);
  lcd.print("SENSOR2: ");
  lcd.setCursor(0, 2);
  lcd.print("SENSOR3: ");
}

void loop()
{
  static unsigned long timer = 0;
  unsigned long interval = 1000;
  if (millis() - timer >= interval)
  {
     timer = millis();

sensorsA.requestTemperatures();
     sensorsB.requestTemperatures();

float tempC1 = sensorsA.getTempCByIndex(0);
     lcd.setCursor(10, 0);
     lcd.print("        ");
     lcd.setCursor(10, 0);
     lcd.print(tempC1);
     lcd.print((char)223);
     lcd.print(“C”);
     Serial.print("SENSOR1: ");
     Serial.println(tempC1);

float tempC2 = sensorsB.getTempCByIndex(0);
     lcd.setCursor(10, 1);
     lcd.print("        ");
     lcd.setCursor(10, 1);
     lcd.print(tempC2);
     lcd.print((char)223);
     lcd.print(“C”);
     Serial.print("SENSOR2: ");
     Serial.println(tempC2);

float tempC3 = sensorsB.getTempCByIndex(1);
     lcd.setCursor(10, 2);
     lcd.print("        ");
     lcd.setCursor(10, 2);
     lcd.print(tempC3);
     lcd.print((char)223);
     lcd.print(“C”);
     Serial.print("SENSOR3: ");
     Serial.println(tempC3);
     Serial.println();
  }
}

Thanks a lot. The code worked. If you don’t mind, can you please explain the code? Especially this part?

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();

That is the "blink without delay" method of timing with millis(). I does non-blocking timing (replaced the delay() in your code).

These tutorials have more about how to do non-blocking programming.
Several things at a time.
Beginner's guide to millis().
Blink without delay().

groundFungus:
That is the "blink without delay" method of timing with millis(). I does non-blocking timing (replaced the delay() in your code).

These tutorials have more about how to do non-blocking programming.
Several things at a time.
Beginner's guide to millis().
Blink without delay().

groundFungus:
That is the "blink without delay" method of timing with millis(). I does non-blocking timing (replaced the delay() in your code).

These tutorials have more about how to do non-blocking programming.
Several things at a time.
Beginner's guide to millis().
Blink without delay().

Thanks again

sabit068:
Yes, for connecting 16 ds18b20 sensors

Explain please.
Tom... :slight_smile:

I don't think a worthwhile explanation is imminent.

Hi! I have connected 16 DS18B20 temperature sensors with Arduino Mega by using 2 digital pins as one-wire bus. I also have a 20*4 LCD & an sd card module connected for data logging purposes. The problem is that, the sensors are not getting enough power. The power goes immediately after starting. I'm using a 5V 2A external adapter for the Arduino. Is there any solution? Should I use a 12V 2A adapter? Thanks in advance.

Hi! I have connected 16 DS18B20 temperature sensors with Arduino Mega by using 2 digital pins as one-wire bus. I also have a 20*4 LCD & an sd card module connected for data logging purposes. The problem is that, the sensors are not getting enough power. The power goes immediately after starting. I'm using a 5V 2A external adapter for the Arduino. Is there any solution? Should I use a 12V 2A adapter? Thanks in advance.

What makes you think that the sensors are not getting enough power?

How are they wired? What kind of wire?

sabit068:
2 digital pins as one-wire bus.

That needs explanation. The one-wire bus requires only one pin. What is the second pin doing?

In a prior thread they were talking about multiple one wire buses. Like 8 sensors on one pin and 8 on another. They were asked why 2 buses, but never answered the question.

sabit068:
I need to use total 16 sensors. But through 1 pin, only 8 sensors can be used. So, I used two pins for 16 sensors

That implies seriously long cables, as the bus is limited by wire capacitance (which in turn is mainly dependent on wire length), rather than the number of actual sensors on it.

Each sensor takes mere milliamps at most, so 2A is more than enough current available.

More likely your wire length is at least part of the problem. So do describe your project and the exact behaviour of your sensors in detail.

sabit068:
I'm using a 5V 2A external adapter for the Arduino. Is there any solution? Should I use a 12V 2A adapter?

External supply connected to what...

The DC socket needs between 7 and 12volt.
5volt on the DC socket is too low, 12volt is (almost) too much.
2A is overkill, because of an inline reverse protection diode that can only cope with 1A.

Best way to power the Mega is with a 5volt cellphone charger, connected to the USB socket.
500mA is more than enough.
Leo..

“ 2A is overkill, because of an inline reverse protection diode that can only cope with 1A”

Not sure that makes any sense .

" 2A is overkill, because of an inline reverse protection diode that can only cope with 1A"

Not sure that makes any sense .

I agree - normally a power source (A common point of failure) is specified at 110% at MINIMUM of the power requirment of a project, I normally try to provide a power supply of 150%, as the higher the overhead the easier the supply has to work & (in my head) should not be under any adverse straign.