Ardurio收集的傳感器數(shù)據(jù)存入sd卡

本章節(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ù)進行分析處理

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容