Monday, January 6, 2014

Getting an SD Shield to Work With Arduino Mega

As I already talked in the previous post, my experience with Shields and the Arduino Mega was not good.  I first tried a GPRS shield, and then an SD shield, and both were not happy experiences...  But enough complaining.  Lets get into what actually works.

The short of the story is that the Arduino Mega does not work with most shields.  They plug in nice, but the pins they expect to use do not function the way that is expected.  It comes down to the SPI pins.  On most the most popular Arduinos ( Uno and Duemilanove ) they are 10 through 13 (technically 11 through 13 and 10 as chip selector since the job of the 10 can be done by other pins).  On the Mega it is 50 - 53 with 53 being chip select.  This is all described in the SPI documentation, but SD Shield documentation does not really talk about the fact that it is using SPI or what SPI is.   The same is true for the GRPS Shield.

Early Unsuccessful Attempt

For those of you who are steeped in electronics, it will probably sound hilarious that when something uses 20+ pins to plug into something else, I expect that pretty much all of them are used in some mysterious fashion.  Understanding that only 4 pins are really all the pins that are being used ( in addition to power and ground ) was a bit of a revelation.  And even as my brain was going through that piece of mental re-adjustment I was running into a problem with all the documentation and examples of the SD Shield.  Which talked about the Chip Select pin being a number of possible pins (4, 8, 10) but never 53.

The final working design is really simple  Simply bend out the SPI pins on the shield and jumper them from the top side.  Then modify the example code to use pin 53 as chip select.  Or Slave Select.  Or SS.  Whatever your example talks about.

Notice the 4 bent pins sticking out of the side of the shield.

Then just jumper the 4 wires


You can get example code and adapt it from Seed Studio.

The solution pictured above is not optimal as it makes stacking additional shields a bit problematic.  But it works.  And it is simple.  And if you really want to stack more shields, route your wires better and get your solder out.  Or wires capped with female plugs...  the possibilities are endless...

During this research I learned that some effort for standardization is actually being made.  The Arduinos have something called ICSP Header.  Which has everything you need to run an SPI shield.  It is the set of 6 pins sticking out from the middle of your Arduino.  Those are pretty much hard wired to the corresponding pins on the side of the Arduino.  So its not like you have and extra SPI port.  And its in the same place on all? (I hope) Arduinos.
ICSP Header on Arduino... something or other
Newer shields, like the Seed Studion SD Card shield V4.0 are setup to have and use the ICSP header which should help future users not to want to end their life in despair...  Or at least cause significant bruising to forehead due to frequent collisions with wall...
SD Card Shield with ICSP Header