Arduinoでデータロガーを作る【温度センサ+SDカード+RTC】 [Arduino]
これまでで、
#Arduinoでデータロガーを作る【温度センサ+SDカード】
#ArduinoでRTCを使う
ができたので、これらを一つにまとめてタイムスタンプとセットでデータを保存できるようにしたいと思います。
【配線図】
Fritzingの配線図では省略してしまったのですが、RTCのVCCとGNDには0.1μFのパスコンが接続してあります。ブレッドボードやジャンプワイヤのせいもあるかもしれませんが、動作が不安定だったのでパスコンはあった方がいいと思います。
あと、SDカード配線引出基板と電源とGNDの間のパスコンも。#ArduinoでSDカード情報を取得する
【スケッチ】
これで、タイムスタンプと温度データを一緒にSDカードのファイルに保存することができました。
#Arduinoでデータロガーを作る【温度センサ+SDカード】
#ArduinoでRTCを使う
ができたので、これらを一つにまとめてタイムスタンプとセットでデータを保存できるようにしたいと思います。
【配線図】
Fritzingの配線図では省略してしまったのですが、RTCのVCCとGNDには0.1μFのパスコンが接続してあります。ブレッドボードやジャンプワイヤのせいもあるかもしれませんが、動作が不安定だったのでパスコンはあった方がいいと思います。
あと、SDカード配線引出基板と電源とGNDの間のパスコンも。#ArduinoでSDカード情報を取得する
【スケッチ】
/******************************************************************************* SDカードデータロガー ++++++ note ++++++ ・測定した温度をコンソール画面に表示およびSDカードにデータ保存するプログラム ・Arduino Pro Mini(3.3V, 8MHz) ・温度センサ LM61BIZ(測定範囲:-25℃~+85℃) ・(※)RTCから取得した現在の日時データをSDカードファイル書き込みとコンソール画面表示に使う *******************************************************************************/ #include#include #include "RTClib.h" //RTCライブラリを導入 File myFile; //Fileオブジェクトの生成(インスタンス名:myFile) RTC_DS3231 rtc; //RTCオブジェクトの生成(インスタンス名:rtc) const float Vcc = 3.3; //電源電圧(定数) float temp_volt; //出力電圧 float temp_data; //温度 char daysOfTheWeek[7][6] = {"Sun.", "Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sat."}; String youbi; //曜日を代入する int year, month, day, hour, minute, second; // the setup routine runs once when you press reset:初期化(電源投入/リセット時1回のみ実行) void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); //9600bpsでシリアルポートを開く while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } //RTCオブジェクトの初期化 if (! rtc.begin()) { //RTCオブジェクトに初期化に失敗 Serial.println("Couldn't find RTC"); Serial.flush(); abort(); } //RTCオブジェクトに初期化に成功 // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //今の(PCの)時刻に合わせる // →合わせ終わったらコメントアウトすること //SDカードの初期設定 Serial.print("Initializing SD card..."); if (!SD.begin(10)) { //4→10に変更 SDカードの初期化に失敗 Serial.println("initialization failed!"); while (1); } //SDカードの初期化に成功 Serial.println("initialization done."); } // the loop routine runs over and over again forever: main loop void loop() { int sensorValue = analogRead(A0); //read the input on analog pin 0: temp_volt = Vcc * sensorValue / 1023; //アナログピンから読み取った値を元の出力電圧値に戻す // 出力電圧から温度データを作成 temp_data = (temp_volt - 0.6) / 0.01; //【データシート】T[℃] = (Vo - 0.6) / 0.01 // RTCから現在の日時を取得(※) DateTime now = rtc.now(); year = now.year(); month = now.month(); day = now.day(); youbi = daysOfTheWeek[now.dayOfTheWeek()]; hour = now.hour(); minute = now.minute(); second = now.second(); // RTCの日時と温度データをコンソール画面に表示(※) // ①float(浮動小数点)形式で表示 Serial.print(year); //RTCの日時を表示 Serial.print('/'); Serial.print(month); Serial.print('/'); Serial.print(day); Serial.print('('); Serial.print(youbi); Serial.print(')'); Serial.print(hour); Serial.print(':'); Serial.print(minute); Serial.print(':'); Serial.print(second); Serial.print(" "); //tab(空欄)はダブルクォーテーションで囲む Serial.print(temp_data); //温度を表示 Serial.println("[℃]"); //単位は℃ // SDカードに書き込む // 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("tempdata.txt", FILE_WRITE); //"tempdata.txt"ファイルを書き込み用に開く //戻り値がファイル情報になり、myFile に受け渡す // ファイルに温度データを書き込み(※) // if the file opened okay, write to it: if (myFile) { // ①float(浮動小数点)形式で書き込み myFile.print(year); myFile.print('/'); myFile.print(month); myFile.print('/'); myFile.print(day); myFile.print('('); myFile.print(youbi); myFile.print(')' ); myFile.print(hour); myFile.print(':'); myFile.print(minute); myFile.print(':'); myFile.print(second); myFile.print(" "); //tab(空欄)はダブルクォーテーションで囲む myFile.print(temp_data); myFile.println("[℃]"); // close the file: myFile.close(); //ファイルを閉じる //myFile.close() Fileオブジェクト(インスタンス)に対する関数 } else { // if the file didn't open, print an error: Serial.println("error opening tempdata.txt"); } delay(1000); //1秒単位 ※delay():単位ms }
これで、タイムスタンプと温度データを一緒にSDカードのファイルに保存することができました。
2021-03-13 10:32
nice!(0)
コメント(0)
コメント 0