[adrotate banner=”7″]

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 amp 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.


[adrotate banner=”1″]

can arduino record video Video

[mam_video id=PQhQfww-qGQ]



[adrotate banner=”2″]


[mam_tag id=5560]



[adrotate banner=”3″]


can arduino record video news






[adrotate banner=”4″]



can arduino record video Social



[adrotate banner=”5″]




Last weekend we announced that we’re working on a new development environment with advanced features. Let’s take a deeper look at what is in store for the Arduino Pro IDE!

“Let us change the world by making technology accessible to everyone and put it into the hands of every student and educator.”

My Week Ending January 27, 2019

Electronic LEGO Super Mario Bros. Dry Bones Koopa Sprite


[adrotate banner=”6″]