
Arash (Customer) asked a question.
I have been trying for weeks but I couldn't set up a code read the temperature from P1-04RTD and store data into SD inserted into P1-AM100. Here is my code: (I got SD INITIALIZATION FAILED!)
#include <Arduino.h>
#include <P1AM.h>
#include <SPI.h>
#include <SD.h>
const int SD_CS_PIN = 4; // 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-6; // constant A for PT1000 RTD
const float B = -5.775e-7; // constant B for PT1000 RTD
float temperature; // variable to store temperature reading
float resistance; // variable to store resistance calculation
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.txt", 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() {
temperature = P1.readTemperature(1, 1); // read temperature from input channel 1 in slot 1
resistance = (VCC*R_REF*temperature)/(VREF*(VCC-temperature)); // calculate resistance using voltage divider circuit equation
myFile.print(millis()); // write current time to file
myFile.print(","); // separate time and temperature with comma
myFile.println(temperature); // write temperature to file and add newline character
Serial.print("Temperature: "); // print a label for the temperature reading
Serial.print(temperature); // print the temperature value
Serial.println(" degrees Celsius"); // print the unit of measurement for temperature
delay(5000); // wait for 5 seconds
}
void finish() {
myFile.close(); // close file when program ends
}
Apologies, I missed this line up top.
CS for the P1AM SD card is 28, not 4 as shown here. However, SDCARD_SS_PIN is automatically set to 28, so you can simply change the line to the below if you don't want to make any other code tweaks.