Hi everyone,
I posted this elsewhere but realised it would fit better here.
I just started a new project and want to log some sensor data to an SD card.
The code that I am using is the one found under file -> example -> SD -> datalogger, but I have also tried using the code from different websites but all have had the same problem, therefore I doubt that it is software related, but I may be wrong.
after uploading the code and opening the serial monitor, the first message I get is: "card failed, or not present."
To get it to work I need to take the sd card out of the module and put it back in before pushing the reset button. And every time that I want to reset the Arduino (UNO btw) I have to take the sd card back out and in again for it to work, don't know if that is normal...
After getting the: card initialized message, it prints out the three sensor values once but logs nothing. then I just get: "error opening datalog.txt." Twice in 30 - 50 tries, it logged the values about two times before going back to the error message.
when inserting the SD card into the computer, it sometimes shows strange files and folders named tings like ®└/00.@. These can not be opened nor deleted, the only thing that works is formatting the SD card.
I am using a 16 GB SanDisk ultra micro SD card with an adaptor, so maybe that is the problem? I don't know if there is a limit to the storage capacity of the card that the Arduino can handle. I have also tried to use the SD card normally on the computer and that worked fine, so there is nothing wrong with the SD card itself it seems. When I run the CardInfo script the card seems to fit all requirements for it to work (SDHC card and formated FAT32).
I did update my SD library as I found someone else here that had that problem but that did not change anything.
I hope that I have put out all the necessary information and that someone can help.
If so, thanks in advance.
Hi Peter, thank you for replying.
Yes, I did indeed use the IDE example datalogger (and a few other but similar sketches before that) so I don't think the problem lies there.
I have also checked my wiring multiple times as well as changing SD card module and Arduino, but none of that has helped.
what was the storage capacity of your SD card?
Hi Peter,
I just tried with a brand new card (32GB again) as well as an older formatted 4GB SD card but they did not solve the issue. both are Ultra SD cards (don't have anything else on hand rn). I feel like that should not be a problem, but maybe that the Arduino can't cope with that?
But I can't see any other possible problem as the Arduino can recognise the card and interact with it as it should when using the IDE example CardInfo script, the only thing it can't do is reliably creating or opening the file to log the data...
Hi Peter,
here is a link to the exact SD card module I have:
https://www.amazon.de/kwmobile-Modul-Arduino-Andere-Microcontroller/dp/B06XH3FCN1/ref=sr_1_8?crid=90O39DLXJWY3&dchild=1&keywords=arduino+sd+card+modul&qid=1597056580&sprefix=arduino+SD%2Caps%2C190&sr=8-8
This type of card reader seems to be relatively common but there are probably many different manufacturers which may change the quality. But I doubt that this is the problem as it does have a relatively good review and a lot of people seem to have made them work
the SD cards are, as I mentioned before SanDisk Ultra 16GB, 32GB, and an older 4GB card (all of them are SDHC).
The Arduino is just a standard Arduino UNO (I have tried with both an official Arduino UNO and a clone)
I found a video from 2018 where the person said that an SD card of more than 2GB would work. I suppose that this has improved since it is hard to get an SD card with such low storage capacity, but that does not mean that it works with an Ultra SD card.
If you (or anybody else reading this) have the possibility to test and/or confirm that an Ultra SD card works that would be greatly appreciated, as that is the most likely issue I think.
Best regards,
Bruno
hi Peter,
your sketch does work.
If you look at pic1 under the attached pictures you will see that I get about the same result. What is strange though is if you look at the lower half of that picture, you will see the files found on the card. these are named some jibberish that the computer can't understand and can not interact with (pic4 shows what it looks like on the computer). These files are created by the Arduino using the script below. (pic2 shows where I found it)
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
analog sensors on analog ins 0, 1, and 2
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
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
}
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");
}
}
using this code or any other similar code that has to create or interact with the files on the card will give a result similar to what is shown on pic3, I also found an old 2GB SD card and it gave the same result, so from this and your test, we can probably conclude that the issue is not the card itself but lies elsewhere.
My best guess now is that both my SD card modules are dysfunctional since the only other factor is the Arduino itself and I have used 3 different ones, that all 3 never have had an issue before. So I think that I will have to buy other ones.
Thank you for taking your time to help, and if you come up with something else that might cause the problem please let me know.
Kind regards,
Bruno