Activity Forums Questions & Troubleshooting Controllino MEGA – SD Card Logging

  • Controllino MEGA – SD Card Logging

  • LeonardClassen

    Member
    October 5, 2023 at 12:42 pm

    Hi all,

    I am using the Controllino MEGA with a HW-125 SD Card module.
    https://components101.com/modules/micro-sd-card-module-pinout-features-datasheet-alternatives.

    I use the following code:

    #include <controllino.h></controllino.h>

    #include <sd.h></sd.h>

    #include <spi.h></spi.h>

    File myFile;

    int pinCS = 53; // Pin 10 on Arduino Uno

    void setup() {

    Serial.begin(9600);

    Serial.println(“start setup”);

    // needs Controllino.h import

    Controllino_RTC_init(); //FAQ#7: https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library#faq

    //pinMode(pinCS, OUTPUT);

    pinMode(10, OUTPUT);

    digitalWrite(10, HIGH);

    // SD Card Initialization

    if (SD.begin())

    {

    Serial.println(“SD card is ready to use.”);

    } else

    {

    Serial.println(“SD card initialization failed”);

    return;

    }

    // Create/Open file

    myFile = SD.open(“test.txt”, FILE_WRITE);

    // if the file opened okay, write to it:

    if (myFile) {

    Serial.println(“Writing to file…”);

    // Write to file

    myFile.println(“Testing text 1, 2 ,3…”);

    myFile.close(); // close the file

    Serial.println(“Done.”);

    }

    // if the file didn’t open, print an error:

    else {

    Serial.println(“error opening test.txt”);

    }

    // Reading the file

    myFile = SD.open(“test.txt”);

    if (myFile) {

    Serial.println(“Read:”);

    // Reading the whole file

    while (myFile.available()) {

    Serial.write(myFile.read());

    }

    myFile.close();

    }

    else {

    Serial.println(“error opening test.txt”);

    }

    }

    void loop() {

    // empty

    }

    However, I get the error:

    SD card initialization failed

    I use the following pins on the controllino:

    CS / SS is pin53.

    What am I doing wrong?

Viewing 1 of 1 replies

Log in to reply.

Original Post
0 of 0 posts June 2018
Now