Using SD Cards with Arduino - Record Servo Motor Movements
SD and microSD cards are an ideal way to store large amounts of non-volatile data for your Arduino projects. Today I will show you how to use SD card modules in your Arduino designs. I’ll also build a servo motor position recorder that can record and playback the motion of a servo motor.
Full article with code - https://dbot.ws/sdcard
Today I will take a look at SD and microSD card modules. These are a simple way of adding storage to your Arduino projects.
SD cards have been around since the turn of the century and are used in a variety of electronic devices. I’m sure you probably have several of them for your phone, tablet, music player or camera. If you use a Raspberry Pi a microSD card is used to hold your operating system and data, it basically takes the place of a hard drive in this case.
It is very simple to use SD cards with the Arduino. They connect via the SPI bus and the Arduino IDE has a built-in library to work with them.
I’ll show you how to connect a microSD card module to an Arduino Uno and how to use some of the sample code that is included with the library. This includes code to read and write files to the card, as well as a Datalogger that can record the position of three potentiometers.
After we master the sample code we will create a small project.
Our project will have a servo motor and potentiometer along with the microSD card module. You will be able to move the servo using the pot and record its movements on the microSD card. After that, I’ll show you how to play those movements back.
You can use this code “as-is” or expand upon it to use more servos, perhaps to control a robot arm. You could also use it to record DC motors, LEDs or pretty well anything else you can think of.
Here are the contents of today's video:
00:00 - Introduction
02:20 - SD and microSD Card Module introduction
05:44 - ReadWrite Example
11:53 - Datalogger Example
18:56 - DumpFile Example
21:46 - Servo Recorder
27:25 - Servo Playback
As always you will find a detailed article to accompany this video on the DroneBot Workshop website. Check it out at https://dbot.ws/sdcard <. The article also has the code for the servo motor recorder and player in a handy ZIP file.
"Never trust a computer you can’t throw out a window." — Steve Wozniak
I have a question about using SC Cards to record servo motor movements. I am working on a sixteen-channel servo controller. What I would like to do is build up the channels one at a time which would allow me to be able to go back and just rerecord one channel without affecting any of the other channels. What would you suggest to be able to do this? Thank You for your help.
Did you mean SD cards ?
Assuming that you only need to store values 0-180 as the angle of the servo, each datum can be contained within a single byte.
So, on your SD card, use the first 2 bytes to save the count of samples stored on the card, where each sample is the full 16 bytes of servo positions.
Therefore, the address on the card of the first entry for servo N is 2+N (i.e. N bytes past the length counter). Each subsequent sample reading for that servo will be at 2+N+16 and so on.
So, for sample number S (for S=0,1,2,...) and servo number N (N=0,1,2,...,15), the address of its value on the card will be 2+16*S+N.
So, to record a value for servo you can use the command
to set the address and position for this samples servo N value and read or write from that position.
This way, you don't even have to implement your servos from 0 to 1 to 2 ... but can implement them in any order without corrupting the other values.
Yes, I meant SD Card. Thank You for your Idea I would like to try this out. I am still not sure to implement it but like to get this working.
If it helps, think of it as a giant array of 16 byte arrays. The 16 bytes represent your servo values but the list extends along the SD card instead of your Arduino's memory.
So, you simulate moving to the index of the sample desired by passing over all of the 16 byte servo value lists before that sample and then go to the appropriate index of your servo in that list of 16 values.
If you always store the same number of samples, then you don't even need the 2 bytes at the start (i.e. the count of samples stored).