Reading / Printing Dallas OneWire Temperature Sensors (Solved)

I am new to programming and have been experimenting using examples and code shared by others. I am trying to read and print temperatures from 3 sensors. I am attempting to find all available sensors, capture their address and output temperature readings. My current code finds the sensors but outputs -197 for each sensor instead of actual temperature. I have tested the sensors and the wiring configuration utilizing code that defines each sensor address in the sketch and all works OK. Below is the serial output I am currently producing and the complete sketch. Any help to get the actual temperature readings would be greatly appreciated.

Serial Output
Dallas Temperature IC Control Library Demo
Locating devices…Found 3 devices.
Found device 0 with address: 2841832F05000006
Setting resolution to 9
Resolution actually set to: 9
Found device 1 with address: 28B1E62F050000B7
Setting resolution to 9
Resolution actually set to: 9
Found device 2 with address: 28DF543005000009
Setting resolution to 9
Resolution actually set to: 9
Requesting temperatures…DONE
T1 T2 T3
-197 -197 -197
Requesting temperatures…DONE
T1 T2 T3
-197 -197 -197

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

// Data wire is plugged into port 8 on the Arduino
#define ONE_WIRE_BUS 8
#define TEMPERATURE_PRECISION 9

// 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);

int numberOfDevices; // Number of temperature devices found

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
//Variables to 3 sensor addresses
DeviceAddress Sensor1;
DeviceAddress Sensor2;
DeviceAddress Sensor3;

// Here are the temperature variables.
float T1;
float T2;
float T3;


void setup(void)
{
 
 
  // start serial port
  Serial.begin(9600);
  while(!Serial);  // wait for Serial port to connect.
  
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
  
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
  
  // locate devices on the bus
  Serial.print("Locating devices...");
  
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");
  delay(10000);
  
  // Loop through each device, print out address
  for(int i=0;i<numberOfDevices; i++)
  {
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i))
	{
		Serial.print("Found device ");
		Serial.print(i, DEC);
		Serial.print(" with address: ");
		printAddress(tempDeviceAddress);
		Serial.println();
		
		Serial.print("Setting resolution to ");
		Serial.println(TEMPERATURE_PRECISION, DEC);
		
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
		sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
		
		 Serial.print("Resolution actually set to: ");
		Serial.print(sensors.getResolution(tempDeviceAddress), DEC); 
		Serial.println();
                delay(10000);
                if(i==0){
                  Sensor1 == tempDeviceAddress;
                }
                if(i==1){
                  Sensor2 == tempDeviceAddress;
                }
                if(i==2){
                  Sensor3 == tempDeviceAddress;
                }
	}else{
		Serial.print("Found ghost device at ");
		Serial.print(i, DEC);
		Serial.print(" but could not detect address. Check power and cabling");
                delay(10000);
	}
  }

}



void loop(void)
{ 
  

  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  ReadAllSensors();
  Serial.println("DONE");
  SerialReport();
  delay (10000);
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

//Function to Read all sensors
void ReadAllSensors(){
  sensors.requestTemperatures();
  delay(1000);
  T1 = sensors.getTempF(Sensor1);
  delay(1000);
  T2 = sensors.getTempF(Sensor2);
  delay(1000);
  T3 = sensors.getTempF(Sensor3);
  delay(1000);
}

//Function to print temperature readings
void SerialReport(){
  Serial.print(F("T1  T2  T3 "));
  Serial.println(F(" "));
  Serial.print(T1 , 0);
  Serial.print(F("  "));
  Serial.print(T2 , 0);
  Serial.print(F("  "));
  Serial.println(T3 , 0);
}

I don't have any of those (though the whole idea is neat!) and no practical experience but I do recall seeing a video on them and just had a look... there's more than a couple videos on the subject that may help.

Open a tab and go to Youtube then type in the search bar: onewire sensor and maybe there's even links to code with one or more of them.

I don't know what the problem is but there are a couple of different ways to power one-wire devices and I remember that the parasitic power mode didn't work reliably for me. How are you powering these sensors?

I'm not much of a programmer but I was able to get 6 DS18B20s reading correctly with no effort by just installing the library from the ONEWIRE playground page and using the examples. Are you sure you wired it correctly ? I didn't see you mention the pullup resistors .

Thanks for the replies. I'm not using parasitic power. I do have a pull-up resistor included in my wiring and I have tested the wiring and sensors using the basic example sketches and I'm able to get accurate temperature readings. I'm thinking the issue is with my sketch.

                if(i==0){
                  Sensor1 == tempDeviceAddress;
                }
                if(i==1){
                  Sensor2 == tempDeviceAddress;
                }
                if(i==2){
                  Sensor3 == tempDeviceAddress;
                }

The result of the equality operator (==) will be true or false. You discard the result, so the whole exercise was a waste of time.

Now, the assignment operator (=), would have been more useful.

TomyM, Maybe you should research the syntax a little better or spend more time studying the ONEWIRE examples. All the operators are on the Refererence page http://arduino.cc/en/Reference/HomePage

Thanks for the suggestion PaulS. I modified the sketch by replacing the equality operator (==) with the assignment operator (=). This makes sense to me now and I was hopeful it would resolve the issue, but I get the following errors:

Auto_SensorAddress_ReadTemp3.ino: In function 'void setup()': Auto_SensorAddressReadTemp3:77: error: invalid array assignment Auto_SensorAddressReadTemp3:80: error: invalid array assignment Auto_SensorAddress_ReadTemp3:83: error: invalid array assignment

Any suggestions would be appreciated

Here is the section of code I modified

if(i==0){
                  Sensor1 = tempDeviceAddress;
                }
                if(i==1){
                  Sensor2 = tempDeviceAddress;
                }
                if(i==2){
                  Sensor3 = tempDeviceAddress;
                }

Any suggestions would be appreciated

Look at what type Sensor1 is. Look at what type tempDeviceAddress is. See if it makes sense to assign one to the other. The compiler is telling you that it does not.

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
DeviceAddress Sensor1;

So, what IS a DeviceAddress?

typedef uint8_t DeviceAddress[8];

Aha, it's really just an array of 8 unsigned bytes. So, use memcpy() to copy the address from one place to another.

PaulS: use memcpy() to copy the address from one place to another.

That definitely looks as if PaulS has spotted the problem. However, rather than copy the address via a temp variable, I suggest you put the result in the right variable in the first place. The best way to do that is to put your address variables in an array and use the device index as the array index.

Using memcpy() to copy the address from one place to another solved the problem! Thanks PaulS ! I will also take a look at the suggestion PeterH provided. Thank you both for the input.