Using SD Cards with Arduino - Record Servo Motor Movements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop will be working with SD memory cards in the Arduino we'll see how SD cards work and how to use them in our Arduino sketches we'll also use an SD card to record and playback the motion of a servo motor we're making memories today so welcome to the workshop [Music] hello and welcome to the workshop today we're going to be working with secured digital or SD cards now I'm sure you're very familiar with SD cards you probably use a number of them in your different electronic devices they're used in phones they're used in tablets they're used in camera as the cameras I'm recording with right now use SD cards and if you use a Raspberry Pi an SD card does the functionality of what a hard drive would do any time you need to store large amounts of data inexpensively an SD card is very good for the application now the Arduino by itself does not have a slot for SD cards but there are several modules that you can get very inexpensively to add sd functionality to the Arduino you can also get fields that have SD cards either by themselves or with other components like real time clocks or Ethernet cards so what we're going to do today is we're going to hook an SD card model actually a micro SD card module up to an Arduino they connect very easily through the SPI bus and then we're going to run a few of the built-in examples that are included with the SD library that is already in your arduino ide and after that we're going to perform a little experiment where we take a servo motor and a potentiometer and we will take an SD card and record the movements of the servo motor and then play them back and you can use that as the basis for a project of your own either with a servo motor or you can control DC motors maybe you want to make a little robot car that you can program to go around the room and then play that back you could use this as the basis for that as well so let's get started and learn a little bit about SD cards secure digital or SD cards are non-volatile memory cards the SD card was originally developed as a joint effort by SanDisk Panasonic and Toshiba SD cards were first introduced in August of 1999 the micro SD or TF card was introduced in 2005 SD cards use serial data and communicate using the serial peripheral interface or SPI bus SDHC cards used a fat32 filesystem and have a maximum capacity of 32 gigabytes SDXC and SD UC cards use the exFAT filesystem SDXC cards have a maximum capacity of 2 terabytes and SD UC cards have a theoretical capacity of 128 terabytes although currently no cards of this capacity are available commercially there were modules available for both the SD and micro SD cards all SD cards use 3.3 volt logic and some of the modules have logic level converters built into them the modules communicate with the Arduino using the SPI bus the Arduino IDE has a built-in SD library so it's not necessary to install any external libraries to use SD cards in your Arduino designs so here are a couple of SD card modules for you to look at this is a full-size SD card module and this is a micro SD card module and otherwise they're essentially the same they've got the SPI bus on them there is however one little difference they both have voltage regulators to drop the supply voltage to 3.3 volts because that's what an SD card uses ana micro SD card this one has another chip on it that does a full logic level conversion so this one is made to use with 5-volt logic and as that it's a little bit easier to use this with an Arduino Uno this one does not actually do any logic conversion so if you want to interface this with a 5 volt device you're going to need to provide your own logic level conversion or perhaps a resistive array to king your 5-volt logic levels down to 3.3 volt logic levels this one will accept either a 5 volt or a 3.3 volt supply or as this one will only take the 5 volt supply and it uses its onboard regulator to drop it neither of the modules really have any components on the back all of the action is on the front and there actually isn't very much to either module because really the SD card already has an SPI interface on it so all it's really doing is providing a connector and some pins and in this case power supply voltages for the card to interface it with the SPI bus on the Arduino so now that we've learned a little bit about SD cards and taken a look at the modules it's time to get to work with them now I'm going to hook the my micro SD card module up to my Arduino it's a very simple hookup because it just uses the SPI bus and after that we're going to start using some of the built-in examples that come with your Arduino IDE the first one being one that simply writes to the card and reads the data back so let's take a look at how we're going to hook that up now here's how we will connect the micro SD card module to an Arduino now I'm using an art you know here if you decide to use a different model of Arduino please check the article on the drone bot workshop comm website for details regarding the different pin outs you'll need to use we also of course we'll need the microSD card module itself we'll begin by connecting the ground from the Arduino to the ground on the microSD card module we'll connect the 5 volt output of the Arduino to the VCC connection on the module pin 12 of the Arduino will be connected to the MIS o pin on the module that's master in slave out pin 11 on the Arduino will be connected to the MOS I pin the master out slave in pin 13 is the clock pin the SC K pin on the module and finally pin 4 of the Arduino is connected to the CS or chip select line on the SD card module note that if you're using a shield with an SD card module you may find chip select connected to a different pin such as 10 or 6 you will have to modify the sketches accordingly if this is the case we are now ready to experiment with the SD card module now all of the example sketches we're going to be using with the SD card module are already included in your Arduino IDE go into the file menu and then go to examples and scroll down until you see SD you'll notice a number of different examples here we're going to start with the readwrite example and as the name might imply it writes a file to the SD card and then reads the data back now the sketch starts by including the SPI library the built-in library for communicating on the SPI bus which is how our card is attached and it also includes the built-in SD library then we define a file object which we are going to call my file this is the key to working with files on the Arduino now everything in this sketch happens in this setup we start the setup by opening up the serial monitor at 9600 baud and then we print to it initializing SD card we check to see if the initialization actually happened so if SD begin does not happen then we'll print initialization failed incidentally if you have an SD module such as a shield that does not use pin 4 for chip select you'll need to change the number over here accordingly if it does initialize we print initialization done then we will go and open a file so we define my file as being an SD open and we're going to open the file called test dot txt now you can change the name of this if you wish but otherwise this is the name of the file we will write to the SD card and this is being opened for a write operation then as long as the file object is ok we're going to print to the serial monitor that we're writing to it we are going to actually print to the SD card and this is how we do it over here and then we will close the file you always need to close a file when you're finished with it and then on the serial monitor we simply print that we're done the file didn't open we're just going to print an error message to the serial monitor after that we're going to open the file again but this time for reading and notice that you only need to pass the filename as a parameter when you're opening a file for reading operations once again we'll see if we have a valid file we'll print the name of the file up in the serial monitor and then this operation here this while operation reads everything that's in the file so as long as we see something in the file we'll print it out to the screen so we'll write through the serial be my file read and then again we close the file once again it's always important to close a file and remember you can only open one file at a time on the arduino if it didn't print we'll dis print an error message and that's basically the sketch there is nothing that happens in the loop so let's run the sketch and see what happens all right here is our read write demo I've just opened up the serial monitor and basically the whole sketch runs in the setup so it runs and stops and writes out a line and reads it back now you'll notice I've got several lines read back and that's because I've reset my Arduino several times and every time I do it it will write another line let me reset it again right now and there we go and if you're to count lines you will notice there's one extra line of testing 1 2 3 it appends another line to the file every time that it writes to it the first time it wrote to it it opened the file and created the first line and every subsequent operation it just appended to that so every time you reset it you will get one more line but it's a very simple demo of how to read and write from an SD card using the SD library in the arduino now the next example we're going to look at is something called a data logger a data logger is a device that can as the name implies log data over a period of time it can be used to record things like temperature humidity quite often data loggers use real time clocks to give you time stamp but this simple one we're doing today does not it's one of the examples again that comes with the Arduino IDE and the SD library now our data logger is just going to use three potentiometers connected to the analog inputs of the Arduino we're going to be able to vary them and vary the voltage going to those inputs and we'll take the readings from those three inputs and write it out to a comma delimited file that's going to be stored on an SD card so let me show you how we're going to hook that up and then we'll run the data logger experiment to run the data logger experiment we will start with the hookup with the SD card that we already have you will need to add three linear Potts I used 10k but any value from 5k upwards would suffice connect one side of the pots to the ground on the Arduino the other side of the pots are connected to the 5 volt line the wiper on control a is connected to our Dino analog pin a zero the wiper on pot B is connected to Arduino analog pin a1 and the wiper on pot C is connected to the analog pin a2 so let's go over the data logger sketch which is also found in the SD card examples in your Arduino IDE now once again we start off by including both the SPI library and the SD library so that we can communicate with the SD card module this variable over here indicates the line that the chip select is connected to and right now it's connected to pin 4 if you are using a field where the chip select is connected to another pin such as 10 or 6 you'll need to change this now in the setup we just open up the serial monitor again and we print that we're initializing the SD card and we go and we make certain that the SD card can be initialized similar to what we did in the first sketch the action in this sketch is actually in the loop now what we're going to do is we're going to create a string and populate it with the values that we get by reading the three analog ports and we're going to call our string data string and we start off with a blank string then we'll read the three sensors it's interesting how they do this they use a for loop which is actually a very clever way of doing it so basically analog pin zero to an analog pin of less than 3 in other words 0 1 & 2 and we will increment the loop by 1 all the time we'll take a sensor value which is the analog read from the current analog pin 0 1 or 2 and then we'll write that to the data string the value of the sensor now if the pin is 0 or 1 in other words if it's less than 2 we'll also put a comma after that and that way we can create a comma delimited string so this is a very nice way of making a column of delimited string with very few lines of code after this we're going to need to write this to our SD card so we define a file object which we're calling data file and we do an SD open and again we're doing it for a write so we have to specify both the name of the file which we're going to call data log txt and the fact that we're going to be doing a write to it then we check to see if the file is available in other words it had opened correctly and if it is available we will print the data string to it and then we will close the file and we'll also print the data string to the serial monitor if the file isn't open we'll put an error on the serial monitors say that we cannot open our data log txt file and this is the loop we will discontinue to repeat it over and over again now one interesting thing to note is that when we first open the file this file will not exist and so it will be created on all subsequent operations the file will already exist and it will just append to the file and that's one of the key things about writing to a file on SD card so now that we've seen that let's put it into action move the pots around and watch the different values be recorded on the SD card now here's the data logger sketch running and I've got all of the pots right now at one end and so we're getting values of zeros or the occasional one as you can see on the serial monitor and let me bring one of them up and as you can see the value is increasing as I bring it all the way up it'll go to the maximum value which is 10 23 and I can do that to the next one and then to the next one and so as I move the pots you can see on the serial monitor the value is reflected and of course that value is also being written to a file on our SD card which is over here and so now that we've written to the SD card let's take a look at the file that we've actually created so a very simple method of making sure that you've actually written to the SD card is to simply plug it into a computer and I've done that with my workbench computer and I've opened up the file now the file is called data log txt which is the one that we've recorded and we've recorded quite a bit of data as you can see over here notice that the file date is not correct it comes up as December 31st 1999 but one other thing to notice is that the file name is in uppercase and whenever you work with the SD library in the arduino it reads and writes in uppercase so it doesn't matter what case you use in your code everything will be converted to uppercase but as you can see I've opened up the file in a text editor and you can see the contents of the file scroll down there you go so that represents the position of the three potentiometers now as this is a comma delimited file I could have also opened it up in a spreadsheet you could use Excel or if you're using Libre Office you could use calc or if you're using Google Drive you could bring it up into sheets and open it that way as well and so there you go we've written a file to our SD card using an Arduino now the final built-in experiment that we are going to look at today is something called dump file what dump file does is it takes the contents of a file and dumps it out to the serial monitor so that you can read it now this is going to work with the file that we used with our data logger so we can take a look at the data logger file using the Arduino instead of using our computer to read the file so now let's go and take a look at the dump file sketch so here's the dump file sketch that we are going to use to read the contents of the file and print it out to the serial monitor it's actually quite simple now once again we'll start off by including both the SPI and SD libraries and we set our tip select a pin for again you will need to change that if yours is different and this is another sketch that runs entirely in the setup and doesn't do anything in the loop so in this setup we start our serial monitor at 9600 baud we print initializing SD card out to the monitor we see if the card is actually present so we go and see if the card is there if it's not we print out that the cards failed or not present if it is there we print out card initialized and then we'll go and open the file and we've seen this before as well we set up a file object which we're calling data file and we do an SD open and this is the name of the file that were open data log txt now of course if you want to display the contents of a different file you'll need to change that and then we simply go into this if loop if the data file does indeed exist while it is available we will write the contents of it out to the serial monitor and then we will close the file if the file isn't available we'll pop an error message into the serial monitor instead and that's all there is to it again there is no code in the loop so let's run that and display the contents of our data log ext file so let's use dump file to examine the file we created with the data logger so let's open our serial monitor and as we can see we're starting to read the file and this is a very long file as you saw when we looked at it on the computer so this is going to go on for quite some time but it seems to work very well you can see the position of the three different potentiometers and this will continue until we've dumped the entire file which is going to take a bit of time so we won't wait for it we'll move on to the next experiment so now we've seen a number of examples that came with the arduino ide that show you how to use an SD card it's time to write some of our own code now what our code is going to do is it's going to work with a servo motor we're going to use a potentiometer to move the servo into various positions and we're going to record those positions on the SD card then I will show you a second piece of code where we can play back what we recorded and send the servo through the same sequence now you can expand upon this in many different ways first of all you could combine the two sketches to make a recorder and playback unit if you wanted to another thing you could do is you could use multiple servo motors and control perhaps a robotic arm put it through a bunch of sequences and then play those sequences back any time that you want to you could also use devices that are controlled by pulse width modulation using the same basic code and things like that would include LEDs or DC motors as I said at the beginning of the video you could use this type of code to run a little robotic car around the room in a preset pattern and then record that pattern so the car could go through it over and over again so let's take a look at how we're going to hook this up first and then I'll show you the sketch that we can use to record the movements and then I'll show you the one we can use to play them back now to hook up our servo motion recorder I'm going to start with essentially the same that we had for the datalogger I've removed two of the potentiometers and left the one that is connected to analog pin a zero next I'm going to add a servo motor and I'm also going to add a separate power supply I'm using my 5 volt bank supply but you could also use a 6 volt battery I don't like powering my servos from the same power supply that the Arduino uses I'll start by connecting pin 9 from the Arduino to the control pin on the servo next the positive side of the power supply will be connected to the VCC pin on the servo motor I'll connect the negative side of the power supply to the ground on the servo we'll also make a connection to the Arduino ground this is very important so they'll forget that here's the sketch we're going to use to record the position of our servo motor and write it to an SD card now we'll start off by including the required libraries the SPI library the SD library and the servo library all of these libraries are built into the Arduino IDE so you won't need to add anything to your IDE next we define the chip select pin as being pinned for of course if you're using a shield with a different chip select pin you'll need to change that we define the pin we've got the pot connected to which is pin 0 analog pin a 0 and we'll also define a value to hold the pot value and we'll set that to a value 0 to start with we create a servo object called my servo and then go into the setup will set up the serial monitor at 9600 baud print initializing SD card to the stereo monitor and then we will initialize the SD card itself and print card initialized to the monitor once that's done we'll also attach the servo to the servo object on pin number 9 now if you're using a different pin you will need to change that pist remember that a servo needs to use a pin that's capable of pulse width modulation then we go into the loop we create a called data stirring and initialize it with a blank value and then we get the value from our pot using the analog read command and we use the map command to map it from a value of 0 to 1023 down to a value of 0 to 180 which is what our server requires and we assign that to the valve area below then we will append that variable to our data string next we will write to the servomotor with the same value to move the servo into position and delay for 15 milliseconds is to allow the servo to stabilize then we'll open the file and we're going to call our file servo pause dot txt and we're going to open it for writing and then we will write the data to the file and if the file doesn't work we'll pop up an error otherwise we close it and we repeat the loop over and over so we continually write the data to the file as we move the pot position and change the value so now that you've seen the sketch let's take a look at it in action okay so we have our demo of our servo recorder here's the servo motor over here in the pot that I'm using to control it now as you can see when I move the pot the servo moves accordingly and also if you take a look at the serial monitor you'll see that I'm recording the values right now and those values are being written into this SD card over here so I'm going to move this around just a little bit and finish off over here let's try to finish about in the center that's a value of about 90 over there okay so now we've recorded the data onto the SD card let's take a look at the sketch we'll need to use in order to play that data back now here's the sketch we're going to use to playback the servo motion that we recorded on to the SD card we're going to start off by including the same three libraries we did before and also defining the tip select for the SD card module as we did before well define a string that we call buffer it's going to hold one line of text at a time and we're going to create a servo object called my servo now all of this sketch is going to run in the setup routine if you want to continually move the servo in the same pattern that you've recorded you can move some of this into the loop now we're going to start again by opening up the serial monitor at 9600 and printing out initializing SD card and then initializing the SD card itself exactly as we did in the previous sketches will also attach pin 9 to the servo as we did before and we will open the servo pause dot txt file which is the file we recorded the servo motion on now we're going to read that file and while the file data is available we're going to read the string until we get to a newline character which is what this is and that way we will just read one entry at a time if we don't do that the entire file will be read and we'll only get the last entry we're going to print the value to the serial monitor that's the value that's sitting in the buffer and then we need to convert that value to an integer so we're going to use the two int function and will convert the buffer value to an integer write that to the servo and once again delay our servo by 15 milliseconds is to give it time to stabilize and then we close the data file after we do this so this is a while loop so we will do this continually until we are out of data and if the file doesn't open will pop up an error message and that's basically it there's nothing in the loop but as I said you could move this section of the sketch into the loop if you wish to continually move the servo in the same pattern so now that we've seen the sketch let's take a look at it all right so let's run our playback demo now I've got it started and a few seconds we can see the servo move also take a look at the serial monitor and there we go and so this is playing back the movements that I recorded originally of the servo and now we're parking it at the 90 degree mark and so this seems to work very well all right well that wraps it up for today's video I hope you enjoyed it and I hope it's got you thinking about how you can use SD cards in your own Arduino applications now if you'd like to see more videos like this the best way to find out about them is to subscribe to the YouTube channel and you can do that by clicking the subscribe button that's just below this video or the little robot that's in the bottom corner of the video and he'll subscribe you as well another thing is if you need some more details about what I've talked about today or if you'd like to get the code for the SD card recorder and playback that I just used you can find that on the article and the drum bots workshop dot-com website and you will also find a link to that below the video while you're on the website if you would consider joining my newsletter I would be very honored my newsletter is not a spam letter by any means and it's not a sales letter it's my way of keeping in touch with you to let you know what's going on in the workshop and also to solicit your opinions about the videos and articles that I create so I can always create better content for you you'll also find a link to that below the video so until we meet next time please take care of yourselves and I hope to see you again very soon here in the drone bots workshop goodbye for now [Music]
Info
Channel: DroneBot Workshop
Views: 116,957
Rating: undefined out of 5
Keywords: arduino project, Arduino SD Card, SD Car Module, microSD
Id: PQhQfww-qGQ
Channel Id: undefined
Length: 31min 53sec (1913 seconds)
Published: Sat Mar 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.