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