SD Card on a Wemos Mega +WiFi R3 Module ATmega2560+ESP8266

Hi, having got over my inital problems with the Wemos® Mega +WiFi R3 Module ATmega2560+ESP8266 I am now moving onto the next steps which is adding an SD Card to the board.

The SD Card module I got where these from Banggood

From looking at the details it tells me it has a 5v-3.3v regulator built in, so I should not need to level shift (I presume so though I am a noob with memory issues so I could be wrong)

I have added the sd card module and connected the pins to...

  • MISO - 50
  • SCK - 52
  • MOSI - 51
  • CS - 53
  • Vcc to Mega 5v
  • Gnd to Mega Gnd

I then uploaded a simple script to the Mega but I just can not get the SD to initialised. The SD Card I have tried are all Fat32 formatted and in various sizes 2Gb, 4Gb 32Gb

The script I used was

#include <SPI.h>
#include <SD.h>

int CS_PIN = 53;

File file;

void setup()
{

  Serial.begin(57600);

  initializeSD();
  createFile("test.txt");
  writeToFile("This is sample text!");
  closeFile();

  openFile("prefs.txt");
  Serial.println(readLine());
  Serial.println(readLine());
  closeFile();
}

void loop()
{
}

void initializeSD()
{
  Serial.println("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);

  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
}

int createFile(char filename[])
{
  file = SD.open(filename, FILE_WRITE);

  if (file)
  {
    Serial.println("File created successfully.");
    return 1;
  } else
  {
    Serial.println("Error while creating file.");
    return 0;
  }
}

int writeToFile(char text[])
{
  if (file)
  {
    file.println(text);
    Serial.println("Writing to file: ");
    Serial.println(text);
    return 1;
  } else
  {
    Serial.println("Couldn't write to file");
    return 0;
  }
}

void closeFile()
{
  if (file)
  {
    file.close();
    Serial.println("File closed");
  }
}

int openFile(char filename[])
{
  file = SD.open(filename);
  if (file)
  {
    Serial.println("File opened with success!");
    return 1;
  } else
  {
    Serial.println("Error opening file...");
    return 0;
  }
}

String readLine()
{
  String received = "";
  char ch;
  while (file.available())
  {
    ch = file.read();
    if (ch == '\n')
    {
      return String(received);
    }
    else
    {
      received += ch;
    }
  }
  return "";
}

I then tried using the Mega 3.3v instead of the 5v and it still does not initialised.

I tried using a different pin for the CS but still does not get initialised.

The thing that is bugging me is that if I use my Uno (using the pins of the uno 10, 11, 12, 13) then the SD can be initialised and be write/read fine.

I have read many posts and tried various other things but to no avail.

any help or opinion would be most greatfull.

The SD library defaults to using pin 4 for CS. If you're using a pin other than 4, you should specify it as a parameter to SD.begin():

Thanks for the reply. I added CS_PIN to the SD call

eg if (SD.begin(CS_PIN)) but still I can not initialise the SD Card. I even tried using pin 4 on the Mega and changing CS_PIN to 4 but still nothing.

Just don't know why yet but any other possible solutions or comments will still be gratefully received.

use 53 if you connected CS to 53. if you use 4, you must set 53 as OUTPUT for SPI in master mode.

try the CardInfo example

Try using 49 pins instead of 53 and check the Vin electricity for sure.

taweetra:
Try using 49 pins instead of 53 and check the Vin electricity for sure.

just so? some arguments? any reason?