SD Card and Arduino Mega

I purchased this awesome little SD card breakout from Sparkfun about 2 years ago https://www.sparkfun.com/products/12941 – a lot of family issues came up and I wasn’t able to work on it with the kids like I wanted to.  With some recent work I’m doing with sensors (more blog entries to follow) I also know I’ll need to log some to an SD card.

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.