本章節(jié)我們將介紹如何實現(xiàn)將Ardurion收集的傳感器數(shù)據(jù)存入sd卡中(火焰?zhèn)鞲衅鳌貪穸葌鞲衅鳛槔?/p>
準備器材:
Arduino開發(fā)板一塊
杜邦線若干
溫度傳感器、火焰?zhèn)鞲衅?br> SD卡模塊及一張SD卡

SD卡模塊連接示意圖
火焰?zhèn)鞲衅鲗嵗a
#include <SPI.h>
#include <SD.h>
#include<time.h>
int fireSensor = A0;
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(fireSensor,INPUT);
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.
// re-open the file for reading:
myFile = SD.open("demo.txt");
/* if (myFile) {
Serial.println("zk.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() {
String dataString="";
Serial.println(analogRead(fireSensor));
int sensor = analogRead(fireSensor);
dataString += String(sensor);
dataString += ",";
myFile = SD.open("demo.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println(dataString);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
delay(5000);//延時5s
}
溫濕度傳感器實例代碼
#include <DHT11.h>
#include <SPI.h>
#include <SD.h>
#include<time.h>
dht11 DHT11;
File myFile;
#define DHT11PIN A0
void setup()
{
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.");
myFile = SD.open("zk.txt");
}
void loop()
{
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
//濕度讀取
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);
//溫度讀取
String dataString="";
int Humidity = DHT11.humidity;
int Temperature = DHT11.temperature;
dataString += String(Humidity);
dataString += ".00";
dataString += ",";
dataString += String(Temperature);
dataString += ".00";
myFile = SD.open("demo.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println(dataString);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
delay(5000);
}
將代碼燒寫到Arduino開發(fā)板,如果順利傳感器手機的數(shù)據(jù)成功寫入SD卡,我們可以使用讀卡器查看SD卡中的內(nèi)容,如下圖:

圖片.png
打開demo.txt對傳感器收集數(shù)據(jù)進行分析處理