Is it possible to wire one DS18B20 on D2 and another DS18B20 on D3 ??

Hello all,

In my project, I just need two DS18B20 sensors to monitor temperature at different locations. I see few example on line about wiring two DS18B20 sensors together based on one-wire function.

I just wonder if I could just let one DS18B20 sensor data line goes to D2 and another DS18B20 data line goes to D3? Is it possible?

Here is my modified code. Any comments? Thanks!!

#include <OneWire.h> 

int DS18S20_Pin1 = 2; //DS18S20 Signal pin on digital 2
int DS18S20_Pin2 = 3; //DS18S20 Signal pin on digital 3


//Temperature chip i/o
OneWire ds1(DS18S20_Pin1);  // on digital pin 2
OneWire ds2(DS18S20_Pin2);  // on digital pin 3


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

void loop(void) {
  float temperature1 = getTemp1(); //will take about 750ms to run
  float temperature2 = getTemp2(); //will take about 750ms to run

  Serial.print(temperature1);
  Serial.println(temperature2);
  
}


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

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

  if ( !ds1.search(addr)) {
      //no more sensors on chain, reset search
      ds1.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;
  }

  ds1.reset();
  ds1.select(addr);
  ds1.write(0x44,1); // start conversion, with parasite power on at the end
  
  delay(750); // Wait for temperature conversion to complete

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

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds1.read();
  }
  
  ds1.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;
  
}



float getTemp2(){
  //returns the temperature from another DS18S20 in DEG Celsius

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

  if ( !ds2.search(addr)) {
      //no more sensors on chain, reset search
      ds2.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;
  }

  ds2.reset();
  ds2.select(addr);
  ds2.write(0x44,1); // start conversion, with parasite power on at the end
  
  delay(750); // Wait for temperature conversion to complete

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

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds2.read();
  }
  
  ds2.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;
  
}

I won't ask why you would ever want to do this, but you are not totally alone and the Sheepdog Guides site has a means of doing it. I recall it even absolves you from the absurdity of using OneWire twice.

Is it possible?

Yes.
Have you tried your code?

Pete

Why two pins.
Code seems too complicated for such a simple thing.
You only need three lines in void loop() for two sensors (one shared resistor, and on one digital pin).
Working test code attached.
Leo..

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

OneWire oneWire(6); // pin D6
DallasTemperature sensors(&oneWire);
float temp1, temp2;

void setup() {
  Serial.begin(115200);
  Serial.println("DS18B20 thermometer");
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures();
  temp1 = sensors.getTempCByIndex(0);
  temp2 = sensors.getTempCByIndex(1);
  
  Serial.print("Temp1 is ");
  Serial.print(temp1, 4); // four decimal places
  Serial.println(" C");
  Serial.print("Temp2 is ");
  Serial.print(temp2, 4);
  Serial.println(" C\n");
}

Hello Wawa:

How could I know which DS18B20 is outputting?

  temp1 = sensors.getTempCByIndex(0);
  temp2 = sensors.getTempCByIndex(1);

Thanks a lot.

I think the code organises the sensors by (burned-in) serial number.

Hold one in your hand, and see which one increases in temp.
Mark the wire/sensor.

This
Serial.print(temp1, 1); // one decimal place
is easier to understand for humans
Leo..

Wawa:
I think the code organises the sensors by (burned-in) serial number.

Hold one in your hand, and see which one increases in temp.
Mark the wire/sensor.
Leo..

Yeah. I was thinking the same way. :slight_smile:

Wawa:
I think the code organises the sensors by (burned-in) serial number.

Hold one in your hand, and see which one increases in temp.
Mark the wire/sensor.

This
Serial.print(temp1, 1); // one decimal place
is easier to understand for humans
Leo..

Hello Leo,

Would you mind if you could illustrate the wiring two sensor in one pin? I am going to try it this morning.

Thanks a lot.

Would you mind if you could illustrate the wiring two sensor in one pin? I am going to try it this morning.

  1. Connect up one of the sensors along with it's required pull-up resistor.

  2. Connect the other sensor in parallel with the first. VDD to VDD, GND to GND, and DQ to DQ.

In other words the DQ pins of the two sensors are both connected to the same resistor and to the same Arduino pin.

Don

floresta:

  1. Connect up one of the sensors along with it's required pull-up resistor.

  2. Connect the other sensor in parallel with the first. VDD to VDD, GND to GND, and DQ to DQ.

In other words the DQ pins of the two sensors are both connected to the same resistor and to the same Arduino pin.

Don

Thank you Don and Leo. I got it wired. I did not have 4k7 resistors with me, but I did try with a 1k resistor. I got outputs, but it only showed correct value in one of the sensors. Overall, the code and wire worked.

changks:
Thank you Don and Leo. I got it wired. I did not have 4k7 resistors with me, but I did try with a 1k resistor. I got outputs, but it only showed correct value in one of the sensors. Overall, the code and wire worked.

I tested it with a 10k resistor. The readings are stable. Does that mean I could use 10k instead of using 4k7? Thanks guys.

changks:
I tested it with a 10k resistor. The readings are stable. Does that mean I could use 10k instead of using 4k7? Thanks guys.

10k is probably ok if total wire length is short (few meters).

You can "make" a 4k7 (5k) resistor by connecting two 10k resistors in parallel.
Leo..

Wawa:
10k is probably ok if total wire length is short (few meters).

You can "make" a 4k7 (5k) resistor by connecting two 10k resistors in parallel.
Leo..

I measured the distance from the Arduino and where the sensor will be placed. The total distance is ~4 meters per line. I will go find some resistors from other labs. Thanks Leo.