MOSI | MISO | SCK | CS | |
Arduino Uno | 11 | 12 | 13 | 10 |
Arduino Nano | 11 | 12 | 13 | 10 |
Arduino Mega | 51 | 50 | 52 | 53 |
chipSelect (CS)
pin of the SD card module is connected. The CS pin is the only one that is not really fixed as any of the Arduino digital pin. We don’t need to declare other SPI pins since we are using hardware SPI interface and these pins are already declared in the SPI library. After declaring the pin, we then create an object myFile, which will be used later on to store data on the SD card.setup()
section: We start the serial communication for showing the results on the serial monitor. Now, using the SD.begin()
function we will initialize the SD card and if initialization is successful the “if” statement becomes true and the String “initialization done.” gets printed on the serial monitor, else the string “initialization failed!” gets printed and the program terminates.SD.open()
function will open the file named “test.txt”. In our case, as such file is not present, it’ll be created. The other parameter FILE_WRITE
Lady gaga cheek to cheek album zip. opens the file in read-write mode. Jagged alliance 2 savegame editor.myFile.println()
function we will write the text “testing 1, 2, 3.” into the file. After that we need to use close()
function to ensure that the data written to the file is saved.SD.open()
, but this time as the file “test.txt” has already been created, the function will just open the file. Then using the myFile.read()
function we will read from the file and print it on the serial monitor. The read()
function actually reads just one character at a time, so therefore we need to use the “while” loop and the function myFile.available()
to read all characters in file. At the end we need to close the file.setup()
function which runs just once, instead of putting it in a loop()
function which runs over and over again.print()
and println()
functions just like Serial objects, to write strings, variables, etcRead()
only returns a character at a time. It does not read a full line or a number!close()
the file(s) when you’re done to make sure all the data is written permanently! This reduces the amount of RAM used.SD.open('/myfiles/example.txt')
. Note that the file path is relative.exists('filename.txt')
which will return true or false.remove('unwanted.txt')
be careful! This will really delete it, and there’s no ‘Recycle Bin’ to pull it out of.mkdir('/mynewdir')
handy when you want to stuff files in a location. Nothing happens if it already exists but you can always call SD.exists()
above first.seek()
on a file. This will move the reading/writing cursor to a new location. For example seek(0)
will take you to the beginning of the file, which can be very handy!position()
which will tell you where you are in the file.size()
to get the number of bytes in the file.isDirectory()
openNextFile()
openNextFile()
on a directory. In this case, call name()
which will return a pointer to the 8.3-formatted character array you can directly Serial.print()
if you want.