Go Down

Topic: Datalogger for SD Card not working (Read 959 times) previous topic - next topic


I am using an Arduino Mega board and a Velleman VMA202 Data Logging Shield to save measurements on an SD Card. To test if everything works, I tried running the Datalogger script from the standard library. However, I keep getting the error message : "Card failed or not present". I have changed the Chipselect Pin from 4 to 10 as suggested in the datasheet of my Data Logging Shield. TThe Cardinfo script works fine; I get my card type, size and files present on the card, so the card clearly works and can be detected.  Is anyone familiar with this problem and what should I do to fix it?

My Code:
  SD card datalogger
 This example shows how to log data from three analog sensors
 to an SD card using the SD library.
 The circuit:
 * SD card attached to SPI bus as follows:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header
       Pin 4 used here for consistency with other Arduino examples
 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>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

File dataFile;

void setup()
 // Open serial communications and wait for port to open:
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(53, OUTPUT);
  // 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.");
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;

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 += ",";


  // print to the serial port too:
  // The following line will 'save' the file to the SD card after every
  // line of data - this will use more power and slow down how much data
  // you can read but it's safer!
  // If you want to speed up the system, remove the call to flush() and it
  // will save the file only every 512 bytes - every time a sector on the
  // SD card is filled with data.
  // Take 1 measurement every 500 milliseconds


I think it doesn´t work because the SPI pins on the Mega (52-SCK, 50-MISO, 51-MOSI) aren´t the same as on the Uno (13-SCK, 12-MISO, 11-MOSI).

Have you tried this:
"The ATmega2560 (VMA101) and ATmega32u4 Leonardo (VMA103) development boards do not use the same
hardware SPI pin-out. If you are using one of these boards, please specify the pins used for SPI communication with the SD card. For the VMA202, these are pins 10, 11, 12 and 13.

In the CardInfo sketch, modify line:

while (!card.init(SPI_HALF_SPEED, chipSelect)) {

while (!card.init(SPI_HALF_SPEED,1,11,12,13)) {

Also, an updated SD library is needed in order to avoid error messages. How to replace the SD library:
3. Download the updated SD library from the products page on www.velleman.eu. Make sure the Arduino®
IDE is not running.

4. Go to C:\Program Files\Arduino and create a new map, e.g. SD Backup.

5. Go to C:\Program Files\Arduino\libraries\SD and move all files and maps to your newly created map.

6. Extract the downloaded SD library into the now empty SD map. Make sure that the .h and .cpp files are
directly under C:\Program Files\Arduino\libraries\SD.

7. Start the Arduino® IDE"



Sep 04, 2018, 02:05 am Last Edit: Sep 04, 2018, 02:19 am by Nick_Pyner
Eimaster is correct and, essentially, you have made a really bad choice of board.

The gear uses SPI bus for SD and i2c bus for the RTC. BOTH are different on the Mega.

If you are handy with pliers and soldering iron, which I guess you are supposed to be with this board, then

You might check that the relevant proto holes align with the six ICSP pins that are on Mega. If so you cn put a sixpin cluster in there, clip the pins 11,12,13, and put jumpers therefrom the the new cluster

Clip pins A4,A5 and run jumpers to pins 20,21 on Mega.

I would be very suss about any "replacement library". It sounds like Velleman are trying to use software to cover up a hardware failing that should never have been there in the first place. Having said that, they never claimed it was kosher for Mega 

Looking more closely at a pic, they appear to have seen you coming. They provide solder points for the SDA/SCL, and you can see that the ICSP pins are marked, so I think you can be sure they are aligned.

You might also check the SDA/SCL pins near the reset button. They are an alternative and directly wired to A4,A5. My Megas don't have those pins but, if yours does, I guess it is wired direct to pins 20,21, and will elegantly solve your problem there.

Go Up