This entry's latest version is outdated and must be revised. Please see the documentation for the latest API.

Melexis MLX90614 IR Temperature Sensor Driver (I2C) by rzander

Oct. 25, 2014   |   Snippet   |   Licensed as LGPL 3.0   |   1842 views

The MLX90614 (http://www.melexis.com/Infrared-Thermometer-Sensors/Infrared-Thermometer-Sensors/MLX90614-615.aspx) Sensor is an IR Temeprature sensor with I2C Interface.

The class contains methods to get the Air and the IR temperature...

Comments or questions?   Discuss on the forum.



Author Version Date
rzander 1 10/25 '14 at 04:43pm
1 — Source
  1. using System;
  2. using Microsoft.SPOT;
  3. using Microsoft.SPOT.Hardware;
  4.  
  5. namespace GadgeteerApp1
  6. {
  7. class MLX90614
  8. {
  9. private static I2CDevice.Configuration con = new I2CDevice.Configuration(0x5A, 50);
  10. private bool bCelcius = true;
  11. private I2CDevice MyI2C;
  12.  
  13. /// <summary>
  14. /// MLX90614 IR Temperature sensor
  15. /// </summary>
  16. /// <param name="Celsius">True = Celsius ; False = Fahrenheit</param>
  17. public MLX90614(bool Celsius = true)
  18. {
  19. bCelcius = Celsius;
  20. MyI2C = new I2CDevice(con);
  21. }
  22.  
  23. /// <summary>
  24. /// Get the Air Temperature
  25. /// </summary>
  26. public double AirTemp
  27. {
  28. get
  29. {
  30. return GetTemp(0x06);
  31. }
  32. }
  33.  
  34. /// <summary>
  35. /// Get the Temperature from the IR Sensor
  36. /// </summary>
  37. public double IRTemp
  38. {
  39. get
  40. {
  41. return GetTemp(0x07);
  42. }
  43. }
  44.  
  45. /// <summary>
  46. /// Connect the Sensor over I2c and read the specified Register
  47. /// </summary>
  48. /// <param name="Register">0x06 = AirTemp; 0x07 IR temp</param>
  49. /// <returns>Temperature</returns>
  50. private double GetTemp(byte Register)
  51. {
  52. var buffer = new byte[2];
  53. var wbuffer = new byte[] { Register };
  54.  
  55. I2CDevice.I2CTransaction[] xacts = new I2CDevice.I2CTransaction[] {
  56. I2CDevice.CreateWriteTransaction(wbuffer),
  57. I2CDevice.CreateReadTransaction(buffer)
  58. };
  59.  
  60. int byteCount = MyI2C.Execute(xacts, 100);
  61. if (byteCount < (wbuffer.Length + buffer.Length))
  62. throw new System.IO.IOException();
  63.  
  64. //from peekay123 https://community.spark.io/t/reading-i2c-ir-temperature-sensor/4529/2
  65. double tempData = (double)(((buffer[1] & 0x007F) << 8) + buffer[0]);
  66. tempData = (tempData * 0.02) - 0.01;
  67.  
  68. if (bCelcius)
  69. return tempData - 273.15;
  70. else
  71. return ((tempData - 273.15) * 1.8) + 32;
  72. }
  73. }
  74. }