Skip to content

Instantly share code, notes, and snippets.

@jjones646
Last active May 11, 2016 17:19
Show Gist options
  • Save jjones646/3b46e27054bb82d1d8f40b7ca2afc44a to your computer and use it in GitHub Desktop.
Save jjones646/3b46e27054bb82d1d8f40b7ca2afc44a to your computer and use it in GitHub Desktop.
Example program showing how to get the temperature readings from an MLX90614 infrared temperature sensor using an mbed.
#pragma once
namespace gy906 {
//const int default_addr = 0x5a;
const int default_addr = 0x00;
namespace opcode {
const int eeprom_access = 0x20;
const int ram_access = 0x00;
const int read_flags = 0xf0;
const int sleep = 0xff;
const int read_mask = 0x80;
const int write_mask = 0x00;
}
namespace eeprom {
const int T0_max = 0x00;
const int T0_min = 0x01;
const int PWMCTRL = 0x02;
const int Ta_range = 0x03;
const int EC_coef = 0x04;
const int config_reg1 = 0x05;
const int SMBus_addr = 0x0e;
const int id1 = 0x1c;
const int id2 = 0x1d;
const int id3 = 0x1e;
const int id4 = 0x1f;
}
namespace ram {
const int ir1 = 0x04;
const int ir2 = 0x05;
const int T_ambient = 0x06;
const int T_obj1 = 0x07;
const int T_obj2 = 0x08;
}
}
/*
* Example program showing how to get the temperature readings from
* an MLX90614 infrared temperature sensor using an mbed.
* http://www.haoyuelectronics.com/Attachment/GY-906/MLX90614.pdf
*
* Jonathan Jones
*/
#include "mbed.h"
#include "gy-906.h"
using namespace gy906;
I2C i2c(p9, p10);
const uint8_t addr = default_addr;
// kelvin to fahrenheit
float k2f(float raw_kelvin)
{
return (raw_kelvin - 273.15) * 1.8 + 32;
}
// read and return one of the temperature regs
float get_temp(uint8_t reg) {
char cmd[3] = { 0 };
// read the temperature data (kelvin)
cmd[0] = opcode::ram_access | reg;
i2c.write(addr,cmd,1,true); i2c.read(addr,cmd,3);
// convert to meaningful units, still in kelvin - just normalized
return 0.02 * static_cast<float>((cmd[1]<<8)|cmd[0]);
}
int main() {
uint8_t reg_addrs[] = { ram::T_ambient, ram::T_obj1 };
float tt = 0.0;
// clear terminal & hide cursor
printf("\033[r\033[2J\033[?25l"); fflush(stdout);
while (true) {
for (size_t i = 0; i < sizeof(reg_addrs); ++i) {
tt = get_temp(reg_addrs[i]);
printf("\r\033[KT%u:\t%.2f°F\r\n", i, k2f(tt));
fflush(stdout);
}
wait(0.1);
printf("\033[%uA", sizeof(reg_addrs)); fflush(stdout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment