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





Friday, January 3, 2014

Shileds FAIL with Arduino Mega.

Shortly after the astounding success of my garage parking sensor, I decided to try my luck with something that on its face should have been really really really easy.  I wanted to play with some 'Shields'.  For those of you who have not yet played with Arduinos - shields are small boards designed to easily plug on top of an Arduino to provide additional capabilities.  They can theoretically stack on top of each other.
A Stack of Shields
GPRS Shield
SD Card Shield

 

I wanted to install a GPRS module on top of my Arduino Mega 2650.  Should have been a piece of cake.  I got a small SIM adapter (little plastic holder that allows the small modern SIM cards to be inserted into the SIM card holders designed for full-size cards) , pulled the card out of my AT&T HTC One and put it in the GPRS module.  Then as instructed on SeedStudio's Wiki I plugged my shield into the Arduino, downloaded and installed the example....  And nothing happened.  I re-read the instructions.  Tweaked stuff.  Nothing happened.  I googled hard.  Nothing useful.Nothing useful. 

Not quite true.  The GPRS Shield has a red light and a green light.  The red light shows the unit is ready.  The green light blinks fast when the unit is first turned on and then starts blinking slowly when network connection is established.  This happened.  This, however, should have been accompanied by output on the serial terminal.  Which never ever happened.  I thought I had a defective unit. 

For Christmas I had bought a friend a Mega and a GPRS shield as well in the hope of a joined project.  And when swapping components we did not achieve success, I felt there was something fundamental missing. 

I gave up after a couple of evenings of frustration.  My friend did not get it either.  I will describe the solution is a follow up post.  After we talk about the SD Shield in the next post.

Hint: It has to do with Arduino MEGA being just a little different, from other Arduino...  Not that anyone would ever tell you that...