Skip to content

Instantly share code, notes, and snippets.

@krhoyt
Last active August 30, 2016 20:56
Show Gist options
  • Save krhoyt/3f6f56e02996440e6360147e043e9e10 to your computer and use it in GitHub Desktop.
Save krhoyt/3f6f56e02996440e6360147e043e9e10 to your computer and use it in GitHub Desktop.
HIH6130 temperature and humidity from a Tessel 2 (I2C).
// Hardware access
var tessel = require( 'tessel' );
// Sensor values
var humidity = null;
var temperature = null;
// Sensor
// Default address
var hih = tessel.port['B'].I2C( 0x27 );
// Reading
function update() {
hih.transfer( new Buffer( 0x2F ), 4, function( error, data ) {
var raw_humidity = null;
var raw_temperature = null;
if( !error ) {
// First two bytes for humidity
// In the range ( 2^14 - 1 )
raw_humidity = ( ( data[0] & 0x3f ) << 8 ) | data[1];
humidity = raw_humidity / 16382;
// Second two bytes for temperature
// In the range ( 2^14 - 1 )
// Multiply by 165 and subtract 40
// Because that is what the documentation says
raw_temperature = ( ( data[2] << 8 ) | data[3] ) >> 2;
temperature = ( raw_temperature / 16382 ) * 165 - 40;
// Display
console.log( temperature );
console.log( humidity );
} else {
console.log( error );
}
} );
}
// Read values once per second
var interval = setInterval( function() {
update();
}, 1000 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment