Created
June 17, 2014 19:59
-
-
Save jrleeman/89481921a29d53917438 to your computer and use it in GitHub Desktop.
Pressure Temperature Logger for Arduino Data Shield
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SD.h> | |
#include <Wire.h> | |
#include "RTClib.h" | |
// Data logger for temperature and pressure data using the Adafruit | |
// logger shield. | |
// Logging Interval (milliseconds) | |
#define LOG_INTERVAL 60000 | |
// Write Interval (milliseconds). How often to write data to the SD card | |
// Writing every time would be better and prevent any data loss, but this | |
// will use much less power. Power is indeed a concern for this setup | |
#define WRITE_INTERVAL 600000 | |
uint32_t syncTime =0; | |
// Echo to serial settings. Turn on (1) for daignostics, but turn off (0) | |
// before deploying to save power. | |
#define SERIAL_DIAG 1 | |
// Pin definitions - where we have things connected | |
#define PressurePin 0 | |
#define TempPin 1 | |
#define ChipSel 10 | |
#define BANDGAPREF 14 // special indicator that we want to measure the bandgap | |
#define aref_voltage 5.0 // Analog voltage reference | |
#define bandgap_voltage 1.1 // this is not super guaranteed but its not -too- off | |
RTC_DS1307 RTC; | |
// Setup the logging file | |
File logfile; | |
void error(char *str) | |
{ | |
Serial.print("error: "); | |
Serial.println(str); | |
while(1); | |
} | |
void setup(void) | |
{ | |
Serial.begin(9600); | |
Serial.println(); | |
// Initalize the SD Card (or try anyway) | |
Serial.print("Initializing SD card..."); | |
pinMode(ChipSel, OUTPUT); | |
if (!SD.begin(ChipSel)) { | |
error("Card failed, or not present"); | |
} | |
Serial.println("card initialized."); | |
// Create the logging file | |
// Change the file name here! | |
char filename[] = "PressureLogger.CSV"; | |
for (uint8_t i = 0; i < 100; i++) { | |
filename[6] = i/10 + '0'; | |
filename[7] = i%10 + '0'; | |
if (! SD.exists(filename)) { | |
// only open a new file if it doesn't exist | |
logfile = SD.open(filename, FILE_WRITE); | |
break; // leave the loop! | |
} | |
} | |
if (! logfile) { | |
error("couldnt create file"); | |
} | |
Serial.print("Logging to: "); | |
Serial.println(filename); | |
// connect to RTC | |
Wire.begin(); | |
if (!RTC.begin()) { | |
logfile.println("RTC failed"); | |
#if SERIAL_DIAG | |
Serial.println("RTC failed"); | |
#endif //ECHO_TO_SERIAL | |
} | |
logfile.println("Millis,Stamp,DateTime,Pres,Temp,Vcc"); | |
#if ECHO_TO_SERIAL | |
Serial.println("Millis,Stamp,DateTime,Pres,Temp,Vcc"); | |
#endif //ECHO_TO_SERIAL | |
// If you want to set the aref to something other than 5v, use this. Docs online. | |
//analogReference(EXTERNAL); | |
} | |
// This is the main loop | |
void loop(void) | |
{ | |
DateTime now; | |
// delay for the amount of time we want between readings | |
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); | |
// log milliseconds since starting | |
uint32_t m = millis(); | |
logfile.print(m); // milliseconds since start | |
logfile.print(", "); | |
#if SERIAL_DIAG | |
Serial.print(m); // milliseconds since start | |
Serial.print(", "); | |
#endif | |
// Get time from the RTC | |
now = RTC.now(); | |
// log time | |
logfile.print(now.unixtime()); // seconds since 1/1/1970 | |
logfile.print(", "); | |
logfile.print('"'); | |
logfile.print(now.year(), DEC); | |
logfile.print("/"); | |
logfile.print(now.month(), DEC); | |
logfile.print("/"); | |
logfile.print(now.day(), DEC); | |
logfile.print(" "); | |
logfile.print(now.hour(), DEC); | |
logfile.print(":"); | |
logfile.print(now.minute(), DEC); | |
logfile.print(":"); | |
logfile.print(now.second(), DEC); | |
logfile.print('"'); | |
#if SERIAL_DIAG | |
Serial.print(now.unixtime()); // seconds since 1/1/1970 | |
Serial.print(", "); | |
Serial.print('"'); | |
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.print('"'); | |
#endif //ECHO_TO_SERIAL | |
analogRead(PressurePin); | |
delay(10); | |
int PressureReading = analogRead(PressurePin); | |
analogRead(TempPin); | |
delay(10); | |
int tempReading = analogRead(TempPin); | |
// converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0 | |
float voltage = tempReading * aref_voltage / 1024; | |
float temperatureC = (voltage - 0.5) * 100 ; | |
float temperatureF = (temperatureC * 9 / 5) + 32; | |
logfile.print(", "); | |
logfile.print(PressureReading); | |
logfile.print(", "); | |
logfile.print(temperatureF); | |
#if SERIAL_DIAG | |
Serial.print(", "); | |
Serial.print(PressureReading); | |
Serial.print(", "); | |
Serial.print(temperatureF); | |
#endif //ECHO_TO_SERIAL | |
// Log the estimated 'VCC' voltage by measuring the internal 1.1v ref | |
analogRead(BANDGAPREF); | |
delay(10); | |
int refReading = analogRead(BANDGAPREF); | |
float supplyvoltage = (bandgap_voltage * 1024) / refReading; | |
logfile.print(", "); | |
logfile.print(supplyvoltage); | |
#if SERIAL_DIAG | |
Serial.print(", "); | |
Serial.print(supplyvoltage); | |
#endif // ECHO_TO_SERIAL | |
logfile.println(); | |
#if SERIAL_DIAG | |
Serial.println(); | |
#endif // ECHO_TO_SERIAL | |
// Dump data to the disk. This is power hungry | |
if ((millis() - syncTime) < WRITE_INTERVAL) return; | |
syncTime = millis(); | |
logfile.flush(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment