
Arash (Customer) asked a question.
Could you please help me with the question? I really appreciate it.
Here is my code :
#include <Arduino.h>
#include <P1AM.h>
#include <SPI.h>
#include <SD.h>
const int SD_CS_PIN = SDCARD_SS_PIN; // SD card CS pin connected to P1AM slot 1 pin 4
const float VCC = 3.3; // supply voltage of the voltage divider circuit
const float VREF = 5.0; // reference voltage of the P1AM input module
const float R_REF = 1000.0; // reference resistor value for the voltage divider circuit
const float R_0 = 1000.0; // resistance of the PT1000 sensor at 0 degrees Celsius
const float A = 3.9083e-3; // constant A for PT1000 RTD
const float B = -5.775e-7; // constant B for PT1000 RTD
float temperature[4]; // array to store temperature readings
float resistance[4]; // array to store resistance calculations
unsigned long previousMillis = 0; // variable to store the last time a data point was collected
const unsigned long interval = 5000; // interval between data collection in milliseconds
const unsigned long duration = 100000; // duration of data collection in milliseconds
File myFile; // file object for writing to SD card
void setup() {
Serial.begin(9600); // initialize serial communication at 9600 baud
while (!P1.init()) {
; // wait for input module to initialize
}
pinMode(SD_CS_PIN, OUTPUT); // set SD card CS pin as output
if (!SD.begin(SD_CS_PIN)) { // initialize SD card
Serial.println("SD card initialization failed!");
while (1); // stop program if initialization fails
}
myFile = SD.open("data.csv", FILE_WRITE); // open file for writing
if (myFile) { // check if file opened successfully
Serial.println("File opened for writing.");
} else {
Serial.println("Error opening file for writing!");
}
}
void loop() {
unsigned long currentMillis = millis(); // get the current time
if (currentMillis - previousMillis >= interval) { // check if interval has elapsed
previousMillis = currentMillis; // update previous time
for (int i = 0; i < 4; i++) {
temperature[i] = P1.readTemperature(1, i + 1); // read temperature from input channel i+1 in slot 1
resistance[i] = (VCC * R_REF * temperature[i]) / (VREF * (VCC - temperature[i])); // calculate resistance using voltage divider circuit equation
myFile.print(millis()); // write current time to file
myFile.print(","); // separate time and temperature with comma
myFile.print(i + 1); // write channel number to file
myFile.print(",");
myFile.println(temperature[i]); // write temperature to file and add newline character
Serial.print("Channel "); // print a label for the temperature reading
Serial.print(i + 1);
Serial.print(": ");
Serial.print(temperature[i]); // print the temperature value
Serial.println(" degrees Celsius"); // print the unit of measurement for temperature
}
}
if (currentMillis >= duration) { // check if duration has elapsed
myFile.close(); // close file when program ends
Serial.println("Data collection complete.");
while (1); // stop program
}
}
The default configuration for the P1-04RTD I believe is pt100 and set for F. The 1562 reading is the max value for that range.
Try configuring the module to what you want using the below link and see if that fixes things.
https://facts-engineering.github.io/modules/P1-04RTD/P1-04RTD.html
Do note the temperature modules take longer to initialize after calling P1.init(). So if you're taking readings right away in your program, you might get some bad data.