Download PDF Parts List View on YouTube Download Code


Secure Digital, or SD, Cards are used in a variety of applications. You likely have several of them in your electronic devices as they are used in phones, tablets, cameras, and music players.

The Raspberry Pi also uses a microSD card for storage, in a sense it performs the function of a hard drive.

Anywhere that you need a large amount of inexpensive, non-volatile memory an SD (or microSD) card is a good choice. And, as you are about to see, these cards are very easy to use in your Arduino projects.

SD and microSD Cards

The SD card was developed as a joint effort between SanDisk, Panasonic, and Toshiba. The first SD cards were released in August 1999. In January 2000 the three companies formed the SD Association to create standards for SD cards.

SD Cards with Arduino

There are actually three sizes of SD cards – standard SD cards, miniSD cards, and microSD cards. The miniSD card was never that popular and hasn’t been produced since 2008 so modern devices make use of either standard SD cards or microSD cards.

There are also a number of designations on SD cards such as “SDXC”, SDUC”, “UHS-I”, “Class 10” etc. This can get a bit confusing when trying to choose an SD card.

The following is an (admittedly lame) attempt to clarify some of this!

File Systems

SD Cards have evolved to use different file systems, different speeds, and different connection methods than the original 1999 design.  These differences are designated into five different storage classes:

  • SD or SDSC – Standard Capacity SD cards. These use the FAT32 file system and can store up to 2 GB of data.  Older cards used the FAT16 file system.
  • SDHC – These are High Capacity cards. They also use the FAT32 file systems and can store from 2 GB to 32 GB of data.
  • SDXC – These are Extended Capacity cards.  They use the exFAT file system and have capacities from 32 GB to 2 TB.
  • SDUC – These are Ultra Capacity cards. They have capacities from 2 TB to a whopping 128 TB.
  • SDIO – This is a standard that combines memory cards with Bluetooth and other wireless networking standards, allowing the cards to be read remotely.


SD cards are serial data cards and thus have limits to the speed that they can transfer data. As SD cards evolved so has their speeds and there are new designations to determine which cards are faster than others.

Older cards used a Class designation from 1 to 10, with a 10 being the fastest. Modern SD cards can all exceed Class 10 speed so the class designation is virtually meaningless.

SDHC and SDXC cards can use an Ultra High-Speed bus and have the following designations:

  • UHS-I – Transfers data at a rate of 50 MBps to 104 MBps.
  • UHS-II – Transfers data at a rate of 156 MBps.
  • UHS-III – Transfers data from 312 MBps to 624 MBps.

There is also a new specification called “SD Express” which can transfer data at up to 985 MBps.

Electrical Connections

A standard SD card uses the SPI bus and works at 3.3 volts.  SDHC and SDXC cards can also switch to the “one-bit SD bus” and in this mode they work on 1.8 volts.

When an SDHC or SDXC card is inserted into its socket it will initially use the SPI bus. The host computer can switch the device to one-bit mode if the device supports it.

SD cards and microSD cards are electrically compatible, however, they do not use the same pinouts. The plastic “SD Adapter” that is usually included with microSD cards is wired to reconfigure the pinout so the microSD card can also be used in an SD card slot.

SD Card Modules for Arduino

There are many SD card modules available for the Arduino.  Some of them are stand-alone, others are shields. Many of the shields also have additional components like real time clocks, Ethernet adapters and temperature sensors integrated along with the SD card holder.

SD and microSD Modules for Arduino

In the experiments I’ll be performing I used a microSD card adapter, however you can substitute a shield of full-sized SD card adapter if you wish.

Standard SD Modules

The full-sized SD card modules I’ve used have SPI bus connections and connections for either a 5-volt or 3.3-volt power supply.

The SD card uses 3.3-volt logic so there is a built-in voltage regulator to reduce the 5-volt supply to 3.3-volts. If you use a 3.3-volt supply the regulator is bypassed.

One thing to note is that many of these modules do not have logic-level converters and therefore expect that 3.3-volt logic will be used. If you are using 5-volt logic, as with an Arduino Uno or Mega, you’ll need to supply logic-level converters or use a resistor array to work with the 3.3-volt logic.

microSD Modules

In my experiments I elected to use the microSD module, the one I’m using is very common.

These modules are made to be used with 5-volt logic as they contain built-in logic converters, as well as voltage regulators. As such they have only a 5-volt power input.

Using SD Cards with Arduino

The Arduino does not have a particularly fast bus so any type of SD card will work well with it.

I’ll be showing you the connections using a microSD module but they are pretty well identical for a full-sized SD module. Remember though that if you elect to use a full-sized module you may need to do some logic-level conversion, otherwise your data will be garbled.

Hooking up the microSD Module

The following diagram shows how to hoo the microSD card module to the Arduino Uno, using the SPI bus.

Arduino SD Card Module Hookup

If you are using a shield with an SD or microSD card you might need to change the Chip Select (CS) pin connection, the one I have connected to pin 4. Some shields use pin 6 or pin 10, check with your shield manufacturer or use a multimeter to determine if this is the case with your shield

If you are using an Arduino Mega the SPI pin connections are different.  The following table shows the relationship between the Uno and the Mega.

SPI Bus Connect Arduino Uno Arduino Mega
MISO 12 50
MOSI 11 51
SCK 13 52
CS 4 (can be another pin) 53 (can be another pin)

Again note that you can use almost any pin for Chip Select (CS), so if you do change it just remember to change the appropriate line in the sketch to reflect the difference.

Arduino SD Library

The Arduino IDE already has a library for working with SD cards. It supports both FAT16 and FAT32 file systems on both standard SD (SDSC) or SDHC cards.  Keep that in mind if you are reformatting your microSD card and don’t use exFAT.

Another thing to remember is it uses the older 8.3 naming convention for file names, so keep your names down to 8-characters or less with a maximum 3-character extension.  A name like “TESTFILE.TXT” is great, one like “MYNICEFILE.FILE” won’t work.

You can also use forward slashes (“/”) to specify directories if you need to.

The Arduino also has the SPI Library for communicating over the SPI bus, you’ll need that to talk to your SD or microSD card module.

Arduino Example Sketches

The Arduino SD Library has a number of example sketches included with it, and they are great for learning how to work with an SD card.

You can access these libraries by opening the File menu and selecting Examples. From the sub-menu scroll down until you get to the SD entry and highlight that. You’ll see six example sketches that you can try.

We will look at a few of them now.


The first example we will look at is the ReadWrite example. As its name would imply it shows you how to read and write data from an SD card.

The sketch starts by including both the SPI and SD libraries.  We then define a File object called myfile.

The whole sketch runs in the setup, so it runs once and then ends.

In the Setup we set the speed for the serial monitor and then write “Initializing SD card…” to it.  We then initialize the SD card and print an error message if it fails.

Note the line with SD.begin(4) in it. The number “4” is the pin that the Chip Select (CS) of the module is connected to. If you’re using a shield which uses a different pin you’ll need to change this line to the proper value.

Assuming that the card is OK we then open a file. Take a good look at this statement as it is key to understanding how to work with the file system.

The opens the file. We specify the file name and then we add the FILE_WRITE parameter to indicate that we want to write to the file.  If we hadn’t specified that the file would be open to read instead.

If the file does not exist it will be created and opened. If it already exists then it will be opened and any text we add wil be appended to it. This is an important point to note.

We write to the serial monitor and then use myFile.println to write to the file itself.

After that we close the file. Again, this is an important point to note when working with files, you need to close them when you are done with them.

After we write to the file we open it again, this time to read from it. Note that this time we only need to specify the file name.

Compile the sketch and send it to your Arduino, make sure that you have a microSD card in your module and that it is formatted with the FAT32 file system.  Then open your serial monitor.

You should see the text “testing 1, 2, 3.” displayed on the screen.

SD Card ReadWrite Demo

Try pressing the reset button on your Arduino and observe the serial monitor. You should see an additional line of text every time you press the button. This is because the sketch is appending the text to the file every time it is run.


A Datalogger is a device that can record data over a period of time. It can be used to observe trends or just to record data.

The SD library has a simple Datalogger example. Unlike a professional datalogger, this one does not use a real time clock to add a timestamp to the data, it simply reads the data and writes it to a file on the SD card.

To use this sketch you will need to add some additional components to the circuit, specifically three potentiometers. These should be linear taper devices, any value from 5k up would work – I used three 10k pots in my setup.

Arduino SD Datalogger Hookup

The wiper of each potentiometer is connected to one of the Arduino analog input pins, A0, A1, and A2. One side of each pot is connected to the 5-volt output, the other side is connected to ground.

This will allow you to vary the voltage sent to the analog pins from zero to 5 volts.  We will measure the voltage and write the value, from 0 to 1023, to the SD card. The results will be saved in a comma-delimited file.

The Datalogger sketch starts off in a similar fashion to the previous example. You’ll note that a constant named chipSelect is assigned to the pin you are using to connect to the CD line on your module. Again, if you are using a shield that connects CS to a different pin you’ll need to adjust that accordingly.

Unlike the last sketch this one operates in the Loop, which makes sense as it will continually read the value of the voltages on the three analog inputs and write them to the file.

The section that reads the sensors is a nice example of efficient programming. Instead of writing a line for each sensor a for loop is used to step through the sensors and to format the text for the comma-delimited text file. You can easily extend this to use more analog ports.

SD Card Datalogger Demo

Load the sketch to your Arduino and open your serial monitor. You will see the values of the three analog inputs displayed, adjust some of the potentiometers and observe how they change.

After running it for a while power down the Arduino and remove the card. Now insert the card into your computer and read the resulting file. You should see it contains the same data you observed on the serial monitor.

Read SD Card file on Computer

As this is a comma-delimited text file you can also open it up using your preferred spreadsheet and read the data in columns.


Reading the file on your computer is useful but not as convenient as using the Arduino to do it. The final example we will take a look at will read the file to the Arduino serial monitor.

The DumpFile sketch dumps the contents of the file on the SD card to the serial monitor. As with the first example it works entirely in the Setup routine so there is no code in the loop.

After opening the file for reading the sketch uses a while statement to read the data line-by-line while it is available. Each line is printed to the serial monitor.

SD Card DumpFile Demo

Run the sketch and open the serial monitor. You should observe the same data you saw in the serial monitor when you initially ran the Datalogger sketch.

Servo Motor Recorder & Playback

So now that we have looked at some of the SD card example sketches let’s write one of our own.

The sketches I’m about to show you will allow you to record the position of a servo motor and then play it back. Essentially I’ve combined parts of the Datalogger and DumpFile sketches with a simple servo driver.

You can use this sketch as-is or, better yet, expand upon it to record and playback movements on a robot arm, a robot car or you can use it to create a sequence that plays back on one or more LEDs.

Although I’m only controlling one servo motor in this example it would be very simple to add more of them.

Arduino Hookup

If you still have the Datalogger circuit hooked up on your breadboard you can leave it as it is, we will only be using the potentiometer connected to pin A0.  Otherwise, you’ll need one potentiometer that is hooked up as follows.

Arduino Servo and SD Card Hookup

You will also need a servo motor, along with a power supply for that motor. Although you could hook the servo up to the Arduino 5-volt output I really don’t recommend it, as sharing a servo with the Arduino power supply isn’t a very good idea – it can induce noise and voltage drops onto the supply lines.

I used my bench power supply for the external 5-volt supply, you could also use a 6-volt battery if you wish.

I used a common SG90 servo motor for this test but you can use pretty well any servo you happen to have on hand.

Servo Recorder

I’ve created two sketches, one that records the servo data onto the SD card and a second one that plays it back. You could expand upon them and include them both in the same sketch, perhaps with some record and playback pushbuttons.

The sketch starts by including libraries for the SPI bus, the SD card module and for the servo motor. All three libraries are part of your Arduino IDE so you don’t need to install anything.

Once again if your SD card is on a shield you might need to change the chipSelect constant value to match.

I also define integers to hold the analog input pin number and to hold the value read from that pin.

Finally, an object called myservo is created to represent the servo motor.

The Setup contains code we have seen before with the addition of a line to set up the servo motor and attach it to pin 9. You can use a different pin if you want to, just make sure that it is a pin that supports PWM.

The Loop works in a similar fashion to the Datalogger, except in this case only one analog input is read.

Pay attention to the line that reads the analog input value. It uses a Map function to change the range of the input from 0 to 1023 to a range of 0 to 180, which is what the servo requires.

We then send this value to the servo motor to position it. A small time delay is added to allow the servo to move into place and settle.

Lastly we write the same value to the file on the SD card.  It is also pointed to the serial monitor so you can observe it in action.

Then we repeat the Loop again.

SD Servo Motor Position Recorder

Run the sketch and put your servo through its paces.  Then stop it and move on to the next sketch, the one that will play back the motions you just recorded.

Servo Playback

The servo playback sketch will read the file we created and playback the servo motor movements.

The sketch starts identically to the previous one with the addition of a string variable called buffer. This variable will hold one line of text from the file.

All of the action for this sketch is contained in the Setup routine, so the sketch will run once and move the motor and then it will stop. If you wish you can move some of the sketch into the loop so that the servo repeats the recorded motions indefinitely.

The file is opened for reading and then it is read, one line at a time. We accomplish this in the line buffer = dataFile.readStringUntil(‘\n’) .   This will read the file until it encounters a NewLine character, which is inserted into the file after each line.

We then print the line value to the serial monitor.

After that we use the value to set the servo motor position. Note that since the line is a string we need to convert it to an integer so that the servo object can use it. The toInt() parameter accomplishes this for us.

Once again we delay the servo after each reading to give it a chance to move into position.

We then close the file.

Run this and observe the servo motor motion, it should be identical to the motion you initially recorded.


As you can see it is very simple to incorporate SD cards and microSD cards into your Arduino designs. By using them you can add a huge amount of non-volatile storage to your projects.

Hopefully this has inspired you to start using these versatile devices yourself and make some beautiful memories of your own!


Parts List

Here are some components that you might need to complete the experiments in this article. Please note that some of these links may be affiliate links, and the DroneBot Workshop may receive a commission on your purchases. This does not increase the cost to you and is a method of supporting this ad-free website.




Code from this article – The record and playback code, inconvenient ZIP file.

SD Association – The SD Association, the people who set the standards for SD cards and all of their derivative form factors.


SD Card Experiments with Arduino
SD Card Experiments with Arduino
Article Name
SD Card Experiments with Arduino
SD and microSD cards are a simple way to add huge amounts of non-volatile storage to your Arduino designs. In this article, I will show you how to use SD card modules with the Arduino. I will also show you how to record and playback the motion of a servo motor.
Publisher Name
DroneBot Workshop
Publisher Logo
Tagged on:     
Notify of

Inline Feedbacks
View all comments

Once again another good workshop, thanks Bill.

Ken Thorne
5 years ago

A great workshop i have built the record/play back SD project and it works very well indeed can you please tell me how to add a second pot and servo.
Once again many thanks for time and effort for tech info. Ken

Matt Dobbs
5 years ago

Thank you for all that you are doing. I am new to arduino’s programing language and you have been a lot of help.
I have a 7 dof arm that I have been able to use pots to control and have saved them to the sd card in this form:
536,533,0,270,477,434,452 this is per line. (base,shoulder,elbow,…….).

I really need help on reading the file so I can play the arm back.
Thank You !!

3 years ago
Reply to  Matt Dobbs

hi Matt, i tot of the same idea with the 6 dof robot arm. Good work you got there. So hows your progress so far? must have been successful already. do share some insights.

5 years ago

Hi Bill, I just finished building the SD card servo controller. I did a slight twist. I created a text file on my computer using Notepad. All I did was type in a list of angles I wanted the servo motor to go through. On your program I increased the 15ms to 1500ms so that it would just go to each angle every 1.5 seconds. It works just as I had hoped. I’m going to add a bit more programming with two pieces of data on each line of the text file. The first piece of data will be the… Read more »

Patrick Fields
5 years ago

Hi Bill,

Great video as always! But I am struggling with replicating the result of the servo recording. I am able to get the SD card to capture the file with all the different positions of the potentiometer, but when I run the playback sketch, it runs through the code so quickly that the servo just jumps around on the desk. Any insight?

Patrick Fields
5 years ago
Reply to  Patrick Fields

Disregard. I guess it helps when you wire it correctly. 🙂

Akshay Jose
4 years ago

Hi Bill,
I built this project but I don’t know why servo is not moving. Everything else working fine. Such as recording pot values to SD card & playback. I can see it in serial monitor but servo is not responding. Please help me. It is for my college project.

Dave Wreski
4 years ago

Bill, Best to you and we ALL hope you have a good and healthy recovery. We are so sorry that this happened to you, But when we get to our state in life, things do happen. I am so sorry it did happen to you. We do not know what we would do without you. Your preciperation in this series training videos shows me that you really do care for us. Words cannot explain how we feel about you. Please keep a good outlook on this bump in the road. God bless you. Best regards and hoping you will have… Read more »

Roman Brown
4 years ago

Hey Bill, I am having a difficult time in setting up my SD card module. The wiring is correct Arduino IDE shows that it registers the card but does not register it when I try writing a code to it! Please Help!!!

4 years ago

i am new to this field can anyone tell how to combine all the codes in this project

Lawrence Lemieux
4 years ago

Hi All. I’m new to DroneBot. Love the content. I’m coming to you from Northern Canada.
I just worked through the using SD cards with Arduino tutorial. Great job.
I have one question. When I’m finished recording my data, how do I stop recording?

3 years ago

You have done a good job presenting this tutorial in the most awesome way. You are a good teacher. i have learned a lot. Thank you very much.

3 years ago

Hi, Great work done. I am having trouble in reading and writing a database (.db) file on SD Card using Arduino.
How it can be done?

3 years ago

Very usefull tutorial

3 years ago

Really appreciate your tutorials, it’s clear how much work you put into these and they are very informative. Especially enjoyed the analogue pin output to a delimited string , fantastic. just wanted to say thank you for your effort and kindness.

3 years ago

Can you do the same but with a push button please, this will help a a lot . thank you
I really like your videos and they help me all the time. I wish you can do that for us.

3 years ago

How to add push buttom record and play sir?? Mybe add delet button sir??

3 years ago

Thank you for that tutorial for spi standard. I could do the same for PIC32?

3 years ago

There seems to be a common problem working with the SD library. When compiling my sketch I get a low memory alarm. I can use the example sketches without any issues but combining other header files uses up the remaining amount of memory. I’m trying to use a GPS module in my sketch. Has anyone found a work around for this? I’m using an Arduino Uno.

Eric - France
3 years ago

Very nice job. Many Thanks. I learned a lot with your vidéo and you gave me the wish to learn more.

john d glover
3 years ago

Does anyone know if it’s possible to read/write SD cards using Windows 10, then read/write the data using an Arduino, and vice versa?

Rick Grimes
3 years ago

Hello Sir, would it be possible to use a mp3 player module with a micro sd in this Arduino playback project? my module has 4 pins (“vcc, ground, Rx, Tx).

Thanks Rick!

John Paul Pineda
3 years ago

Awesome Tutorial! Thank You So Much! Sir Bill – This is a great help for me.

3 years ago

good article
it can help me, than you

Vikash Thakur
3 years ago

Hi sir I am a bigginer at coding and I am facing a problem on modifying you code that you uploaded on the SD card module to read and write with Arduino and servo motor actually I am trying to connect 5 servo motor at a time but when I edit the code it’s not compiling so can you make that Arduino code for me for record and for play please send me that code to my gmail account please please please 🙏🙏🙏🙏

Vikash Thakur
3 years ago

Hi sir this is my third comment so please reply I am trying to use 5 servo motor with 5 potentiometer but Arduino code that I edited from your code that you putted in description is not working can you please send me the code for using 5 servo with SD card module 🙏 please help me please please❤❤

2 years ago

Awesomely Presented…! Thank you for making and sharing your Projects…!

Edwin Edgar
2 years ago

Always excellent presentations. With such detail and clarity.
You’re the best!  Thank you.
How can I donate to work?

2 years ago

I like your vidios and tutorials but do not like having your request for comments block the screen so much. would not not be unhappy with a request for comment at the end .

arnov sharma
2 years ago

Hi, how can we use the SD Card reader module to directly open the SD card with the Arduino plugged in? I mean like a Arduino SD Card reader

Joao from Porto (Portugal)
2 years ago

I’ve joined only recently the workshops but I’m loving it! Quite pedagogic and accurate. Beautiful, thank you Bill!

Fred Orbeta
2 years ago

DroneBot Workshop, I would like to thank you for your kind and precious contributions to the education of people like me. Please don’t stop with your lessons.

Best regards from NY,

1 year ago

Thanks a lot for valuable information.
I have question.
I use port 10, 11 for the GSM card. Can I use other ports then 11 (MOSI) for the SD card reader?


1 year ago

Hello, in the video you said recording position and playing it back can be done using DC motors with encoders. what would be the process to accomplish this? Im assuming it would involve recording the motors position, then accessing that data and passing it through something such as a PID controller? How would something like this be accomplished?

And a secondary question is how do you set your sampling rate for how often the serial monitor data is saved in the program?

Thank you!

Lawrence Tetzner
1 year ago

Always have amazing tutorials on these technologies. I’d say a must read for those learning this stuff. I’d also like to see how to get data from the sd card, like “Storage total, Storage available” etc. So can flag the user when the data files are about to fill up an SD card. Perhaps delete earlier records once the size is 80% of the capability. Anyway thank you so much. As an electrical engineer, I enjoy your videos just for the fun of watching. But also find them useful when working on something i have not done before. Thank you… Read more »

1 year ago

Good morning Bill, I have a problem with the code (Servo Record) when I have uploaded it and open my serial port it keeps showing up error opening servopos.txt , but the file SERVOPOS.txt has been created on the micro SD Card !!!

1 year ago
Reply to  Miltos

Try a small delay or press the reset button, The SD card may take a few millisec to to wake up.

1 year ago

Thank you very much Mr. Bill, the problem was the micro SD Card configuration , all your programs working !!!

4 months ago

great job! have you any suggestion how to connect multiple spi devices to uno?