Sriram Iyer

while(!broken) { fix(); }

Arduino: Making a microSD Breakout

After much procrastination I’ve finally decided to bite it and make a microSD card breakout board for the Arduino. I want it to be as modular as possible so that I can use it for data logging as and when required. In the process of making this I ended up learning quite a lot. This post is a detailed, aggregated, and probably condensed account and may sometimes get into explaining trivial detail. But I’m writing this for future me, you know, the guy who’ll thank me for the detailed explanation.

Redditor ‘chrwei’ has made a keen observation that this is technically not a shield but a breakout board as shields directly mount on the arduino while this does not. I have made the changes where ever possible.

Arduino-SD card schematic

The SD cards are pretty nifty in the sense that they run over SPI. So it’s quite easy to interface them to the arduino using the SD library. It’s literally the SPI, you have a Chip Select, MOSI, MISO, and SCLK on the SD card along with the obviously required VCC and GND pins. Well, the SD card also runs over the SD Card bus interface which is a much faster interface as compared to SPI. That’s what is used when you connect the card to the PC or other devices. But we’re not get to use that, because it’s closed source. So, SPI it is. And it’s relatively easy to implement SPI over the arduino.

But there’s just one catch, the SD card operates at 3.3V for both, power and logic. And it isn’t 5V tolerant. That’s not really a problem for VCC since you can get 3.3 volts from the arduino board (if you’re using an arduino) or from your regulated power supply/battery cell.

With the power requirements taken care of, you now need to deal with the logic levels. The SD card requires the logic to also be at 3.3V levels. The arduino runs at 5V logic. A logical 1 on the arduino would be 5V, and if you send that voltage to the SD card, you might end up blowing the pin, since the voltage rating for SD-card is 2.7 – 3.6V. So, you’ll need to drop the SD input voltage from the Arduino from 5V to 3.3V or lower. How low can you go before it considers the input voltage as a logic 0? I couldn’t find the exact value in the SD specification but looking at some of the card manufacturers spec. sheet makes me assume it’s 0.625 x VCC. I’m going to take this as truth and go forward from here.

Assuming I’m able to supply a good quality 3.3V, the SD card should see atleast 2.06V to consider that signal as a logic 1. Logic 0 is quite easy… you just send 0V and it’ll be considered as a logic 0. Conversely, when the SD card sends a 0V, the arduino will see that as a logic 0. But the maximum voltage the card can send is 3.3V. Now we need to know if 3.3V is enough for the arduino to see it as logic 1. I tried to PWM a pin and feed it to another pin and read that as a digital input to get an approximate idea of when the arduino considers the input to be logic 1 and I got it at around 127-128 which roughly corresponds to 2.5V. So, yes, 3.3V is sent from the SD card is enough to trigger a logic 1 at the arduino’s end.

It had to be, or else thousands of guides across the internet would have promptly spoken about needing a transistor or something. But it’s always nice to know what actually is going on, and this gives you a general idea of what threshold voltage your arduino has. We’re also making sure we clear the minimum system requirements and now we know how much further ahead we are from the minimum required values. So, now that we’re theoretically clear on both ends we can actually start collecting material to build the actual board.

What we’re going to do is set up a voltage divider on each of the pins that go from the arduino to the SD card (by which I mean, the SD card terminals act as inputs) because we want to drop the voltage from 5V to something less than 3.3V. For the pin that goes from the SD card to the arduino we don’t need to worry since our arduino is both, capable of handling 3.3V (5V actually), and can reliably see both logic 0 and 1 that the SD card sends. Wikipedia has a really good explanation of a voltage divider. In this setup what happens is, the 5V signal is reduced to a fraction of itself depending on the resistance values you select. I found this to be a really good resource for calculation of resistor values for the divider. It’s far quicker to use than calculating your resistor values manually. Quite a few guides on the internet advocate using 1K8 and 3K3 or 2K4 and 4K7 pairs of resistors for the voltage divider. If you plug in these values into the calculator you’ll get something around 3.3V which is fine (since the card runs between 2.7 and 3.6V). But while researching on this topic, I came across a page where the author had used an oscilloscope to see the rise time. Although I lost the website, I distinctly remember that with higher values of resistor pairs, the longer it took to reach full voltage. It shouldn’t make a lot of difference, but I decided to go for the lowest valid pair that I could come up with. Rummaging through my resistor box, I figured out that the 150ohm and 220ohm makes for a good pair. The equivalent output voltage was around 2.97V which is plenty for a logic 1 to be registered (we need 2.06V if you remember from above).

Alright, now that the theory was out of the way it was time to go collect all the parts that I needed to make this. The requirements are pretty basic actually. To make an SD card breakout board that will be compatible with the arduino I needed the following items:

  • Three pairs of resistors for the voltage divider (I used 3x150ohm and 3x220ohm 1/8W resistors)
  • One microSD card to full-size SD card adapter (Personally, if this is your first big project it always helps to have an extra as we shall soon see)
  • A prototyping board
  • Male header pins (I had a 2x40pin which I broke into a 2x7)
  • Female header pins (I used two pieces of 1x7)
  • Some wires (I cut open an ethernet cable and pulled out the wires from it)
  • Soldering iron
  • Soldering flux
  • Solder

Securing the SD-card to the pins for ease of soldering The other side of the holder setup

So, the first step is to solder the SD card adapter to the header pin. How should one do that? In my case it was quite simple actually. Since I had a 2x7, I just placed the card in between the two pins and stuffed a plastic card behind the SD card and went to town with the soldering. Being my first soldering job in quite a few years, it took some time but nothing that a hot rod and solder can’t fix, right? So once the soldering was done, I thought it would be wise to test it on the breadboard with the resistors so that I get a general idea of what I’m going to do. After quickly wiring it all up, I tested the voltage divider and the output voltage was around 2.8V which is good enough. Then I downloaded the very helpful Adafruit SD card library and replaced the default one with it. The CardInfo sketch is the one you want. It’s at the Arduino Examples->SD->CardInfo.

Testing – Phase 1

I didn’t want to disturb anything and decided that I’ll test it with the card in the holder position. Okay, so the cardinfo sketch ran, but it was a bit iffy. Resetting the board would yield unpredictable results. Maybe, the giant plastic card has something to do with this. Let’s remove it and then test the setup.

Testing – Phase 2

And, there was nothing. Nope. The CardInfo sketch threw up an error. On further inspection it turns out that the pins weren’t actually touching the SD card contacts, and that the plastic card which I’d been using as support while soldering ended up putting stress on the setup. A multimeter test confirmed the same. Now, being one adapter poorer, but infinitely wiser(hey, from zero any progress is relatively infinite isn’t it?) I pulled out my second and final adapter.

Second and final adapter, it’s either now or never

This time, I made sure not to allow any strain while soldering the pins. I put a thin visiting card (which thankfully possesses better deformation properties than plastic).

Soldering complete, now to test. Hmm, this stage looks surprisingly familiar. Also, I need to refine my soldering skills

Taking even more time to solder, I made it a point to add the flux this time around… which again, I had completely forgotten previously. Man, it was way easier to solder with the flux in the picture. Once this was done I decided to test contacts as well as non-contacts with the multimeter. I didn’t want a microSD card to blow up just because I had been a bit absent-minded to see if two pins were/weren’t shorted because of solder.

So this is what it looks like after soldering From a slightly different angle Gotta put a card before testing you know

With that done, I added a microSD card, plugged it into the breadboard and booted it. Success at last!(“At last”? On a more philosophical note, would you continue trying once you’ve succeeded?) The CardInfo sketch lists all information about the currently inserted card and all files on the card. So, how do you solder an SD card adapter to the header pin? Very carefully.

Testing – success

With my basic prototype done, it was time to put it on the protoboard. I wanted the card to sit flush with the board surface. I could have tried to bend the pins on the card to right angles but there was no way I was touching that finally working piece of my project. Also, I didn’t have a third SD-card adapter if things went south. Desperate times call for desperate measures.

My workaround to get the card fix flush to the board. A bent female header.

So I broke out 7 female header pins and bent the pins into a neat right angle. This enables you to fix the headers horizontally on the board and fix your adapter card. This step took me quite a while because I thought I’d be smart and connect the wires right with the header.

Unrelated picture of de-snaked CAT5 just to make this post look more awesome

Since I used the ethernet cable, I had 8 different wires and I could color code the points to my fancy. The basic communication connections required are CS, MOSI, MISO, and SCLK, with VCC and GND being for power.

This is how it looks from top so far And this is how it looks from the bottom. It is a mess of wires.

I soldered all the wires with the power wires in one direction and the comm. wires in the other. Otherwise, it seemed that the area would get quite messy with wires, and I’m not that great with the soldering iron that I can make pin point accurate connections. After soldering each connection, I made it a point to check for two things – (1) a continuity test between the farthest point in the wire and the other side of the header pin, and (2) a discontinuity test at the point of solder with other pins (You can see that I’m testing everything and think, wow, this guy checks everything systematically whereas in truth I’m just really distrustful of my soldering skills :D).

Once I had soldered all 7 points on the board, I kind of realized that I soldered myself into a corner. I really hadn’t planned where I’d put the resistors and the microcontroller connection “socket”. Well, I’d come too far to start over again now and so I was iterating over various locations to place the resistors and found a pretty decent place to stick the voltage divider setup. You need voltage dividers for CS, MOSI, and SCLK since these are signals from the arduino to the sd card. You can connect the MISO pin directly to the socket as this is the signal going from the sd card to the arduino.

This is what the back looks like once everything is soldered And this is what the front looks like. It looks surprisingly neat.

Some how, I found a decent place to put the resistors in. Although it meant that I’d need to do a lot of wire-twisting and turning soldering. After placing them all, and taking my time to solder the voltage divider arrangement I now had to find a place to put the other female header pins that are meant to connect to the microcontroller. Sure, it’s straight forward when you write it – just connect the various pins in a continuous fashion to the other pin but it took me quite a bit of time to master the soldering technique. Once the soldering is done, all that’s left is to test the board. But before testing it with the microcontroller, I checked the connections with the continuity tester. Surprisingly (or should I say shockingly :) ), all connections were perfect even though it looks like the T-1000 was involved. Connecting it to the microcontroller, I was pleasantly greeted with the directory listing of the SD card content.

Connected to the arduino The SD adapter is inserted, the header pins help to grip in case I need to remove it from the board. With everything connected, this setup works and is relatively robust

Project Status: Success

Future considerations: After running it for some time, I realized that this board assumes right off the bat that you have a 3V3 dedicated power line. Now, full sized arduino boards do come with 3V3 lines but you can’t have that when you’re testing a bare microcontroller. I don’t want to make another voltage divider because I honestly don’t know where I’d fit it. Instead, I’m going to put a zener diode. Of course, I somehow managed to buy a bunch of 7.5V zeners (don’t ask me what I was thinking when I bought them, I don’t know where I’m going to use them either) but I don’t have 3.3V ones. So I’ve just ordered some from ebay and it’ll be a few weeks till delivery.

Special mention: You know there’s a saying related to sharpening sickles and harvesting. Similar to that, throughout this project, one tool has been the most valuable is the solder cleaning tool. Here’s a picture of mine as a final closing of this project. Thanks, soldering iron cleaner, I couldn’t have done this without you. Truly the most useful tool in the entire project