Skip to content

Instantly share code, notes, and snippets.

@ProfJayHam
Created November 7, 2015 19:28
Show Gist options
  • Save ProfJayHam/a2862e4de19121c13742 to your computer and use it in GitHub Desktop.
Save ProfJayHam/a2862e4de19121c13742 to your computer and use it in GitHub Desktop.
HTU21DF data logger program for Arduino R3 and Adafruits datalogger shield
/* HTU_logger_3.ino
RH and Temp data logger for the Arduino using a Adafruits HTU21D breakout board and
datalogger shield
J. Ham, Colorado State Univesity
last revision: 11/05/2015
Desciption: Program gets Temperature and RH from HTU21D sensor at a user-defined
interval (5s default) and saves the result to SD card with time stamp obtained from the RTC.
Also calculates and saves Dewpoint temperature as calculated with user-defined function.
The program waits to start unitl 0 seconds into a 1 min interval.
Reference: portions adapted from adafruits lighttemplogger.ped
http://learn.adafruit.com/adafruit-data-logger-shield/downloads
Notes:The RTC must be set prior to running this program.
https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/understanding-the-code
search for the word "unique" to find user-adjustable variables such as the sampling
interval or the file name to be stored on the sd card.
Libraries: download and install the RTC and HTU21DF libaries
SD and RTC library: https://github.com/adafruit/RTClib/
HTU21DF library: https://github.com/adafruit/Adafruit_HTU21DF_Library/
Hardware Requirements:
Shield https://www.adafruit.com/products/1141
Sensor: https://www.adafruit.com/product/1899
Wiring for HTU21DF to Arduino or Datalogger Shield
VIN to 5V
GND to Ground
SCL to SCL (or analog 5, the I2C bus)
SDA to SDA (or analog 4, the I2C bus)
3Vo not used
Jumper wires: from D4 to L1, and D5 to L2 on Shield
*/
// include files
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include "Adafruit_HTU21DF.h"
// ECHO_TO_SERIAL controls output to computers serial monitor
// turn on (1) for testing/troubleshooting,// turn off (0)during field deployment
#define ECHO_TO_SERIAL 0 // 1 = echo results to screen, 0 = no echo, unique
// the digital pins that connect to the green and red LEDs on the datalogger shield
// run jumper wire from digital pins 4 and 5 to L1 and L2 connections on datalogger shield
#define greenLEDpin 4 // L1, flashing green indicates sampling
#define redLEDpin 5 // L2, solid red led indicates error
//class initialization
Adafruit_HTU21DF htu = Adafruit_HTU21DF(); // HTU21DF
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 for SD card
File logfile;
const unsigned long TRHSTEP = 5000UL; // Sensor query period, ms, unique
unsigned long trhMillis = 0; // Time interval tracking
//define global variables
float RH_htu, T_htu; // htu sensor output, RH and T
float T_dew;// dewpoint temperature
//******************** Setup ********************
void setup(void)
{
Serial.begin(9600);
Serial.println();
// LEDs on datalogger shield
pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
// initialize the SD card
#if ECHO_TO_SERIAL
Serial.print("Initializing SD card...");
#endif
// 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)) {
error("Card failed, or not present");
}
#if ECHO_TO_SERIAL
Serial.println("card initialized.");
#endif
// create a new file name
char filename[] = "RHlog_00.csv"; // "RHlog" is root name, must be 5 characters, unique
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");
}
#if ECHO_TO_SERIAL
Serial.print("Logging to: ");
Serial.println(filename);
#endif
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
error("RTC failed");
}
#if ECHO_TO_SERIAL
Serial.println("RTC initalized");
#endif //ECHO_TO_SERIAL
// initialize HTU sensor
htu.begin();
Wire.begin();
if (!htu.begin()) {
error("HTU failed");
}
#if ECHO_TO_SERIAL
Serial.println("HTU initalized");
#endif //ECHO_TO_SERIAL
// delay until time is 0 second into 1 min interval
DateTime now;
#if ECHO_TO_SERIAL
Serial.println("waiting to start at O sec into 1 min");
#endif //ECHO_TO_SERIAL
while(now.second() !=0) {
now = RTC.now();
}
// write header to SD card and screen
logfile.println("datetime,unixtime,temp,RH,tdew");
#if ECHO_TO_SERIAL
Serial.println("datetime, unixtime, temp, RH, tdew");
#endif
}
//******************* Main Loop ***********************
void loop(void)
{
unsigned long curMillis = millis(); // Get current time
// read sensor
if (curMillis - trhMillis >= TRHSTEP) { // Time for new measurements?
trhMillis = curMillis;
T_htu=htu.readTemperature(); // read temperature
RH_htu=htu.readHumidity(); // read humidity
T_dew=dewpoint(T_htu, RH_htu); // calc dewpoint
#if ECHO_TO_SERIAL
printData();
#endif
logData();
digitalWrite(greenLEDpin, HIGH); // tutn on LED in sample loop
} // end sampling loop
if (curMillis - trhMillis >= 500) { // turn off green LED after 0.5 sec
digitalWrite(greenLEDpin, LOW);
}
} // end main loop
//******************************************
//* subroutines and user defined functions *
//******************************************
float dewpoint(float T, float RH) {
// Murray (1967) On the Computation of Saturation Vapor Pressure
float b,dp;
b = (log(RH / 100) + ((17.27 * T) / (237.3 + T))) / 17.27;
dp = (237.3 * b) / (1 - b);
return dp;
}
void printData() {
DateTime now;
now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print(F("-"));
printDigits(now.month());
Serial.print(F("-"));
printDigits(now.day());
Serial.print(F(" "));
printDigits(now.hour());
Serial.print(F(":"));
printDigits(now.minute());
Serial.print(F(":"));
printDigits(now.second());
Serial.print(F(", "));
Serial.print(now.unixtime()); // seconds since 1/1/1970
Serial.print(F(", "));
Serial.print(T_htu);
Serial.print(F(", "));
Serial.print(RH_htu);
Serial.print(F(", "));
Serial.print(T_dew);
Serial.println();
}
void logData() {
DateTime now;
now = RTC.now();
logfile.print(now.year(), DEC);
logfile.print(F("-"));
logDigits(now.month());
logfile.print(F("-"));
logDigits(now.day());
logfile.print(F(" "));
logDigits(now.hour());
logfile.print(F(":"));
logDigits(now.minute());
logfile.print(F(":"));
logDigits(now.second());
logfile.print(F(","));
logfile.print(now.unixtime()); // seconds since 1/1/1970
logfile.print(F(","));
logfile.print(T_htu);
logfile.print(F(","));
logfile.print(RH_htu);
logfile.print(F(","));
logfile.print(T_dew);
logfile.println();
logfile.flush();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
// Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void logDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
// logfile.print(":");
if(digits < 10)
logfile.print('0');
logfile.print(digits);
}
// error subroutine, does this when error occurs
void error(char *str)
{
#if ECHO_TO_SERIAL
Serial.print("error: ");
Serial.println(str);
#endif
// turn on red LED to indicate error
digitalWrite(redLEDpin, HIGH);
while(1); // halt program
}
@ProfJayHam
Copy link
Author

A radiation shield for the HTU21DF is available here
http://www.thingiverse.com/thing:1067700

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment