Data Logger Shield เป็น shield ที่ใช้สำหรับเก็บค่าต่างๆ ตามเวลาที่ต้องการ เช่น ค่าของ Sensor ต่างๆ โดยค่าที่ต้องการจะบันทึกค่าลงบน SD Card ใน shield ประกอบด้วย Real time clock (RTC) ช่องเสียบ SD Card และช่องใส่ถ่าน Back up เพื่อช่วยให้ RTC ยังทำงานอยู่ในกรณีไม่มีไฟเลี้ยงบอร์ด ใช้งานได้กับบอร์ด Arduino UNO, Duemilanove, Diecimila, Leonardo, ADK/Mega R3 (ต่อสายเพิ่ม)
Library ที่นำมาใช้งานจะประกอบด้วยกัน 2 ส่วนคือ
ดาวน์โหลด Library SD Card
ดาวน์โหลด Library Real Time Clock (RTC)
เมื่อทำการ add library ทั้ง 2 เรียบร้อยแล้วลำดับแรกที่ต้องทำคือ ใส่ถ่านลงไปที่ช่องใส่ถ่านเพื่อเป็นไปเลี้ยงให้กับ Real Time Clock จากนั้นนำโปรแกรมด้านล่าง ไปใช้โหลดลงบอร์ด arduino Uno ที่ติดตั้ง Shield ลงไปเรียบร้อยแล้ว โปรแกรมจะตั้งเวลาให้อัตโนมัติ
ตัวอย่างการตั้งเวลา
#include<Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup ()
{
Serial.begin(57600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
เมื่อโหลดโปรแกรมไปยัง บอร์ด Arduino Uno เรียบร้อยแล้วทำการเปิด Serial Monitor ขึ้นมาค่าที่ได้ดังแสดงในรูปโปรแกรมจะทำการตั้งค่าเวลาให้อัตโนมัติ
*** หากถอดแบตเตอรีไฟเลี้ยงออกหรือแบตหมด เวลาที่ได้จะมีค่าผิดไปจากเดิม
ตัวอย่างการเขียนข้อมูลไปยัง SD CARD
กำหนด Pin ใช้งานในการอ่าน/เขียนไฟล์สำหรับ UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 10 (CS pin อาจมีการเปลี่ยนแปลงได้)
และ pin #10 (SS) must be an output
กำหนด Pin ใช้งานในการอ่าน/เขียนไฟล์สำหรับ Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 10 (CS pin อาจมีการเปลี่ยนแปลงได้)
และ pin #52 (SS) must be an output
นำโปรแกรมด้านล่างไปใช้โหลดลงบอร์ด arduino Uno ที่เสียบ SD CARD เข้าไปเรียบร้อยแล้ว (SD Card ที่ใช้ Format ให้อยู่ในรูแบบ FAT32)
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
File dataFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(SS, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1) ;
}
Serial.println("card initialized.");
// Open up the file we're going to log to!
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (! dataFile) {
Serial.println("error opening datalog.txt");
// Wait forever since we cant write data
while (1) ;
}
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++)
{
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {dataString += ","; }
}
dataFile.println(dataString);
// print to the serial port too:
Serial.println(dataString);
dataFile.flush();
// Take 1 measurement every 500 milliseconds
delay(500);
}
เมื่อโหลดโปรแกรมไปยัง บอร์ด Arduino Uno เรียบร้อยแล้วทำการเปิด Serial Monitor ขึ้นมาค่าที่ได้ดังแสดงในรูป
ตัวอย่างการเขียนโปรแกรมให้บันทึกค่าตามเวลาที่ต้องการ
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>
RTC_DS1307 rtc;
// ค่า sensor สมมติ//////
int Sensor1 = 20;
int Sensor2 = 3.14;
boolean Just_One = true;
String dataToSDCard = "";
////////////////////////
const int chipSelect = 10;
void setup () {
Serial.begin(57600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
//กำหนดให้บันทึกค่าที่ 20.00น. ของทุกๆวัน
if(now.hour() == 20)
{
if(Just_One)
{
dataToSDCard += String(now.hour()) + " "+ String(now.minute()) + " " + String(Sensor1) + " " + String(Sensor2);
Just_One = false;
writeDataToSD();
}
}
else
Just_One = true;
Serial.println();
delay(3000);
}
void writeDataToSD()
{
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
dataFile.println( dataToSDCard );
dataFile.close();
// print to the serial port too:
Serial.println( dataToSDCard );
}
// if the file isn't open, pop up an error:
else
{
Serial.println("error opening datalog.txt");
}
}
เมื่อโหลดโปรแกรมไปยัง บอร์ด Arduino Uno เรียบร้อยแล้วทำการเปิด Serial Monitor ขึ้นมาค่าที่ได้ดังแสดงในรูป
เปิดไฟล์ออกมาจะได้
หน้าที่เข้าชม | 7,146,891 ครั้ง |
ผู้ชมทั้งหมด | 2,843,789 ครั้ง |
เปิดร้าน | 15 ก.ย. 2557 |
ร้านค้าอัพเดท | 22 ต.ค. 2568 |