One thing about working with Megas (and I’m told Leonardos) is that you can occasionally have pin issues. Megas and Unos (Decis, etc) are a little different in pin configurations. If you buy a shield from Adafruit, Sparkfun, or another site, some of them hav their own library and you’ll need to look at their help to find your answer. For this, we’re going to wire up a 2GB Sandisk.
You can see in the image above that I’m only going to use the first six pins of the SD card breakout board.
CS – Chip Select
D1 – MOSI
VCC – 3.3v
CLK – Clock
GND – Ground
D0 – MISO
If you open up the source of any of the SD card examples in the Arduino libraries you’ll see that they (almost) all have the same comment
** MOSI – pin 11
** MISO – pin 12
** CLK – pin 13
** CS – pin 4
This is not for a Mega. Remember the Mega has a different chip layout – these pins will be:
** MOSI – D1 – pin 51
** MISO – D0 – pin 50
** CLK – pin 52
** CS – pin 53
There are a couple of changes you need to make to the Datalogger sketch in the examples:
change const int chipSelect = 4;
to const int chipSelect = 53;
change pinMode(10, OUTPUT);
to pinMode(53, OUTPUT);
– just remember that if you decide to use this sketch on a non-mega you may have to change those values back.
(or you could change chipSelect to 53 and then pinMode(chipSelect, OUTPUT);
That’s it – The Datalogger sketch will then put a file on your SD card and populate it with lines of 3 sets of comma delimited digits.