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

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
jjones646
Date:
Wed May 11 17:48:57 2016 +0000
Commit message:
Adding example program for the MLX90614 temperature sensor.

Changed in this revision

gy-906.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 964606fa7e7a gy-906.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gy-906.h	Wed May 11 17:48:57 2016 +0000
@@ -0,0 +1,34 @@
+#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;
+}
+}
\ No newline at end of file
diff -r 000000000000 -r 964606fa7e7a main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 11 17:48:57 2016 +0000
@@ -0,0 +1,47 @@
+/*
+ * 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);
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 964606fa7e7a mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed May 11 17:48:57 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/7c328cabac7e
\ No newline at end of file