Arduino Projects of note/datalogger sdcard
From Federal Burro of Information
Jump to navigationJump to search
finally it works!
datalogger_sdcard.ino
/* SD card datalogger This example shows how to log data from three analog sensors to an SD card using the SD library. The circuit: * analog sensors on analog ins 0, 1, and 2 * SD card attached to SPI bus as follows: ** MOSI - pin 11 ** MISO - pin 12 ** CLK - pin 13 ** CS - pin 10 created 24 Nov 2010 modified 9 Apr 2012 by Tom Igoe This example code is in the public domain. */ #include <DS3231.h> #include <SPI.h> #include <SD.h> #include <Wire.h> #include "HTU21D.h" HTU21D myHumidity; DS3231 Clock; bool Century=false; bool h12; bool PM; byte ADay, AHour, AMinute, ASecond, ABits; bool ADy, A12h, Apm; byte year, month, date, DoW, hour, minute, second; const int chipSelect = 4; String txtfilename = "datalog.txt"; File myFile; String ReadDS3231() { String ds = ""; int second,minute,hour,date,month,year,temperature; second=Clock.getSecond(); minute=Clock.getMinute(); hour=Clock.getHour(h12, PM); date=Clock.getDate(); month=Clock.getMonth(Century); year=Clock.getYear(); temperature=Clock.getTemperature(); ds += "20"; ds += year; ds += "-"; ds += month; ds += "-"; ds += date; ds += " "; ds += hour; ds += ":"; ds += minute; ds += ":"; ds += second; ds += " clock temp:"; ds += temperature; return ds; } 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..."); // 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."); myHumidity.begin(); Serial.println("HTU21D initialized"); } void loop() { delay(360000); // make a string for assembling the data to log: String dataString = ""; //HTU21D float humd = myHumidity.readHumidity(); float temp = myHumidity.readTemperature(); //RTC String ds; ds = ReadDS3231(); dataString += "time: "; dataString += ds; dataString += " hum: "; dataString += humd; dataString += " temp: "; dataString += temp; // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. if (SD.exists("datafile.txt")) { // Serial.println("datafile.txt exists."); } else { Serial.println("datafile.txt doesn't exist. creating"); // open a new file and immediately close it: Serial.println("Creating datafile.txt..."); myFile = SD.open("datafile.txt", FILE_WRITE); myFile.close(); } // Check to see if the file exists: if (SD.exists("datafile.txt")) { // Serial.println("datafile.txt exists."); } else { Serial.println("datafile.txt doesn't exist."); } File dataFile = SD.open("datafile.txt", FILE_WRITE); // if the file is available, write to it: if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: Serial.println(dataString); } // if the file isn't open, pop up an error: else { Serial.println("error opening datafile.txt"); } }
Python style: Htu21d.py