{"id":1171,"date":"2015-11-25T06:53:26","date_gmt":"2015-11-24T22:53:26","guid":{"rendered":"http:\/\/handsontec.com\/?page_id=1171"},"modified":"2015-12-01T18:05:21","modified_gmt":"2015-12-01T10:05:21","slug":"micro-sd-card-interfacing-tutorial","status":"publish","type":"page","link":"https:\/\/handsontec.com\/index.php\/micro-sd-card-interfacing-tutorial\/","title":{"rendered":"Micro SD Card Interfacing Tutorial"},"content":{"rendered":"<h2>Using the SD library to read and write to a file on a SD card<\/h2>\n<h3 class=\"headline\"><span id=\"intro\">Introduction<\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"color: #800000; font-size: 14pt;\">If you have a project with any audio, video, graphics, data logging, etc in it, you&#8217;ll find that <\/span><span style=\"color: #800000; font-size: 14pt;\">having a removable storage option is essential. Most microcontrollers have extremely <\/span><span style=\"color: #800000; font-size: 14pt;\">limited built-in storage. For example, even the Arduino Mega chip (the Atmega2560) has a <\/span><span style=\"color: #800000; font-size: 14pt;\">mere 4Kbytes of EEPROM storage. There&#8217;s more flash (256K) but you can&#8217;t write to it as<\/span> <span style=\"color: #800000; font-size: 14pt;\">easily and you have to be careful if you want to store information in flash that you don&#8217;t<\/span><br \/>\n<span style=\"color: #800000; font-size: 14pt;\">overwrite the program itself!<\/span><\/p>\n<p style=\"text-align: justify;\">\u00a0<a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/cdcard.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1173 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/cdcard.png\" alt=\"cdcard\" width=\"607\" height=\"467\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/cdcard.png 607w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/cdcard-200x154.png 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/cdcard-300x231.png 300w\" sizes=\"(max-width: 607px) 100vw, 607px\" \/><\/a>If you&#8217;re doing any sort of data logging, graphics or audio, you&#8217;ll need at least a megabyte of storage, and 64M is probably the minimum. To get that kind of storage we&#8217;re going to use the same type that&#8217;s in every digital camera and mp3 player: flash cards! Often called SD or microSD cards, they can pack<strong> gigabytes<\/strong> into a space smaller than a coin. They&#8217;re also available in every electronics shop so you can easily get more and best of all, many computers have SD or microSD card readers built in so you can move data back and forth between say your Arduino GPS data logger and your computer graphing software.<\/p>\n<h3>What to watch for ?<\/h3>\n<p style=\"text-align: justify;\">There are a few things to watch for when interacting with SD cards:<\/p>\n<p style=\"text-align: justify;\">One is that they are strictly 3.3V devices and the power draw when writing to the card can be fairly high, up to 100mA (or more)! That means that you <strong>must <\/strong>have a fairly good 3.3V power supply for the card. Secondly you must also have 3.3V logic to interface to the pins. We&#8217;ve found that SD cards are fairly sensitive about the interface pins &#8211; the newest cards are edge triggered and require very &#8216;square&#8217; transitions &#8211; things like resistor dividers and long wires will have a deleterious effect on the transition speed, so <strong>keep wires short, and avoid using resistor dividers for the 3.3V logic lines<\/strong>. We suggest instead using level shifters, such as <strong>HEF4050<\/strong>, <strong>74LVX245 <\/strong>or <strong>74AHC125<\/strong> chips.<\/p>\n<p style=\"text-align: justify;\">Secondly, there are two ways to interface with SD cards &#8211; <strong>SPI mode<\/strong> and <strong>SDIO<\/strong> <strong>mode<\/strong>. SDIO mode is faster, but is more complex and as far as we can tell, requires signing non-disclosure documents. For that reason, you will likely never encounter SDIO mode interface code. Instead, every SD card has a &#8216;lower speed&#8217; SPI mode that is easy for any microcontroller to use. SPI mode requires four pins (we&#8217;ll discuss them in detail later) so it&#8217;s not pin-heavy like some parallel-interface components<\/p>\n<p style=\"text-align: justify;\">SD cards come in two popular flavors &#8211; <strong>microSD<\/strong> and <strong>SD<\/strong>. The interface, code, structure, etc is all the same. The only differences is the size. MicroSD are much much smaller in physical size.<\/p>\n<p style=\"text-align: justify;\">Third, SD cards are &#8216;raw&#8217; storage. They&#8217;re just sectors in a flash chip, there&#8217;s no structure that you <em>have <\/em>to use. That means you could format an SD card to be a Linux filesystem, a FAT (DOS) filesystem or a Mac filesystem. You could also not have any filesystem at all! However, 99% of computers, cameras, MP3 players, GPS loggers, etc require <strong>FAT16<\/strong> or <strong>FAT32<\/strong> for the filesystem. The tradeoff here is that for smaller microcontrollers (like the Arduino) the addition of the complex file format handling can take a lot of flash storage and RAM.<\/p>\n<div class=\"page-title-wrapper\">\n<h3 class=\"headline\"><span id=\"format\">Formatting notes<\/span><\/h3>\n<div class=\"clear\" style=\"text-align: justify;\">Even though you can\/could use your SD card &#8216;raw&#8217; &#8211; it&#8217;s most convenient to format the card to a filesystem. For the Arduino library we&#8217;ll be discussing, and nearly every other SD library, the card must be formatted FAT16 or FAT32. Some only allow one or the other. The Arduino SD library can use either.<\/div>\n<\/div>\n<div class=\"page-content all-page-view-content\">\n<div class=\"row-fluid build-text\">\n<p style=\"text-align: justify;\">If you bought an SD card, chances are it&#8217;s already pre-formatted with a FAT filesystem. However you may have problems with how the factory formats the card, or if it&#8217;s an old card it needs to be reformatted. The Arduino SD library we use supports both <strong>FAT16<\/strong> and <strong>FAT32<\/strong> filesystems. If you have a very small SD card, say 8-32 Megabytes you might find it is formatted <strong>FAT12<\/strong> which isn&#8217;t supported. You&#8217;ll have to reformat these card. Either way, it&#8217;s <strong>always<\/strong> good idea to format the card before using, even if it&#8217;s new! Note that formatting will erase the card so save anything you want first.<\/p>\n<div class=\"page-title-wrapper\">\n<h3 class=\"headline\"><span id=\"wiring\">Wiring<\/span><\/h3>\n<div class=\"clear\" style=\"text-align: justify;\">Now that your card is ready to use, we can wire up the microSD interface board! There&#8217;s an on-board ultra-low dropout regulator that will convert voltages from 3.3V-6v down to ~3.3V (<strong>IC2<\/strong>). There&#8217;s also a level shifter that will convert the interface logic from 3.3V-5V to 3.3V. That means you can use this board to interact with a 3.3V or 5V microcontrollers.<\/div>\n<div class=\"clear\" style=\"text-align: justify;\"><a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1176 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-2.png\" alt=\"microsd-2\" width=\"892\" height=\"484\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-2.png 892w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-2-200x109.png 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-2-300x163.png 300w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-2-768x417.png 768w\" sizes=\"(max-width: 892px) 100vw, 892px\" \/><\/a><\/div>\n<div class=\"clear\" style=\"text-align: justify;\">\n<p>In this tutorial we will be using an Arduino to demonstrate the wiring and interfacing. If you have another microcontroller, you&#8217;ll need to adapt the wiring and code to match!<\/p>\n<p>Because SD cards require a lot of data transfer, they will give the best performance when connected up to the <strong>hardware<\/strong> SPI pins on a microcontroller. The hardware SPI pins are much faster than &#8216;bit-banging&#8217; the interface code using another set of pins. For &#8216;classic&#8217; Arduinos such as the Duemilanove\/Diecimila\/Uno those pins are <strong>digital 13 (SCK), 12 (MISO) <\/strong>and <strong>11 (MOSI)<\/strong>. You will also need a fourth pin for the &#8216;chip\/slave select&#8217; (<strong>SS<\/strong>) line. Traditionally this is pin <strong>10 <\/strong>but you can actually use any pin you like. If you have a Mega, the pins are different! You&#8217;ll want to use digital <strong>50 (MISO), 51 (MOSI), 52 (SCK), <\/strong>and for the CS line, the most common pin is <strong>53 (SS).<\/strong> Again, you can change the SS (pin <strong>10<\/strong> or <strong>53<\/strong>) later but for now, stick with those pins.<\/p>\n<ul>\n<li>Connect the <strong>5V<\/strong> pin to the <strong>5V<\/strong> pin on the Arduino<\/li>\n<li>Connect the<strong> GND <\/strong>pin to the<strong> GND <\/strong>pin on the Arduino<\/li>\n<li>Connect <strong>SCK<\/strong> to pin <strong>13 <\/strong>or <strong>52<\/strong><\/li>\n<li>Connect <strong>MISO <\/strong>to pin <strong>12 <\/strong>or <strong>50<\/strong><\/li>\n<li>Connect <strong>MOSI<\/strong> to pin <strong>11 <\/strong>or <strong>51<\/strong><\/li>\n<li>Connect <strong>CS <\/strong>to pin <strong>10 <\/strong>or <strong>53<\/strong><\/li>\n<\/ul>\n<p><a href=\"http:\/\/handsontec.com\/index.php\/product\/microsd-card-breakout-board\/\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1178 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-3.png\" alt=\"microsd-3\" width=\"552\" height=\"399\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-3.png 552w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-3-200x145.png 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-3-300x217.png 300w\" sizes=\"(max-width: 552px) 100vw, 552px\" \/><\/a> That&#8217;s it! Refer to connecting picture below using bread board. Now you&#8217;re ready to rock!<\/p>\n<p><a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1179 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-4.png\" alt=\"microsd-4\" width=\"653\" height=\"586\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-4.png 653w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-4-200x179.png 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-4-300x269.png 300w\" sizes=\"(max-width: 653px) 100vw, 653px\" \/><\/a><\/p>\n<h3>Arduino Library &amp; First Test<\/h3>\n<p>Interfacing with an SD card is a bunch of work, but luckily there is very nice Arduino library just for this purpose and it&#8217;s now part of the Arduino IDE known as <strong>SD<\/strong> (pretty good name, right?) You can see it in the Examples submenu.<\/p>\n<p>Next, select the<strong> CardInfo<\/strong> example sketch.<\/p>\n<p><a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1180 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-5.png\" alt=\"microsd-5\" width=\"497\" height=\"600\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-5.png 497w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-5-200x241.png 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-5-249x300.png 249w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><\/a><\/p>\n<div class=\"row-fluid build-text\">\n<p>This sketch will not write any data to the card, just tell you if it managed to recognize it, and some information about it. This can be <strong>very<\/strong> useful when trying to figure out whether an SD card is supported. Before trying out a new card, please try out this sketch!<\/p>\n<p>Go to the beginning of the sketch and make sure that the <strong>chipSelect<\/strong> line is correct, for this wiring we&#8217;re using digital pin 10 so change it to 10!<\/p>\n<p><a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1181 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-6.png\" alt=\"microsd-6\" width=\"499\" height=\"504\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-6.png 499w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-6-200x202.png 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-6-100x100.png 100w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-6-90x90.png 90w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-6-297x300.png 297w\" sizes=\"(max-width: 499px) 100vw, 499px\" \/><\/a><\/p>\n<div class=\"row-fluid build-text\">\n<p>OK, now insert the SD card into the breakout board and upload the sketch.<\/p>\n<p>Open up the Serial Monitor and type in a character into the text box (&amp; hit send) when prompted. You&#8217;ll probably get something like the following:<\/p>\n<p><a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1182 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-7.png\" alt=\"microsd-7\" width=\"511\" height=\"494\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-7.png 511w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-7-200x193.png 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-7-300x290.png 300w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/a><\/p>\n<div class=\"row-fluid build-text\">\n<p>It&#8217;s mostly gibberish, but it&#8217;s useful to see the <strong>Volume type is FAT16<\/strong> part as well as the size of the card (about 2 GB which is what it should be) etc.<\/p>\n<p>If you have a bad card, which seems to happen more with ripoff version of good brands, you might see:<\/p>\n<p><a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1193 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-8.png\" alt=\"microsd-8\" width=\"405\" height=\"550\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-8.png 405w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-8-200x272.png 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-8-221x300.png 221w\" sizes=\"(max-width: 405px) 100vw, 405px\" \/><\/a><\/p>\n<div class=\"row-fluid build-text\">\n<p>The card mostly responded, but the data is all bad. Note that the<strong> Product ID<\/strong> is <strong>&#8220;N\/A&#8221;<\/strong> and there is no <strong>Manufacturer ID<\/strong> or <strong>OEM ID<\/strong>. This card returned some SD errors. It&#8217;s basically a bad scene ! If you get something like this (where there is a response but it&#8217;s corrupted) you can try to reformat it or if it still flakes out, should toss the card.<\/p>\n<p>Finally, try taking out the SD card and running the sketch again, you&#8217;ll get the following:<\/p>\n<p><a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1194 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-9.png\" alt=\"microsd-9\" width=\"510\" height=\"192\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-9.png 510w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-9-200x75.png 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/11\/microsd-9-300x113.png 300w\" sizes=\"(max-width: 510px) 100vw, 510px\" \/><\/a>It couldn&#8217;t even initialize the SD card. This can also happen if there&#8217;s a soldering or wiring error or if the card is <em>really<\/em> damaged.<\/p>\n<h3><span style=\"color: #ff9900;\">Examples<\/span><\/h3>\n<\/div>\n<ul>\n<li><a href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/CardInfo\" target=\"_blank\">Card Info<\/a>: Get info about your SD card.<\/li>\n<li><a href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/Datalogger\" target=\"_blank\">Datalogger<\/a>: Log data from three analog sensors to an SD card.<\/li>\n<li><a href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/DumpFile\">Dump File<\/a>: Read a file from the SD card.<\/li>\n<li><a href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/Files\" target=\"_blank\">Files<\/a>: Create and destroy an SD card file.<\/li>\n<li><a href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/listfiles\" target=\"_blank\">List Files<\/a>: Print out the files in a diretory on a SD card.<\/li>\n<li><a href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/ReadWrite\" target=\"_blank\">Read Write<\/a>: Read and write data to and from an SD card.<\/li>\n<\/ul>\n<h3><span style=\"color: #ff9900;\">Parts Needed in this Tutorial<\/span><\/h3>\n<ul>\n<li><a href=\"http:\/\/handsontec.com\/index.php\/product\/arduino-uno\/\" target=\"_blank\">Arduino Uno <\/a><\/li>\n<li><a href=\"http:\/\/handsontec.com\/index.php\/product\/microsd-card-breakout-board\/\" target=\"_blank\">Micro SD Card Breakout Board<\/a><\/li>\n<\/ul>\n<div class=\"row-fluid build-text\"><\/div>\n<h3 class=\"row-fluid build-text\"><span style=\"color: #ff9900;\">Additional Related Information<\/span><\/h3>\n<ul>\n<li><a href=\"http:\/\/www.handsontec.com\/pdf_files\/SD%20Memory%20Card.pdf\" target=\"_blank\">Access SD Memory Card<\/a><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Using the SD library to read and write to a file on a SD card Introduction If you have a project with any audio, video, graphics, data logging, etc in it, you&#8217;ll find that having a removable storage option is essential. Most microcontrollers have extremely limited built-in storage. For example, even the Arduino Mega chip [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1201,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/pages\/1171"}],"collection":[{"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/comments?post=1171"}],"version-history":[{"count":10,"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/pages\/1171\/revisions"}],"predecessor-version":[{"id":1213,"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/pages\/1171\/revisions\/1213"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/media\/1201"}],"wp:attachment":[{"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/media?parent=1171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}