Code (Integration with SD card data collection)

/*
HTU21D Humidity Sensor

Open serial monitor at 9600 baud to see readings. Errors 998 if not sensor is detected. Error 999 if CRC is bad.

Hardware Connections (Breakoutboard to Arduino):
-VCC = 3.3V
-GND = GND
-SDA = A4
-SCL = A5

*/

#include <Wire.h>
#include <SD.h>
#include “HTU21D.h”

const int chipSelect = 10;
int time = 1;
int outputpin= 0;

HTU21D myHumidity;

void setup()
{
Serial.begin(9600);
Serial.print(“Initializing SD card…”);
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present.”);
// don’t do anything more:
return;
}
Serial.println(“Card initialized.\r\n\r\n”);
Serial.println(“HTU21D Reading START”);

myHumidity.begin();
}

void loop()
{
float humd = myHumidity.readHumidity();
float temp = myHumidity.readTemperature();

//USE ONE OF THE FOLLOWING LINES TO CALIBRATE THE TEMPERATURE
//temp += __
//temp -= __

Serial.print(“Time: “);
Serial.print(millis());
Serial.print(“\r\nTemperature: \r\n Celsius: “);
Serial.print(temp, 1);
Serial.print(“C”);
float fahrenheit = ((temp * 9)/5 +32);
Serial.print(“\r\n Fahrenheit: “);
Serial.print(fahrenheit, 1);
Serial.print(“F\r\n”);
Serial.print(“Humidity: “);
Serial.print(humd, 1);
Serial.print(“%\r\n”);

Serial.println();

File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(time);
dataFile.println(temp);
dataFile.println(fahrenheit);
dataFile.println(humd);
dataFile.close();
}
// if the file isn’t open, pop up an error:
else {
Serial.println(“error opening datalog.txt”);
}

time ++;
delay(1000);
}