Hi everyone,
I've got the code for reading the values from bus (connected to digital pin2) and multiple DS18B20 thermopairs with assigned addresses. Is there any possibilities to detect which sensors are connected to the bus and print ONLY their values and nothing more?
Here's my code:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS_PIN 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress Sensor1 = { 0x28, 0xFF, 0x6B, 0xD3, 0x43, 0x16, 0x03, 0xD1 };
DeviceAddress Sensor2 = { 0x28, 0xCC, 0x92, 0x40, 0x04, 0x00, 0x00, 0xB6 };
DeviceAddress Sensor3 = { 0x28, 0x4D, 0x8D, 0x40, 0x04, 0x00, 0x00, 0x78 };
DeviceAddress Sensor4 = { 0x28, 0x9A, 0x80, 0x40, 0x04, 0x00, 0x00, 0xD5 };
DeviceAddress Sensor5 = { 0x28, 0xE1, 0xC7, 0x40, 0x04, 0x00, 0x00, 0x0D };
void setup()
{
// start serial port to show results
Serial.begin(9600);
Serial.print("Initializing Temperature Control Library Version ");
Serial.println(DALLASTEMPLIBVERSION);
// Initialize the Temperature measurement library
sensors.begin();
// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
sensors.setResolution(Sensor1, 12);
sensors.setResolution(Sensor2, 12);
sensors.setResolution(Sensor3, 12);
sensors.setResolution(Sensor4, 12);
sensors.setResolution(Sensor5, 12);
}
void loop()
{
delay(1000);
float a = sensors.getDeviceCount();
Serial.println();
Serial.print("Number of DS18B20 found on bus = ");
Serial.println(a);
// Command all devices on bus to read temperature
sensors.requestTemperatures();
Serial.print("Sensor1 temperature is: ");
printTemperature(Sensor1);
Serial.println();
Serial.print("Sensor2 temperature is: ");
printTemperature(Sensor2);
Serial.println();
Serial.print("Sensor3 temperature is: ");
printTemperature(Sensor3);
Serial.println();
Serial.print("Sensor4 temperature is: ");
printTemperature(Sensor4);
Serial.println();
Serial.print("Sensor5 temperature is: ");
printTemperature(Sensor5);
Serial.println();
}
// Declare User-written Functions
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00)
{
Serial.print("Error getting temperature ");
}
else
{
Serial.print("C: ");
Serial.print(tempC);
}
}