{"id":42299,"date":"2017-09-08T12:05:35","date_gmt":"2017-09-08T12:05:35","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=42299"},"modified":"2022-09-20T15:16:34","modified_gmt":"2022-09-20T15:16:34","slug":"guide-to-sd-card-module-with-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/guide-to-sd-card-module-with-arduino\/","title":{"rendered":"Guide to SD Card Module with Arduino"},"content":{"rendered":"\n<p>In this post we\u2019re going to show you how to use an SD card module with Arduino to read and write files on an SD card.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"700\" height=\"281\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Arduino-SD-card-thumbnail.png?resize=700%2C281&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-42315\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Arduino-SD-card-thumbnail.png?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Arduino-SD-card-thumbnail.png?resize=300%2C120&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Arduino-SD-card-thumbnail.png?resize=250%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 250w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure><\/div>\n\n\n\n<p>For an Arduino project with the SD card module read our blog post: <a href=\"https:\/\/randomnerdtutorials.com\/arduino-temperature-data-logger-with-sd-card-module\/\">Arduino temperature data logger with SD card<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing the SD Card module<\/h2>\n\n\n\n<p>The SD card module is specially useful for projects that require data logging.<\/p>\n\n\n\n<p>The Arduino can create a file in an SD card to write and save data using the&nbsp;<strong>SD<\/strong> library.<\/p>\n\n\n\n<p>There are different models from different suppliers, but they all work in a similar way, using the SPI communication protocol. The module used in this tutorial is the one shown in figure below (front and back view).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"700\" height=\"282\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/DSC01098-Copy.jpg?resize=700%2C282&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-42317\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/DSC01098-Copy.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/DSC01098-Copy.jpg?resize=300%2C121&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/DSC01098-Copy.jpg?resize=250%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 250w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure><\/div>\n\n\n\n<p>This module works with micro SD card.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"700\" height=\"336\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/DSC01082-Copy.jpg?resize=700%2C336&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-42318\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/DSC01082-Copy.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/DSC01082-Copy.jpg?resize=300%2C144&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Where to buy?<\/h3>\n\n\n\n<p>The SD card module is a very cheap and you can find one&nbsp;for <a href=\"https:\/\/makeradvisor.com\/tools\/sd-card-module\/\" target=\"_blank\" rel=\"noreferrer noopener\">approximately $1 &#8211; check prices on Maker Advisor<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pin wiring<\/h3>\n\n\n\n<p>The table below shows how you should wire the SD card module to your Arduino<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>SD card module&nbsp;<\/strong><\/td><td><strong>Wiring to Arduino Uno<\/strong><\/td><td><strong>Wiring to Arduino Mega<\/strong><\/td><\/tr><tr><td>VCC<\/td><td>3.3V or 5V (check module\u2019s datasheet)<\/td><td>3.3V or 5V (check module\u2019s datasheet)<\/td><\/tr><tr><td>CS<\/td><td>4<\/td><td>53<\/td><\/tr><tr><td>MOSI<\/td><td>11<\/td><td>51<\/td><\/tr><tr><td>CLK<\/td><td>13<\/td><td>52<\/td><\/tr><tr><td>MISO<\/td><td>12<\/td><td>50<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><td>GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Note<\/strong>: different Arduino boards have different SPI pins. If you\u2019re using another Arduino board, check the Arduino official <a href=\"https:\/\/www.arduino.cc\/en\/Reference\/SPI\" target=\"_blank\" rel=\"noreferrer noopener\">documentation<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing the SD card<\/h2>\n\n\n\n<p>The first step when using the SD card module with Arduino is formatting the SD card as FAT16 or FAT32. Follow the instructions below.<\/p>\n\n\n\n<p><strong>1)<\/strong> To format the SD card, insert it in your computer. Go to <strong>My Computer<\/strong> and right click on the <strong>SD card<\/strong>. Select <strong>Format<\/strong> as shown in figure below.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"431\" height=\"573\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/format-SD-card-1.png?resize=431%2C573&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-42300\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/format-SD-card-1.png?w=431&amp;quality=100&amp;strip=all&amp;ssl=1 431w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/format-SD-card-1.png?resize=226%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 226w\" sizes=\"(max-width: 431px) 100vw, 431px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2)<\/strong> A new window pops up. Select <strong>FAT32<\/strong>,&nbsp;press&nbsp;<strong>Start<\/strong> to initialize the formatting process and follow the onscreen instructions.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"254\" height=\"463\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/format-SD-card-2.png?resize=254%2C463&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-42301\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/format-SD-card-2.png?w=254&amp;quality=100&amp;strip=all&amp;ssl=1 254w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/format-SD-card-2.png?resize=165%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 165w\" sizes=\"(max-width: 254px) 100vw, 254px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the SD card module<\/h2>\n\n\n\n<p>Insert the formatted SD card in the SD card module.<\/p>\n\n\n\n<p>Connect the SD card module to the Arduino as shown in the circuit schematics below or check <strong>Pin Wiring<\/strong> in previous section.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/sd-card-adaptor-fritzing_bb.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"782\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/sd-card-adaptor-fritzing_bb.png?resize=1200%2C782&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-42332\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/sd-card-adaptor-fritzing_bb.png?w=1386&amp;quality=100&amp;strip=all&amp;ssl=1 1386w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/sd-card-adaptor-fritzing_bb.png?resize=300%2C195&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/sd-card-adaptor-fritzing_bb.png?resize=768%2C500&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/sd-card-adaptor-fritzing_bb.png?resize=1024%2C667&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Note:<\/strong> depending on the module you\u2019re using, the pins may be in a different order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code &#8211; CardInfo<\/h3>\n\n\n\n<p>To make sure everything is wired correctly and the SD card is working properly, in the Arduino IDE window go to <strong>File<\/strong>&gt; <strong>Examples<\/strong> &gt; <strong>SD<\/strong> &gt; <strong>CardInfo<\/strong>.<\/p>\n\n\n\n<p>Upload the code to your Arduino board. Make sure you have the right board and COM port selected.<\/p>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 9600 and you should see your SD card information.<\/p>\n\n\n\n<p>If everything is working properly you&#8217;ll see a similar message on the serial monitor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"551\" height=\"349\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/card-info-1.png?resize=551%2C349&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-42303\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/card-info-1.png?w=551&amp;quality=100&amp;strip=all&amp;ssl=1 551w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/card-info-1.png?resize=300%2C190&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Read and write to the SD card<\/h2>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">SD<\/span> library provides useful functions for easily write in and read from the SD card.<\/p>\n\n\n\n<p>To write and read from the SD card, first you need to include the <span class=\"rnthl rntliteral\">SPI<\/span> and <span class=\"rnthl rntliteral\">SD<\/span> libraries:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;SPI.h&gt;\n#include &lt;SD.h&gt;<\/code><\/pre>\n\n\n\n<p>You also have to initialize the SD card module at the Chip Select (CS) pin &#8211; in our case, pin 4.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>SD.begin(4);<\/code><\/pre>\n\n\n\n<p>To open a new file in the SD card, you need to create a <span class=\"rnthl rntliteral\">file<\/span> object that refers to your data file. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dataFile = SD.open(\"data.txt\", FILE_WRITE);<\/code><\/pre>\n\n\n\n<p>The first parameter of this function is the name of the file, <span class=\"rnthl rntliteral\">data.txt<\/span>, and the <span class=\"rnthl rntliteral\">FILE_WRITE<\/span> ;argument enables you to read and write into the file.<\/p>\n\n\n\n<p>This line of code creates a file called <strong>data.txt<\/strong> on your SD card. If the <strong>data.txt<\/strong> file already exists, &nbsp;Arduino will open the file instead of creating another one.<\/p>\n\n\n\n<p>To write data to the currently open file, you use:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dataFile.write(data);<\/code><\/pre>\n\n\n\n<p>In which the <span class=\"rnthl rntliteral\">dataFile<\/span> is the file object created previously and the <span class=\"rnthl rntliteral\">data<\/span> is what you want to write in the file.<\/p>\n\n\n\n<p>You can also use the <span class=\"rnthl rntliteral\">print()<\/span> or <span class=\"rnthl rntliteral\">println()<\/span> functions to print data into the file:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dataFile.print(data);\ndataFile.println(data); \/\/ followed by a new line<\/code><\/pre>\n\n\n\n<p>To read the data saved on your file:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dataFile.read();<\/code><\/pre>\n\n\n\n<p>You can only write within a file at once, so you need to close a file before proceeding to the next one. To close the <strong>data.txt<\/strong>&nbsp;file we&#8217;ve just created:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>SD.close(\"data.txt\");<\/code><\/pre>\n\n\n\n<p>The argument of this function is the file you want to close, in this case&nbsp;<strong>data.txt<\/strong>.<\/p>\n\n\n\n<p>For a complete sketch on how to read and write, in your Arduino IDE go to <strong>File<\/strong>&gt; <strong>Examples<\/strong> &gt; <strong>SD<\/strong> &gt; <strong>ReadWrite.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"487\" height=\"648\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/readwriteexample_arduinoide.png?resize=487%2C648&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-42422\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/readwriteexample_arduinoide.png?w=487&amp;quality=100&amp;strip=all&amp;ssl=1 487w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/readwriteexample_arduinoide.png?resize=225%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 225w\" sizes=\"(max-width: 487px) 100vw, 487px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This was just a quick introduction to the SD card module with the Arduino.<\/p>\n\n\n\n<p>Make sure you check the following blog post for a data logging project example using the SD card module with Arduino:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-temperature-data-logger-with-sd-card-module\/\">Arduino temperature data logger with SD card<\/a>. <\/li><\/ul>\n\n\n\n<p>In that project we save temperature readings on an SD card with time stamps using the DS18B20 temperature sensor and the <a href=\"https:\/\/randomnerdtutorials.com\/guide-for-real-time-clock-rtc-module-with-arduino-ds1307-and-ds3231\/\">RTC module<\/a>.<\/p>\n\n\n\n<p>If you like Arduino projects, make sure you check our latest Arduino course:&nbsp;<strong><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino Step-by-step Projects \u2013 Build 23 Projects<\/a><\/strong><\/p>\n\n\n\n<p>We hope you&#8217;ve found this guide useful.<\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we\u2019re going to show you how to use an SD card module with Arduino to read and write files on an SD card. For an Arduino project &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Guide to SD Card Module with Arduino\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/guide-to-sd-card-module-with-arduino\/#more-42299\" aria-label=\"Read more about Guide to SD Card Module with Arduino\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":42315,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,267,2,302,230,269,264,7,10],"tags":[],"class_list":["post-42299","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino-project","category-arduino","category-0-guide","category-guide","category-guide-project","category-project","category-a-theory-and-blog","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/Arduino-SD-card-thumbnail.png?fit=700%2C281&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/42299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=42299"}],"version-history":[{"count":1,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/42299\/revisions"}],"predecessor-version":[{"id":116702,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/42299\/revisions\/116702"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/42315"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=42299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=42299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=42299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}