[尼克的robot]arduino SD卡 储存 实作 伫立的一棵树
[尼克的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.字样
之后会教大家怎么写入感应器的读值
还有每句程式码的功能喔