[尼克的robot]arduino SD卡 儲存 實作 佇立的一棵樹
會來找這篇文的人,或是有興趣點進這篇文的人
想必對arduino的數據儲存很有興趣吧
之前尼克也是想要把顯示在電腦上的數據紀錄下來才來研究sd卡模組的
至於市面上的sd卡模組有分大小卡
尼克建議買大卡的穩定度會大一點
還有大卡有雙排的針腳
記得雙排都要插在麵包版上穩定度會更高喔
底下就是實際接線的情況
接線
5V - 5V
GND - GND(尼克兩個都接,但應該是一個
MOSI - pin 11
MISO - pin 12
CLK - pin 13
CS - pin 4
3.3V - 不用接
程式部分 :
建立一個test.txt檔
然後寫入testing 1, 2, 3
(如果複製到廣告記得刪掉中文字喔)
/*
尼克的robot網址 :
https://kenny2019.pixnet.net/blog
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
傳入程式後打開序列埠
等待一下就可以看到寫入的狀況
如果錯誤則會出現以下情況
請檢察接線有沒有接錯(正負極是接好,訊號線順序)
如果成功的話就會出現以下的畫面
拔出記憶卡在電腦讀取SD卡也會出現一個test檔
裏頭有testing 1,2,3.字樣
之後會教大家怎麼寫入感應器的讀值
還有每句程式碼的功能喔