Program changes for using second temp sensor

Hello guys,

I got this code and I would like to ready a temparature from a second sensor. What should I cahnge/add to read one more temperature from a differend sensor?

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

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

// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

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

void setup(void)
// start serial port
Serial.println(“Dallas Temperature IC Control Library Demo”);

// Start up the library

void loop(void)
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print(" Requesting temperatures…");
sensors.requestTemperatures(); // Send the command to get temperatures

Serial.print("Temperature for Device 1 is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why “byIndex”?
// You can have more than one IC on the same bus.
// 0 refers to the first IC on the wire


Thanks :smiley:

Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?

If you want to read a second sensor, it's index won't be 0. It would be 1. THAT is why ByIndex.

Thanks for your reply


I set this

define ONE_WIRE_BUS 2

define ONE_WIRE_BUS 3

and after that index 0 will print the 2 pin and index 1 will print 3 pin?

Is that right?

I set this

#define ONE_WIRE_BUS 2
#define ONE_WIRE_BUS 3

No. It makes no sense to overwrite the value associated with the name ONE_WIRE_BUS.

Don't you understand what a bus is? Have you seen a bus? How many seats does it have? The one-wire bus lets you connect several sensors to the Arduino using ONE pin. That's why it is called the one wire bus.

Just connect both sensors to the same pin. The library will figure out how to talk to each one.

The one wire library comes with examples. The one with _Temperature in the name searches for devices on the bus, so it can tell you whether you have the devices connected correctly.

I see....

ok but in that case index(1) will print both temperatures right?

Is it possible to "split" the temperatures to print them seperatelly?

Sorry if my questions are silly but I ve just started with that. xD

ok but in that case index(1) will print both temperatures right?

No. getTempCByIndex(2) won’t print 3 temperatures. getTempCByIndex(3) won’t print 4 temperatures, either.

The getTempCByIndex() function gets the temperature from the specified device, by index. There are other methods to get the temperature from the specified device by address. In order to use that method, you’d need to know the addresses of the two sensors.

It’s easier though, to remember that device 0 is the indoor temperature and that device 1 is the outdoor temperature, for instance. Or that they are the kitchen and bathroom temperatures.

Once the devices are on the bus, they keep the same address, and the addresses are always found in the same order. So, the devices do not move around. (Unless you physically move the sensors.)