Activity › Forums › Questions & Troubleshooting › Controllino MEGA – SD Card Logging
-
Controllino MEGA – SD Card Logging
-
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?
Log in to reply.