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.
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..
Connect up one of the sensors along with it's required pull-up resistor.
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.
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.