Simple temperature and humidity program example for Hexiwear featuring UART

Fork of HTU21D by Alex Lipford

This project demonstrates the use of the HTU21D temperature and humidity sensor embedded in Hexiwear

Open a Hyperterminal tool on your computer and connect it to the "mbed Serial port (COMxx)" with Baud rate "9600bps"

Compile the project and copy the binary "Hexi_Humid_Temp_Example_HEXIWEAR.bin" in the DAP-LINK drive from your computer file explorer Press the K64F-RESET button on the docking station to start the program on your board

Every 500ms the value of the Temperature and Humidity will be displayed in the Hyperterminal window and the LED will blink Green

HTU21D/HTU21D.cpp

Committer:
GregC
Date:
2016-10-17
Revision:
3:caccf1879f14
Parent:
2:4fd07be6bad8

File content as of revision 3:caccf1879f14:

// @author Alex Lipford
// Georgia Institute of Technology 
// ECE 4180 Embeded Systems Design
// Professor Hamblen
// 10/19/2014
 
// @section LICENSE
// ----------------------------------------------------------------------------
// "THE BEER-WARE LICENSE" (Revision 42):
// <alexlipford@gmail.com> wrote this file. As long as you retain this notice you
// can do whatever you want with this stuff. If we meet some day, and you think
// this stuff is worth it, you can buy me a beer in return.
// ----------------------------------------------------------------------------

// @section DESCRIPTION
// HTU21D Humidity and Temperature sensor.
// Datasheet, specs, and information:
// https://www.sparkfun.com/products/12064


#include "HTU21D.h"

HTU21D::HTU21D(PinName sda, PinName scl) {

    i2c_ = new I2C(sda, scl);
    //400KHz, as specified by the datasheet.
    i2c_->frequency(400000);
}

int HTU21D::sample_ctemp(void) {

    char tx[1];
    char rx[2];

    tx[0] = TRIGGER_TEMP_MEASURE; // Triggers a temperature measure by feeding correct opcode.
    i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1);
    wait_ms(50); // Per datasheet, wait long enough for device to sample temperature
    
    // Reads triggered measure
    i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2);
    wait_ms(1);
    
    // Algorithm from datasheet to compute temperature.
    unsigned int rawTemperature = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1];
    rawTemperature &= 0xFFFC;

    float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536
    float realTemperature = -46.85 + (175.72 * tempTemperature); //From page 14

    return (int)realTemperature;
}

int HTU21D::sample_ftemp(void){
    int temptemp = sample_ctemp();
    int ftemp = temptemp * 1.8 + 32;
    
    return ftemp;
}

int HTU21D::sample_ktemp(void){
    int temptemp = sample_ctemp();
    int ktemp = temptemp + 274;
    
    return ktemp;
}

int HTU21D::sample_humid(void) {

    char tx[1];
    char rx[2];


    tx[0] = TRIGGER_HUMD_MEASURE; // Triggers a humidity measure by feeding correct opcode.
    i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1);
    wait_ms(16); // Per datasheet, wait long enough for device to sample humidity
    
    // Reads triggered measure
    i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2);
    wait_ms(1);
    
    //Algorithm from datasheet.
    unsigned int rawHumidity = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1];

    rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place
    
    //Given the raw humidity data, calculate the actual relative humidity
    float tempRH = rawHumidity / (float)65536; //2^16 = 65536
    float rh = -6 + (125 * tempRH); //From page 14

    return (int)rh;
}