MicroSD Breakout With Level Shifter Hookup Guide a learn.sparkfun.com tutorial
Available online at: http://sfe.io/t452
Introduction
The MicroSD Breakout With Level Shifter makes it easy to add mass storage to your project, whether you’re working with a 3.3V system or a 5V system.
Figure 1. Shifting µSD Breakout Board
Required Materials
To follow along with this hookup guide, you will need the following:
Suggested Reading
If any of these subjects sound unfamiliar, considering reading the following tutorials before continuing on.
- What is an Arduino? - New to Arduino? Start here.
- Installing the Arduino IDE - A refresher on using the Arduino environment.
- Arduino Shield Basics - Covers shields from a general standpoint.
- Serial Peripheral Interface (SPI) - This tutorial shows you how to communicate with an SD card over SPI.
- SD Cards and Writing Images - Learn the basics of SD and microSD cards.
Hardware Overview
The SparkFun Shifting µSD Breakout is quite similar to the SparkFun microSD Transflash Breakout, but with the additional feature of being 3.0V to 5.0V tolerant for ease of use. No more discrete level shifting is required. That also makes it great for direct use with a single cell LiPo battery or similar.
One thing that makes this product stand out is that it is SPI_FULL_SPEED
stable. Some errors were seen in our testing with other products, but none have been caught using this board. We didn’t have NIST do our testing. We can’t rule out the influence of environmental factors or the host processor used, but if we were looking for stability and reliability at high speed, we’d use this board.
If your processor is capable of it, this board supports the use of even the fastest UHS µSD cards. We only tested to 25MHz, but it should be good to two to four times that. Today most Arduino type µControllers are only capable of SPI_HALF_SPEED
(6Mbps). Consider this board if you want a little future proofing or have a faster setup. The Arduino SD library is capable of SPI_FULL_SPEED
(25Mbps).
The SparkFun Shifting µSD is also a bit unique from its competitors in that it level translates all of its outputs back to the level of the hardware it’s connected to. Other parts make the fairly safe assumption that the inputs on the processor will read 3.3V as a high. This may not always be the case, and isn’t an assumption this board relies on, so you don’t have to bother worrying about it.
Assembly
There are numerous ways to wire up an equivalent circuit to the one used in this guide. That will all depend on the type of Arduino you are using, the availability of a breadboard, or the types of wires you have laying around. The required materials list above assumes you have access to one of the most popular Adrduino form factors and you will wire it directly to the Shifting µSD board with jumper wires. No breadboard required, and only minimal soldering to get a connection to the µSD board.
Here is how you would wire up the Shifting µSD to a RedBoard or Arduino Uno. From the Ardino SPI library: Warning: if the SS pin ever becomes a LOW INPUT then SPI automatically switches to Slave, so the data direction of the SS pin MUST be kept as OUTPUT. This is pin 10, so be careful.
Shifting µSD board wired to 5V RedBoard
Code Example
The example code for this product is a simple file logger that allows the user to write to a file on the µSD card using the Arduino IDE Serial Monitor (57600 baud). When the board boots you should see the following in the Serial Monitor:
Initializing SD card...Initialization done.
demoFile.txt doesn't exist. Creating.
Opening file: demoFile.txt
Enter text to be written to file. 'EOF' will terminate writing.
The last line of that bock of text is important to note. Your work is written to the µSD card every 20 characters, but to make sure everything is written append EOF
to your writing. Doing so writes everything remaining in the buffer to the file and reads back the contents of the file.
EOF
demoFile.txt:
Test line of text.
Bacon ipsum dolor amet beef picanha drumstick alcatra brisket, short ribs sirloiBacon ipsum dolor amet beef picanha drumstick alcatra brisket, short ribs sirloin.
One thing to note is that the UART buffer on the Arduino might limit the number of characters entered on a single line. In one test I noticed that my Arduino only accepted 164 bytes before loosing data, but I’ve seen that vary a bit.
language:c
#include <SPI.h>
#include <SD.h>
File fd;
const uint8_t BUFFER_SIZE = 20;
char fileName[] = "demoFile.txt"; // SD library only supports up to 8.3 names
char buff[BUFFER_SIZE+2] = ""; // Added two to allow a 2 char peek for EOF state
uint8_t index = 0;
const uint8_t chipSelect = 8;
const uint8_t cardDetect = 9;
enum states: uint8_t { NORMAL, E, EO };
uint8_t state = NORMAL;
bool alreadyBegan = false; // SD.begin() misbehaves if not first call
void setup()
{
Serial.begin(57600);
while (!Serial); // Wait for serial port to connect (ATmega32U4 type PCBAs)
// Note: To satisfy the AVR SPI gods the SD library takes care of setting
// SS_PIN as an output. We don't need to.
pinMode(cardDetect, INPUT);
initializeCard();
}
void loop()
{
// Make sure the card is still present
if (!digitalRead(cardDetect))
{
initializeCard();
}
if (Serial.available() > 0)
{
readByte();
if (index == BUFFER_SIZE)
{
flushBuffer(); // Write full buffer to µSD card
}
}
}
// Do everything from detecting card through opening the demo file
void initializeCard(void)
{
...
Troubleshooting
Arduino has troubleshooting tips on their Notes on the Arduino SD Card Library page, including instructions for formatting new cards (if needed) and using the library with other Arduino boards.
Resources and Going Further
Now that you know how to add more storage to your Arduino projects, it’s time to get out there and create something amazing. Need some inspiration? Check out these other SparkFun tutorials.
- SD cards are a great way to add more storage to Internet of Things projects. You can add a microSD Shield to a WiFly Shield to serve up larger web pages or hold more data. You can also use the CC3000 Shield, which has a microSD card slot built in.
- Need more power than your Arduino can provide? Check out the Edison, which also has a microSD card Block to add more storage to larger projects.
- Logging data is a common use for SD cards. Take your logging project to the next level with the Logomatic.
learn.sparkfun.com |CC BY-SA 3.0 | SparkFun Electronics | Niwot, Colorado