#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_MAX31865.h>
#include <OneWire.h>
#include <DallasTemperature.h>
/* - Macro definitions - */
// The 'nominal' 0-degrees-C resistance of the sensor
// 100.0 for PT100, 1000.0 for PT1000
#define RNOMINAL 1000.0
// The value of the Rref resistor. Use 430.0 for PT100 and 4300.0 for PT1000
#define RREF 4300.0
#define MAX_ONE_WIRE_DEVICE_COUNT 20
/* - Global variables and setup - */
// Use software SPI: CS, DI, DO, CLK
// PT1000 Temperature sensor amplifier
Adafruit_MAX31865 thermo = Adafruit_MAX31865(10, 11, 12, 13);
// Setting up One Wire Object
OneWire ds(2); // Pin 2 with 4.7k Resistor
// DallasTemperature to translate the sensor data into Celcius
DallasTemperature sensors(&ds);
// set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
/// Defining Global values
// The Array which will contain the temperature values of the sensors
uint8_t address[8];
uint8_t findDevices()
{
ds.reset_search();
uint8_t address[8];
uint8_t count = 0;
lcd.setCursor(0,1);
if (ds.search(address))
{
do {
count++;
Serial.print("Address of Device ");
Serial.print(count);
Serial.println(" {");
for (uint8_t i = 0; i < 8; i++)
{
Serial.print("0x");
if (address[i] < 0x10) Serial.print("0");
Serial.print(address[i], HEX);
lcd.print(address[i], HEX);
if (i < 7)
{
Serial.print(", ");
lcd.print(",");
}
}
Serial.println(" },");
} while (ds.search(address));
Serial.println("};");
Serial.print("// nr devices found: ");
Serial.println(count);
}
return count;
}
float SensArray[3];
static float OneWireLoop(void)
{
/*Small note on the addressable one wire temp sensors. Their order in the "getTempCByIndex"
varies depending on their factory assigned ID. In our case the sensors are not in the same order as they
are in real life*/
sensors.requestTemperatures();
float Sens1 = sensors.getTempCByIndex(0);
float Sens2 = sensors.getTempCByIndex(1);
float Sens3 = sensors.getTempCByIndex(2);
SensArray[0] = Sens1;
SensArray[1] = Sens2;
SensArray[2] = Sens3;
}
/*This function displays the Acquired values on the LCD */
static void LCDLoop()
{
delay(100);
// lcd.clear();
/*The Analog Pins return a value between 0-1023 (0-5V or 3.3V) for this Arduino depending
on the incoming voltage. I have created the following 2 conversions for 5V and
3V3 Volts.*/
uint16_t A0_bits = analogRead(A0);
float A0_val_5V = (A0_bits / 1023.0) * 5.0;
float A0_val_3v3 = (A0_bits / 1023.0) * 3.3;
uint16_t rtd = thermo.readRTD();
float ratio = rtd;
ratio /= 32768;
/*The part of the function which handles the Serial printing*/
Serial.print("PT1000 Temperature:");
Serial.println(thermo.temperature(RNOMINAL, RREF));
Serial.print("A0 Voltage:");
Serial.println(A0_val_5V);
Serial.println(" Requesting temperatures...");
Serial.print("1st Temp sensor is: ");
Serial.println(SensArray[0]);
Serial.print("2nd Temp sensor is: ");
Serial.println(SensArray[1]);
Serial.print("3rd Temp sensor is: ");
Serial.println(SensArray[2]);
/*lcd.setCursor selects where the print will begin, lcd.print prints*/
// lcd.setCursor(0, 0);
// lcd.print("PT1000:");
// lcd.setCursor(7, 0);
// lcd.print(" ");
// lcd.setCursor(7, 0);
// lcd.print(thermo.temperature(RNOMINAL, RREF));
// lcd.setCursor(0, 1);
// lcd.print("A0(V):");
// lcd.print(A0_val_5V);
lcd.setCursor(0, 0);
lcd.print("S:");
lcd.print(SensArray[0]);
// lcd.print(" ");
// lcd.print(SensArray[1]);
// lcd.print(" ");
// lcd.setCursor(0, 3);
// lcd.print(SensArray[2]);
// lcd.print(" ");
}
void setup()
{
pinMode(A0, INPUT);
// initialize the lcd
lcd.init();
// Activates Backlight
lcd.backlight();
Serial.begin(115200);
thermo.begin(MAX31865_2WIRE); // Begins reading PT1000
sensors.begin(); // Begins reading OneWire sensors
}
void loop()
{
// Calling the previously defined functions
findDevices();
OneWireLoop();
LCDLoop();
}