A Hello World program showing how to get temperature readings from the MLX90614 infrared temperature sensor.

Dependencies:   mbed

Committer:
jjones646
Date:
Wed May 11 17:48:57 2016 +0000
Revision:
0:964606fa7e7a
Adding example program for the MLX90614 temperature sensor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jjones646 0:964606fa7e7a 1 /*
jjones646 0:964606fa7e7a 2 * Example program showing how to get the temperature readings from
jjones646 0:964606fa7e7a 3 * an MLX90614 infrared temperature sensor using an mbed.
jjones646 0:964606fa7e7a 4 * http://www.haoyuelectronics.com/Attachment/GY-906/MLX90614.pdf
jjones646 0:964606fa7e7a 5 *
jjones646 0:964606fa7e7a 6 * Jonathan Jones
jjones646 0:964606fa7e7a 7 */
jjones646 0:964606fa7e7a 8
jjones646 0:964606fa7e7a 9 #include "mbed.h"
jjones646 0:964606fa7e7a 10 #include "gy-906.h"
jjones646 0:964606fa7e7a 11
jjones646 0:964606fa7e7a 12 using namespace gy906;
jjones646 0:964606fa7e7a 13
jjones646 0:964606fa7e7a 14 I2C i2c(p9, p10);
jjones646 0:964606fa7e7a 15 const uint8_t addr = default_addr;
jjones646 0:964606fa7e7a 16
jjones646 0:964606fa7e7a 17 // kelvin to fahrenheit
jjones646 0:964606fa7e7a 18 float k2f(float raw_kelvin)
jjones646 0:964606fa7e7a 19 {
jjones646 0:964606fa7e7a 20 return (raw_kelvin - 273.15) * 1.8 + 32;
jjones646 0:964606fa7e7a 21 }
jjones646 0:964606fa7e7a 22
jjones646 0:964606fa7e7a 23 // read and return one of the temperature regs
jjones646 0:964606fa7e7a 24 float get_temp(uint8_t reg) {
jjones646 0:964606fa7e7a 25 char cmd[3] = { 0 };
jjones646 0:964606fa7e7a 26 // read the temperature data (kelvin)
jjones646 0:964606fa7e7a 27 cmd[0] = opcode::ram_access | reg;
jjones646 0:964606fa7e7a 28 i2c.write(addr,cmd,1,true); i2c.read(addr,cmd,3);
jjones646 0:964606fa7e7a 29 // convert to meaningful units, still in kelvin - just normalized
jjones646 0:964606fa7e7a 30 return 0.02 * static_cast<float>((cmd[1]<<8)|cmd[0]);
jjones646 0:964606fa7e7a 31 }
jjones646 0:964606fa7e7a 32
jjones646 0:964606fa7e7a 33 int main() {
jjones646 0:964606fa7e7a 34 uint8_t reg_addrs[] = { ram::T_ambient, ram::T_obj1 };
jjones646 0:964606fa7e7a 35 float tt = 0.0;
jjones646 0:964606fa7e7a 36 // clear terminal & hide cursor
jjones646 0:964606fa7e7a 37 printf("\033[r\033[2J\033[?25l"); fflush(stdout);
jjones646 0:964606fa7e7a 38 while (true) {
jjones646 0:964606fa7e7a 39 for (size_t i = 0; i < sizeof(reg_addrs); ++i) {
jjones646 0:964606fa7e7a 40 tt = get_temp(reg_addrs[i]);
jjones646 0:964606fa7e7a 41 printf("\r\033[KT%u:\t%.2f°F\r\n", i, k2f(tt));
jjones646 0:964606fa7e7a 42 fflush(stdout);
jjones646 0:964606fa7e7a 43 }
jjones646 0:964606fa7e7a 44 wait(0.1);
jjones646 0:964606fa7e7a 45 printf("\033[%uA", sizeof(reg_addrs)); fflush(stdout);
jjones646 0:964606fa7e7a 46 }
jjones646 0:964606fa7e7a 47 }