{"id":74,"date":"2010-11-27T06:56:27","date_gmt":"2010-11-27T06:56:27","guid":{"rendered":"http:\/\/morganredfield.com\/blog\/?p=74"},"modified":"2022-11-21T03:25:06","modified_gmt":"2022-11-21T03:25:06","slug":"reading-sd-cards-with-the-arduino","status":"publish","type":"post","link":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/","title":{"rendered":"Reading SD cards with the Arduino"},"content":{"rendered":"<p>About a year ago, I got my friend Ryan an Arduino for his birthday. We futzed around with it for a while trying to come up with a good project, and in the end we decided to try to read an SD card.<\/p>\n<p>We didn&#8217;t really have a reason for it, it just seemed like an achievable goal for a first Arduino project. I seem to recall that we wanted to understand SPI, and Ryan wanted to implement SPI in software. After about a month of weekly meetings, we were still trying to figure out the SD card SPI standard. We&#8217;d made some progress with the initialization routine for the card, but the CRC functions had us completely stumped.<\/p>\n<p>In the end, my friend moved to NYC and I completely forgot about the project.<\/p>\n<p>Fast forward to a month ago and I&#8217;m looking into ways to get my Arduino to play some sound files for a robotics project. When I stumbled on the Adafruit <a href=\"http:\/\/www.ladyada.net\/make\/waveshield\/\">Waveshield<\/a>, I also found an answer to my SD card questions. The Waveshield comes with a library to read SD cards. It didn&#8217;t take me long to get it working with the hardware I had on hand.<\/p>\n<p>I wrote up a quick script to read and display the contents of a file, then hooked up my SD card. I didn&#8217;t have an SD slot handy, so I soldered some headers together into a nice holder. It may be hard to tell from the picture, but I used three sets of headers. One on the bottom sandwiched by two on the top. The top two also clamp onto the SD card.<\/p>\n<p><a href=\"https:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"SDheaders\" src=\"https:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg\" alt=\"Three headers can sandwhich an SD card\" width=\"300\" height=\"200\"><\/a><\/p>\n<p>This allowed me to connect the SD card as shown in this <a href=\"http:\/\/blog.erikdebruijn.nl\/archives\/80-DIY-SD-Daughterboard-for-Sanguino-and-Arduinos..html\">schematic<\/a>. The resistors are there to act as voltage dividers. The SD card needs 3.3 V signaling, but the Arduino provides 5 V signaling. In the Adafruit Waveshield, they use buffers to do the level conversion. Voltage dividers are quick and dirty, but probably not the best solution.<\/p>\n<p><a href=\"http:\/\/blog.erikdebruijn.nl\/archives\/80-DIY-SD-Daughterboard-for-Sanguino-and-Arduinos..html\"><img loading=\"lazy\" decoding=\"async\" title=\"schematicuo7\" src=\"https:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/schematicuo7-300x192.jpg\" alt=\"\" width=\"300\" height=\"192\"><\/a><\/p>\n<p>The sketch I wrote required me to add the Adafruit <a href=\"http:\/\/code.google.com\/p\/wavehc\/\">library<\/a> to my library folder, as shown <a href=\"http:\/\/www.ladyada.net\/library\/arduino\/libraries.html\">here<\/a>.<\/p>\n<p>Once I had everything hooked up, it worked great. The only problem I ran into while putting this together was the formatting of the SD card. The SD library expects FAT16 formatting, which means that the naming conventions for files are pretty strict. Filenames have to be at most eight characters before the &#8220;.&#8221;, and file extensions can only be three characters.<\/p>\n<p><a href=\"https:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/full_setup2.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"full_setup2\" src=\"https:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/full_setup2-300x200.jpg\" alt=\"Connected SD card\" width=\"300\" height=\"200\"><\/a><\/p>\n<p><a href=\"https:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/proof.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"output\" src=\"https:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/proof-292x300.jpg\" alt=\"Output text from Arduino SD reader\" width=\"292\" height=\"300\"><\/a><\/p>\n<p>I think I might put together a little R-2R DAC and make myself a quick and dirty music player.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Code for this is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;FatReader.h>\n\nSdReader card;\nFatVolume vol;\nFatReader root;\nFatReader file;\n\nvoid setup() {\n  Serial.begin(9600);\n  if (!card.init()) {\n   Serial.println(\"can't initialize card\"); \n  }\n  if (!vol.init(card)) {\n    Serial.println(\"can't initialize volume\"); \n  }\n  if (!root.openRoot(vol)) {\n    Serial.println(\"can't open directory\"); \n  }\n  \n  Serial.println(\"Files on SD card:\");\n  root.ls();\n}\n\nchar filename&#91;] = \"ITALO.TXT\";\n\nvoid loop() {\n  if (!file.open(root, filename))\n  {\n     Serial.print(\"failed to open file\");\n     Serial.println(filename); \n  }\n  char letter;\n  \n  Serial.print(\"Now reading file: \");\n  Serial.println(filename);\n  while (file.read(&amp;letter, 1) == 1) {\n   Serial.print(letter);\n  }\n  \n  while (1) {}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>About a year ago, I got my friend Ryan an Arduino for his birthday. We futzed around with it for a while trying to come up with a good project, and in the end we decided to try to read an SD card. We didn&#8217;t really have a reason for it, it just seemed like &hellip; <a href=\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Reading SD cards with the Arduino&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5,13],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-arduino","tag-sd-card"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Reading SD cards with the Arduino - Theory and Practice<\/title>\n<meta name=\"description\" content=\"Quick and dirty SD-card usage for Arduino\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reading SD cards with the Arduino - Theory and Practice\" \/>\n<meta property=\"og:description\" content=\"Quick and dirty SD-card usage for Arduino\" \/>\n<meta property=\"og:url\" content=\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/\" \/>\n<meta property=\"og:site_name\" content=\"Theory and Practice\" \/>\n<meta property=\"article:published_time\" content=\"2010-11-27T06:56:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T03:25:06+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg\" \/>\n<meta name=\"author\" content=\"morgan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"morgan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/\"},\"author\":{\"name\":\"morgan\",\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd\"},\"headline\":\"Reading SD cards with the Arduino\",\"datePublished\":\"2010-11-27T06:56:27+00:00\",\"dateModified\":\"2022-11-21T03:25:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/\"},\"wordCount\":448,\"publisher\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd\"},\"image\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg\",\"keywords\":[\"Arduino\",\"SD card\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/\",\"url\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/\",\"name\":\"Reading SD cards with the Arduino - Theory and Practice\",\"isPartOf\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg\",\"datePublished\":\"2010-11-27T06:56:27+00:00\",\"dateModified\":\"2022-11-21T03:25:06+00:00\",\"description\":\"Quick and dirty SD-card usage for Arduino\",\"breadcrumb\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#primaryimage\",\"url\":\"http:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg\",\"contentUrl\":\"http:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/morganredfield.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Reading SD cards with the Arduino\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/morganredfield.com\/blog\/#website\",\"url\":\"https:\/\/morganredfield.com\/blog\/\",\"name\":\"Theory and Practice\",\"description\":\"Exploring life through science and art.\",\"publisher\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/morganredfield.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd\",\"name\":\"morgan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0da7fa0a0a07a8497d51de4300111d978b649e254fc1afa26dc67f3b83d24c38?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0da7fa0a0a07a8497d51de4300111d978b649e254fc1afa26dc67f3b83d24c38?s=96&d=mm&r=g\",\"caption\":\"morgan\"},\"logo\":{\"@id\":\"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/morganredfield.com\/blog\/author\/morgan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Reading SD cards with the Arduino - Theory and Practice","description":"Quick and dirty SD-card usage for Arduino","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/","og_locale":"en_US","og_type":"article","og_title":"Reading SD cards with the Arduino - Theory and Practice","og_description":"Quick and dirty SD-card usage for Arduino","og_url":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/","og_site_name":"Theory and Practice","article_published_time":"2010-11-27T06:56:27+00:00","article_modified_time":"2022-11-21T03:25:06+00:00","og_image":[{"url":"http:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg","type":"","width":"","height":""}],"author":"morgan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"morgan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#article","isPartOf":{"@id":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/"},"author":{"name":"morgan","@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd"},"headline":"Reading SD cards with the Arduino","datePublished":"2010-11-27T06:56:27+00:00","dateModified":"2022-11-21T03:25:06+00:00","mainEntityOfPage":{"@id":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/"},"wordCount":448,"publisher":{"@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd"},"image":{"@id":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#primaryimage"},"thumbnailUrl":"http:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg","keywords":["Arduino","SD card"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/","url":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/","name":"Reading SD cards with the Arduino - Theory and Practice","isPartOf":{"@id":"https:\/\/morganredfield.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#primaryimage"},"image":{"@id":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#primaryimage"},"thumbnailUrl":"http:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg","datePublished":"2010-11-27T06:56:27+00:00","dateModified":"2022-11-21T03:25:06+00:00","description":"Quick and dirty SD-card usage for Arduino","breadcrumb":{"@id":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#primaryimage","url":"http:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg","contentUrl":"http:\/\/morganredfield.com\/blog\/wp-content\/uploads\/2010\/11\/SDheaders-300x200.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/morganredfield.com\/blog\/2010\/11\/reading-sd-cards-with-the-arduino\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/morganredfield.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Reading SD cards with the Arduino"}]},{"@type":"WebSite","@id":"https:\/\/morganredfield.com\/blog\/#website","url":"https:\/\/morganredfield.com\/blog\/","name":"Theory and Practice","description":"Exploring life through science and art.","publisher":{"@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/morganredfield.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/15fce334735dd60ce7bb5712c44a62dd","name":"morgan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0da7fa0a0a07a8497d51de4300111d978b649e254fc1afa26dc67f3b83d24c38?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0da7fa0a0a07a8497d51de4300111d978b649e254fc1afa26dc67f3b83d24c38?s=96&d=mm&r=g","caption":"morgan"},"logo":{"@id":"https:\/\/morganredfield.com\/blog\/#\/schema\/person\/image\/"},"url":"https:\/\/morganredfield.com\/blog\/author\/morgan\/"}]}},"_links":{"self":[{"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":10,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":668,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/posts\/74\/revisions\/668"}],"wp:attachment":[{"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/morganredfield.com\/blog\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}