Logging temperature using an Arduino and AD7410 sensor

This article is a quick and dirty demonstration to hack yourself a temperature data logger which saves its results to an SD card. The sensor of use is a HTU21D, a I²C temperature and humidity sensor.

hookup

Schematic:

hookup_bb

I hooked up a HTU21D sensor to an Arduino Uno + SD-card shield and let it monitor temperature (and humidity) over an entire day:

24h_log

Here is a close-up of what happens at night:

3h_log

Source code:

/*
 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 4

created 24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe

This example code is in the public domain.

*/
 /*
 HTU21D Humidity Sensor Example Code
 By: Nathan Seidle
 SparkFun Electronics
 Date: September 15th, 2013
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

 Uses the HTU21D library to display the current humidity and temperature

 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 (use inline 330 ohm resistor if your board is 5V)
 -SCL = A5 (use inline 330 ohm resistor if your board is 5V)

*/

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "SparkFunHTU21D.h"

//Create an instance of the object
HTU21D myHumidity;

const int chipSelect = 4;

// make a string for assembling the data to log:
String dataString = "";

void setup() {
 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 while (!Serial) {
 ; // wait for serial port to connect. Needed for native USB port only
 }

 myHumidity.begin();
 Serial.println("HTU21D ready!");

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.");
}

void loop() {
 readTemp();

Serial.println("Writing data...");
 // open the file. note that only one file can be open at a time,
 // so you have to close this one before opening another.
 File dataFile = SD.open("datalog.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 datalog.txt");
 }
 delay(10000);
}

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

 // make a string for assembling the data to log:
 dataString = "Time: ";
 dataString += String(millis());
 dataString += String("; Temperature: ");
 dataString += String(temp);
 dataString += String("; Humidity: ");
 dataString += String(humd);
 dataString += String("%");
}

This was just a quick and dirty hack to get some instant results. I’m planing to do some more logging in the future to get a better understanding of how to approach my self build climate control. Stay tuned for more.

Arduino and AD7410 I²C temp sensor

On request, the source code of using the Analog Devices AD7410 I²C temp sensor. (Datasheet: adt7410)

#include <Wire.h>

//ADT7410 13/16-bit digital temperature sensor
//RED (VDD): 2.7 ... 5.5V
//BROWN (GND): 0V
//Arduino uno, wires:
//PURPLE 1 (SCL): SCL (near AREF, should be equal to ANALOG5)
//PURPLE 2 (SDA): SDA (near AREF, should be equal to ANALOG4)
//Arduino due, wires:
//PURPLE 1 (SCL): SCL (pin 21)
//PURPLE 2 (SDA): SDA = (pin 20)
//Arduino IDE 1.5 compatible

#define ADT7410_I2C_ADDRESS 0x48
#define tempRegister 0x00
#define configRegister 0x03
#define selectCode16bitMode 0x80


void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600); // serial connection
  set16bitMode();
}




void loop() {
  delay(5000);
  serieelPrint(); //print on serial port
}


//set ADT7410 in 16-bit temp value mode
void set16bitMode() 
{
  Serial.println("Setting 16-bit mode...");
  Wire.beginTransmission(ADT7410_I2C_ADDRESS);
  Wire.write(configRegister);
  Wire.write(selectCode16bitMode);
}



//gives back the temperature value
float readTemp()
{
  //set read register
  Wire.beginTransmission(ADT7410_I2C_ADDRESS);
  Wire.write(tempRegister);
  Wire.endTransmission();
  //receive data
  Wire.requestFrom(ADT7410_I2C_ADDRESS, 2);
  byte MSB = Wire.read();
  byte LSB = Wire.read();
  
  //check for positive or negative sign
  boolean sign;
  if(MSB>0xA0) {
    sign=0; //negative
  } else {
    sign=1; //positive
  }
  
  //concat MSB&LSB
  float tempValue = MSB * 256;
  tempValue+=LSB;
  
  //BIN to DEC
  if(!(sign)){
    tempValue-=65536;
  }
  tempValue/=128;
  
  return tempValue;
}

void serieelPrint()
{
  Serial.print("Unit: ");
  Serial.print(ADT7410_I2C_ADDRESS, HEX);
  Serial.print(" *** temperature: ");
  //temperature readout
  Serial.print(readTemp());
  Serial.println(" degrees Celcius");
}

UPDATE: you can find my Arduino library here: https://github.com/geoffrey-vl/Easy_ADT7410_Arduino_Library