CDMania (Customer) asked a question.

P1AM-100 SD card datalog issues

I am trying to get the uSD card slot to work on my P1AM-100. I am using the example code and changed the chipselect pin to 28. This still get the "card failed, or not present" every time. The file "datalog.txt" exists on the SD card, it is a 4gb uSD card. I have it formated as FAT (I have tried multiple formates just to verify that wasn't the issue.

 

Does anyone know what i am doing wrong? Is there a certain formate that the P1AM likes best?

 

Thanks,

 

#include <P1AM.h>

#include <SPI.h>

#include <SD.h>

 

const int chipSelect = 28;

 

void setup() {

  // Open serial communications and wait for port to open:

  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }

 

  delay (4000);

  Serial.print("Initializing SD card...");

 

  // see if the card is present and can be initialized:

  if (!SD.begin(chipSelect)) {

    Serial.println("Card failed, or not present");

    // don't do anything more:

    while (1);

  }

  Serial.println("card initialized.");

}

 

void loop() {

  // make a string for assembling the data to log:

  String dataString = "";

 

  // read three sensors and append to the string:

  for (int analogPin = 0; analogPin < 3; analogPin++) {

    int sensor = analogRead(analogPin);

    dataString += String(sensor);

    if (analogPin < 2) {

      dataString += ",";

    }

  }

 

  // open the file. note that only one file can be open at a time,

  // so you have to close this one before opening another.

  File dataFile = SD.open("datalog.txt", FILE_WRITE);

 

  // if the file is available, write to it:

  if (dataFile) {

    dataFile.println(dataString);

    dataFile.close();

    // print to the serial port too:

    Serial.println(dataString);

  }

  // if the file isn't open, pop up an error:

  else {

    Serial.println("error opening datalog.txt");

  }

}


  • AL9000 (Customer)

    I have this code, which does work with my P1AM, I assume I got it from facts-engineering examples somewhere. I have a 256 MB uSD card installed.

     

    It doesn't log data, just verifies the SD card, but it may get you going in the right direction. Two quick differences I see are, a) this sketch includes <SPI.h>, not sure it is required, but the SD card does use SPI,

    1. #include <SPI.h>

    and, b) you are very specific about the chipSelect pin 28, why not use the default:

    1. const int chipSelect = SDCARD_SS_PIN; // SDCARD_SS_PIN works with p1am PLC

    Here's the whole sketch:

    1. /*
    2. SD card test --> works with P1AM PLC
    3. This example shows how use the utility libraries on which the'
    4. SD library is based in order to get info about your SD card.
    5. Very useful for testing a card when you're not sure whether its working or not.
    6. The circuit: SD card attached to SPI bus as follows:
    7. ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
    8. ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
    9. ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
    10. ** CS - depends on your SD card shield or module.
    11. Pin 4 used initially, for consistency with other Arduino examples
    12. my note: guess at their suggestion, CS is SDCARD_SS_PIN (internal name, same as mkrZero --> yes, this works p1am PLC
    13. created 28 Mar 2011
    14. by Limor Fried
    15. modified 9 Apr 2012
    16. by Tom Igoe
    17. */
    18. // include the SD library:
    19. #include <SPI.h>
    20. #include <SD.h>
    21.  
    22. // set up variables using the SD utility library functions:
    23. Sd2Card card;
    24. SdVolume volume;
    25. SdFile root;
    26.  
    27. // change this to match your SD shield or module;
    28. // Arduino Ethernet shield: pin 4
    29. // Adafruit SD shields and modules: pin 10
    30. // Sparkfun SD shield: pin 8
    31. // MKRZero SD: SDCARD_SS_PIN
    32. //const int chipSelect = 4;
    33. const int chipSelect = SDCARD_SS_PIN; // SDCARD_SS_PIN works with p1am PLC
    34.  
    35. void setup() {
    36. // Open serial communications and wait for port to open:
    37. Serial.begin(9600);
    38. while (!Serial) {
    39. ; // wait for serial port to connect. Needed for native USB port only
    40. }
    41.  
    42. Serial.print("\nInitializing SD card...");
    43.  
    44. // we'll use the initialization code from the utility libraries
    45. // since we're just testing if the card is working!
    46. if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    47. Serial.println("initialization failed. Things to check:");
    48. Serial.println("* is a card inserted?");
    49. Serial.println("* is your wiring correct?");
    50. Serial.println("* did you change the chipSelect pin to match your shield or module?");
    51. while (1);
    52. } else {
    53. Serial.println("Wiring is correct and a card is present.");
    54. }
    55.  
    56. // print the type of card
    57. Serial.println();
    58. Serial.print("Card type: ");
    59. switch (card.type()) {
    60. case SD_CARD_TYPE_SD1:
    61. Serial.println("SD1");
    62. break;
    63. case SD_CARD_TYPE_SD2:
    64. Serial.println("SD2");
    65. break;
    66. case SD_CARD_TYPE_SDHC:
    67. Serial.println("SDHC");
    68. break;
    69. default:
    70. Serial.println("Unknown");
    71. }
    72.  
    73. // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
    74. if (!volume.init(card)) {
    75. Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    76. while (1);
    77. }
    78.  
    79. Serial.print("Clusters: ");
    80. Serial.println(volume.clusterCount());
    81. Serial.print("Blocks x Cluster: ");
    82. Serial.println(volume.blocksPerCluster());
    83.  
    84. Serial.print("Total Blocks: ");
    85. Serial.println(volume.blocksPerCluster() * volume.clusterCount());
    86. Serial.println();
    87.  
    88. // print the type and size of the first FAT-type volume
    89. uint32_t volumesize;
    90. Serial.print("Volume type is: FAT");
    91. Serial.println(volume.fatType(), DEC);
    92.  
    93. volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
    94. volumesize *= volume.clusterCount(); // we'll have a lot of clusters
    95. volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
    96. Serial.print("Volume size (Kb): ");
    97. Serial.println(volumesize);
    98. Serial.print("Volume size (Mb): ");
    99. volumesize /= 1024;
    100. Serial.println(volumesize);
    101. Serial.print("Volume size (Gb): ");
    102. Serial.println((float)volumesize / 1024.0);
    103.  
    104. Serial.println("\nFiles found on the card (name, date and size in bytes): ");
    105. root.openRoot(volume);
    106.  
    107. // list all files in the card with date and size
    108. root.ls(LS_R | LS_DATE | LS_SIZE);
    109. }
    110.  
    111. void loop(void) {
    112. // no loop used
    113. }

     

    Expand Post
    Selected as Best
  • CDMania (Customer)

    I have actually tried that one as well. Didn't work. I tried again without creating the "datalog.csv" file on the uSD card and it didn't work. Then created the file with my computer on the uSD card and tried again. No luck. The serial monitor just keeps saying.

     

    Initializing SD card...Card failed, or not present

    error opening datalog.csv

  • AL9000 (Customer)

    I have this code, which does work with my P1AM, I assume I got it from facts-engineering examples somewhere. I have a 256 MB uSD card installed.

     

    It doesn't log data, just verifies the SD card, but it may get you going in the right direction. Two quick differences I see are, a) this sketch includes <SPI.h>, not sure it is required, but the SD card does use SPI,

    1. #include <SPI.h>

    and, b) you are very specific about the chipSelect pin 28, why not use the default:

    1. const int chipSelect = SDCARD_SS_PIN; // SDCARD_SS_PIN works with p1am PLC

    Here's the whole sketch:

    1. /*
    2. SD card test --> works with P1AM PLC
    3. This example shows how use the utility libraries on which the'
    4. SD library is based in order to get info about your SD card.
    5. Very useful for testing a card when you're not sure whether its working or not.
    6. The circuit: SD card attached to SPI bus as follows:
    7. ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
    8. ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
    9. ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
    10. ** CS - depends on your SD card shield or module.
    11. Pin 4 used initially, for consistency with other Arduino examples
    12. my note: guess at their suggestion, CS is SDCARD_SS_PIN (internal name, same as mkrZero --> yes, this works p1am PLC
    13. created 28 Mar 2011
    14. by Limor Fried
    15. modified 9 Apr 2012
    16. by Tom Igoe
    17. */
    18. // include the SD library:
    19. #include <SPI.h>
    20. #include <SD.h>
    21.  
    22. // set up variables using the SD utility library functions:
    23. Sd2Card card;
    24. SdVolume volume;
    25. SdFile root;
    26.  
    27. // change this to match your SD shield or module;
    28. // Arduino Ethernet shield: pin 4
    29. // Adafruit SD shields and modules: pin 10
    30. // Sparkfun SD shield: pin 8
    31. // MKRZero SD: SDCARD_SS_PIN
    32. //const int chipSelect = 4;
    33. const int chipSelect = SDCARD_SS_PIN; // SDCARD_SS_PIN works with p1am PLC
    34.  
    35. void setup() {
    36. // Open serial communications and wait for port to open:
    37. Serial.begin(9600);
    38. while (!Serial) {
    39. ; // wait for serial port to connect. Needed for native USB port only
    40. }
    41.  
    42. Serial.print("\nInitializing SD card...");
    43.  
    44. // we'll use the initialization code from the utility libraries
    45. // since we're just testing if the card is working!
    46. if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    47. Serial.println("initialization failed. Things to check:");
    48. Serial.println("* is a card inserted?");
    49. Serial.println("* is your wiring correct?");
    50. Serial.println("* did you change the chipSelect pin to match your shield or module?");
    51. while (1);
    52. } else {
    53. Serial.println("Wiring is correct and a card is present.");
    54. }
    55.  
    56. // print the type of card
    57. Serial.println();
    58. Serial.print("Card type: ");
    59. switch (card.type()) {
    60. case SD_CARD_TYPE_SD1:
    61. Serial.println("SD1");
    62. break;
    63. case SD_CARD_TYPE_SD2:
    64. Serial.println("SD2");
    65. break;
    66. case SD_CARD_TYPE_SDHC:
    67. Serial.println("SDHC");
    68. break;
    69. default:
    70. Serial.println("Unknown");
    71. }
    72.  
    73. // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
    74. if (!volume.init(card)) {
    75. Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    76. while (1);
    77. }
    78.  
    79. Serial.print("Clusters: ");
    80. Serial.println(volume.clusterCount());
    81. Serial.print("Blocks x Cluster: ");
    82. Serial.println(volume.blocksPerCluster());
    83.  
    84. Serial.print("Total Blocks: ");
    85. Serial.println(volume.blocksPerCluster() * volume.clusterCount());
    86. Serial.println();
    87.  
    88. // print the type and size of the first FAT-type volume
    89. uint32_t volumesize;
    90. Serial.print("Volume type is: FAT");
    91. Serial.println(volume.fatType(), DEC);
    92.  
    93. volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
    94. volumesize *= volume.clusterCount(); // we'll have a lot of clusters
    95. volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
    96. Serial.print("Volume size (Kb): ");
    97. Serial.println(volumesize);
    98. Serial.print("Volume size (Mb): ");
    99. volumesize /= 1024;
    100. Serial.println(volumesize);
    101. Serial.print("Volume size (Gb): ");
    102. Serial.println((float)volumesize / 1024.0);
    103.  
    104. Serial.println("\nFiles found on the card (name, date and size in bytes): ");
    105. root.openRoot(volume);
    106.  
    107. // list all files in the card with date and size
    108. root.ls(LS_R | LS_DATE | LS_SIZE);
    109. }
    110.  
    111. void loop(void) {
    112. // no loop used
    113. }

     

    Expand Post
    Selected as Best
  • CDMania (Customer)

    Thank you AL9000. This code made it clear that it was detecting an SD card but it told me that the card was not formated correctly. I did a long format for FAT32 and it worked. I also had a 64G uSD card and I made a partition for 32G in Fat32 and left the rest of the card un-allocated and that also worked. Thank you very much for your help. I guess it was a formatting issues this whole time.