Arash (Customer) asked a question.

Hello,
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

}


  • FACTS_AdamC (AutomationDirect)

    Apologies, I missed this line up top.

    1. const int SD_CS_PIN = 4; // SD card CS pin connected to P1AM slot 1 pin 4

    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.

    1. const int SD_CS_PIN = SDCARD_SS_PIN;

     

    Expand Post
    Selected as Best
  • FACTS_AdamC (AutomationDirect)

    Can you try replacing "SD_CS_PIN" with "SDCARD_SS_PIN"

  • Arash (Customer)

    I will do it now. See if works. Thanks for your message

    • FACTS_AdamC (AutomationDirect)

      Apologies, I missed this line up top.

      1. const int SD_CS_PIN = 4; // SD card CS pin connected to P1AM slot 1 pin 4

      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.

      1. const int SD_CS_PIN = SDCARD_SS_PIN;

       

      Expand Post
      Selected as Best
  • Arash (Customer)

    Thank you so much for your help. It is now starting to write the data. However, when I open the stored data file from SD card, I don't see any number stored in it. Is there any thing that I am missing to do?

    • FACTS_AdamC (AutomationDirect)

      I believe you just need to call your finish() function to complete the writes.

  • Arash (Customer)

    I did it and it is working now. I can not find a word to thank you enough! I really appreciate your help

  • Arash (Customer)

    Hi.

     

    Thanks for your previous help.

     

    Is there any chance you could help with my question?

     

    I am using P1-01AC+P1AM-100+P1-04RTD+PT1000 (channel one) and using the following code to read the temperature from the pt1000 sensor. It reports that there is no any sensor module connected to the first channel (it reads --328 'C for all of 4 channels). Could you please help me with that?

    Expand Post
  • Arash (Customer)

    #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-6; // 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

      }

    }

    Expand Post