I needed to log output analog voltage from an ozone analyzer's 0-1 V output for my research. I opted for an arduino lying around in the lab rather than purchasing a data logger. The sketch I managed to come up with after some reading here logs the data and milli stamps on the SD but not the voltage!!
Obviously I followed the same protocols with log.data for the voltage, while it does show it on the Serial it DOES NOT log to the SD. I don't know what I'm missing.
Also a lot of the sketches here for data loggers lack something, so I had to get bits and pieces of my sketch from different examples.
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
// A simple data logger for the Arduino analog pins
#define LOG_INTERVAL 5000 // mills between entries
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
// The analog pins that connect to the sensors
#define voltage 0 // analog 0
RTC_DS1307 rtc; // define the Real Time Clock object
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// 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();
#if WAIT_TO_START
Serial.println("Type any character to start");
while (!Serial.available());
#endif //WAIT_TO_START
// initialize the SD card
Serial.print("Initializing SD card SUCKER...");
// 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.print("card initialized.");
// create a new file
char filename[] = "LOGGER00.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);
Wire.begin();
if (!rtc.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("millis,time,voltage");
#if ECHO_TO_SERIAL
Serial.println("millis,time,voltage");
#endif
}
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 ECHO_TO_SERIAL
Serial.print(m); // milliseconds since start
Serial.print(", ");
#endif
// fetch the time
now = rtc.now();
// log time
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);
#if ECHO_TO_SERIAL
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
int reading = analogRead(voltage);
// converting that reading to voltage, for 3.3v arduino use 3.3
float Avoltage = reading * 5.0;
Avoltage /= 1023.0;
logfile.println(Avoltage);
logfile.flush();
#if ECHO_TO_SERIAL
Serial.print(Avoltage);
Serial.println(", ");
#endif //ECHO_TO_SERIAL
delay(5000);
}
One other quick question, will I fry the arduino if connected to both the USB port and the 5V adapter??
Thanks all