Arduino SD Card Reader Tutorial: Initialization, Technical Insights, and Data Logging

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it's been a month since my last video the longest break so far in between my posts other commitments apart from YouTube kept me extremely busy making it difficult to find time this video is going to be about SD card readers I hope you will find it useful as year 2023 is coming to an end I would like to thank everyone who watched my channel subscribe to it and like the videos special thanks to my patrons and channel members for their extra support I am looking forward to creating more interesting content in year 2024 wishing you all the best for the upcoming year may it bring you a lot of happiness and prosperity and proficiency with our doino [Music] [Applause] microcontrollers so here is the SD card module it is an SPI device so it has miso mossi clock and Chip select pens which are standard for such a device there is a slot where you can insert the SD card in my case this is 8 gabes high density card let's look at how this module should be connected to Arduino connecting ground and VCC is straightforward then we connect the clock to digital pin 133 miso 212 and mossy 211 these connections cannot be changed the last one is the chip select and through this pin the device is identified on the SPI bus in my case I chose to use pin 10 to control this SD card module from Arduino code we need to use two libraries the first one is the SPI Library necessary to control the SPI devices and the second is the SD Library these libraries are included in the Arduino IDE installation by default and do not have to be installed separately like any other Library the SD Library comes with a few example sketches which is a great place to start while exploring these examples I noticed something peculiar it appears that sd. H library is essentially a layer on top of another library that was created before this becomes quite clear when you compare the two example codes cart info and file in the cart info example you will notice three classes that are not present in the other example the commands to initiate SD card are different the file example appears to be easier to use trying to solve the mystery surrounding the library I stumbled upon a thread on the Arduino Forum where the individual behind the code clarified that he initially created the library called SD fat this SD fat Library served as a foundation for the standard Library provided in Arduino ID the individual explained SD is a rapper for STD fat when I wrote SD fat I designed three main classes sd2 card STD volume and STD file the Arduino group wrote a rapper class to simplify IO to SD cards that contains the three classes you can't easily integrate the functionality of both in one sketch so in this video I will compare both methods to try to achieve the same results for the first few sketches that I will write and explain afterwards I'll switch to using easier approach demonstrated in the file example first we'll look at how to declare and initialize SD cards both methods require the same libraries however the first method requires three additional classes that we mentioned earlier while the second method does not need them regardless of the method we need to connect the chip select pin or the SD card to Arduino digital pin 10 in setup we open serial Monitor and then we initialize the SD card the command to initialize SD card is different depending on the method used in both cases if the initialization fails the program will stop and go into an endless loop otherwise we output a message stating that it was successful next we'll try to display technical information about the SD card that we have just initialized let's start by identifying the type of SD card there are three types of SD cards two for standard capacity and one for high capacity we can identify them using these labels or values one 2 or three in the first method of card initialization we have a card object that has a type attribute we can write code to check the value of the type attribute and based on the result output that information to the serial Monitor and how can we display card type information using the second method this is the thing you can't I search for quite a bit and realized that initializing the SD card in this way does not allow you to access its technical information let's talk about the size of the SD card and how data is organized on it before we look at the code it's important to understand the basic structure of storing data on the SD card the smallest unit of data on the SD card is a block which is half a bite in size these blocks are organized into clusters which are consecutive groups of blocks used for file allocation all the Clusters together form a volume which represents a logical partition on the SD card the volume has its own own specific file system defining how files and directories are organized and accessed within that volume in the first initialization method we work with a volume object this object has two attributes the number of clusters and the number of blocks per cluster to calculate the volume size we multiply these two attributes together however since each block is only half a bite we need to divide the result by two to get the size in kilobytes we can also display the type of file system used on the SD card to do this we can use the fat type attribute of the volume object there are three types of file systems fat 12 fat 16 and fat 32 to display the file system type we can use these lines of code now let's combine all these commands into a single sketch and run it to see the result but first we need to connect SD card to Arduino starting with ground and VCC connections and then we continue with SPI connections including chip select going to digital pin 10 here is the composite sketch obviously we would use functionality from the fat Library as the alternative approach doesn't provide functionality to display this technical information here here is the Declaration section which includes libraries sdat classes and the chip select pin in the setup we have code to initialize the module continuing in the setup we identify and display the card type then we initialize the volume and display both volume size and volume type let's load the code to observe how the sketch works all information is displayed correctly now let's move on to listing the content of the SD cards we can do it for both initialization methods in the first method we use the SD file object called root that represents the main directory of the volume before we can use it we need to initialize the volume in the same way we did when displaying SD card technical information in the previous sketch by using the ls method which is similar to the ls- LR command in Unix we can see all the files in the main directory and its subdirectories along with their creation dates and sizes let's load the sketch to Arduino and see the result in the serial monitor we see all the files on the SD card including subdirectories in the second initialization method displaying the content of the SD card is much more difficult imagine we have a directory name saved in the variable called dir now if we use the open next file method it will assign the first file in that directory to the variable called entry which represents a file object when we run the open next file method again it will assign the next file it finds we can keep doing this until we go through all the files in the directory each file object has three attributes name is directory and size the name attribute gives the name of the file the is directory attribute tells us if the file is actually a directory or not and the size attribute tells us the size of the file here we have no attribute for the creation date of the file now we can create a function called display directory that will list all the files in a given directory including subd directories by passing the directory to the function as a parameter to handle nested subdir directories we'll use recursion in the function meaning that if we encounter a subdirectory we'll call the same function again for that subdirectory by using the num top we can create an indent for each level of nested subdirectory this will help us display the files in a structured and organized manner making it easier to understand the directory structure inside the endless loop we continuously read files and assign them to a file object called entry if the assignment is successful we first attempt to create an invention initially there will be none as the ntop parameter was set to zero for the first execution next we print the name of the file if the object happens to be a directory we recursively execute the display directory function for that directory increasing the num top value by one this ensures that all the files in that subd directory will be displayed with an additional top indention on the other hand if the file is not a directory we display its size after processing each file object it is important to close it properly to display the entire content of the SD card we run this function for the root directory let's send the code to the microcontroller and check the data sent to the serial monitor you can see all the files on the SD card listed but the information is displayed differently than in the previous method now that we know how to display the content of the SD card let's try to open the file in the project sub directory and display its content let's take a peek into that file so we know what the result should be we have a list of five of my Arduino projects created on this channel after initializing the SD card we use the open method to open the project txt file and assigned it to the file variable if the file can be opened in a loop we read characters from it and save them into a string variable we do this until we encounter The End of Line character if we do that means we have read the entire line and we output it to the serial monitor we repeat the process until we reach the end of the file this way we display the entire content when done we need to close the file let's load the code onto the microcontroller and see if this works all five lines of text were displayed properly next let's look at creating the text file on the SD card and filling it with data you might be surprised to find out that here we also use the open method so when we run that method with the name of the file that does not exist Arduino will attempt attt to create that file if the process is successful we may write a few lines of code into the file using the print method when done we close the file when the operation succeeds we send the appropriate message to the serial monitor we also send a failure message if the file failed to be created let's send the code to the microcontroller and run it we get the success message I will now take the SD card out and plug it into the computer as you can see the file we have just created is there let's open it and you can see all the lines of text we populated now that we have learned how to create a text file add lines of text to it and then display that content we can explore the process of editing such file by removing or modifying lines of text when I started working on this tutorial I thought of a straightforward process taking the text file file let's use the one we have just created and making modifications while adding content is a simple task it always append to the end of the file so what do we do if we need to correct data or remove specific lines of text when you look at the file you see some inconsistencies that require corrections to address this I'll open yet another file and process the original file line by line if a line is correct it is seamlessly Rewritten to the new file however when adjustments are needed such as here when the line was in upper case we write a corrected value into the new file in instances where a line needs to be deleted we simply skip writing it to a new file when the process is complete my initial plan was to remove the original file and rename the temporary file to match the original file name I assumed the existence of the rename method but I was wrong such method does not exist so the only feasible option for editing and correcting the file is to open a new file rewrite the content to it and make the necessary adjustment in the process this results in having two files at the end of the process not ideal but at least it was not a complete failure let's quickly review the code we start by opening the text file that we want to correct if successful we proceed to open the second temporary file then we Implement a loop similar to one used when displaying the content of the file we read lines one by one and subject them to conditions in our case we aim to change the third line to lowercase and ignore the Fifth Line both files need to be close at the end here also I include error messages in case either of the two files fail to open now let's load the code onto the microcontroller for the last time the program is finished I'll once again remove the SD card and inspect it on my computer and what do you know the new file is created and the content looks okay that concludes this video we have established that SD card readers are excellent devices for data logging for saving readings from various sensors to a text file but for operations like opening the file and data manipulation not so much in this video I only scratch the surface I am eager to explore working with more complex files like bit Maps or Row video footage learning to send images or videos frame by frame to devices like OLED display via iarec interfaces it's a fascinating but much more complex subject and I will definitely create some videos on that topic in the future but for now this is it I see you guys in my next video [Music] ciao
Info
Channel: Mario's Ideas
Views: 3,949
Rating: undefined out of 5
Keywords: Arduino, uno, SD Card Reader, SD card, tutorial, easy project, arduino programming, how to, DIY, data logging
Id: sLqgitXERLg
Channel Id: undefined
Length: 17min 20sec (1040 seconds)
Published: Tue Jan 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.