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

I2C program to read MLX90614ESF-BAA Infrared Thermometer by Bill_Wyo

Nov. 23, 2013   |   Snippet   |   Licensed as Apache 2.0   |   1811 views

This code utilizes the hardware I2C interface. I used it on a Cerbuino connected to pins 8 (SDA) and 9 (SCL) on socket 2.

The wiring diagram is available in the product documentation. I found that in order to have more than 2 feet of wiring between the Cerbuino and the thermometer I had to have 4 wire shielded cable. I am reading it with no errors at about 15 feet.

Using a for next loop I found the address for the device. The address is in the configuration (con) statement. Looping up to 127 revealed that addresses 0 and 90 worked. 90 is the correct address to use for temperature reads. 0 is the address to use to write to the device.

The code uses registers 6 and 7 to get the object and ambient temperatures.

The emissivity of the surface you read will affect the accuracy of the temperature measurement. Bare aluminum for example will give a different reading than wood even though they are at the same temperature. The emissivity of the surface you are trying to read can be written into the device. I do not have that implemented but the registers are in the documentation.

Comments or questions?   Discuss on the forum.



Author Version Date
Bill_Wyo 1 11/23 '13 at 03:23pm
1 — Source
  1. using System;
  2. using System.Collections;
  3. using System.Threading;
  4. using Microsoft.SPOT;
  5. using Microsoft.SPOT.Hardware;
  6. using Microsoft.SPOT.Presentation;
  7. using Microsoft.SPOT.Presentation.Controls;
  8. using Microsoft.SPOT.Presentation.Media;
  9. using Microsoft.SPOT.Presentation.Shapes;
  10. using Microsoft.SPOT.Touch;
  11. using System.Text;
  12.  
  13. using Gadgeteer.Networking;
  14. using GT = Gadgeteer;
  15. using GTM = Gadgeteer.Modules;
  16.  
  17. namespace IR_Thermometer_I2C
  18. {
  19. public partial class Program
  20. {
  21. static I2CDevice.Configuration con = new I2CDevice.Configuration(90, 100);
  22. static I2CDevice tempI2C = new I2CDevice(con);
  23.  
  24. void ProgramStarted()
  25. {
  26. Debug.Print("Program Started");
  27. GT.Timer timer = new GT.Timer(1000);
  28. timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  29. timer.Start();
  30. }
  31.  
  32. void timer_Tick(GT.Timer timer)
  33. {
  34. TempMeas();
  35. }
  36.  
  37. void TempMeas()
  38. {
  39. // Create transactions
  40. // First transaction is writing the "read command"
  41. // Second transaction is reading the data
  42. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
  43.  
  44. // create write buffer (we need one byte) value of 7 indicate we will read Register 7 for object temperature
  45. byte[] RegisterNum = new byte[1] { 7 };
  46. xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);
  47. // create read buffer to read the register - could read 3 bytes with the third byte containing error information
  48. byte[] RegisterValue = new byte[2];
  49. xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue);
  50.  
  51. // Now we access the I2C bus using a timeout of one second
  52. // if the execute command returns zero, the transaction failed (this
  53. // is a good check to make sure that you are communicating with the device correctly
  54. // and don’t have a wiring issue or other problem with the I2C device)
  55. if (tempI2C.Execute(xActions, 1000) == 0)
  56. {
  57. Debug.Print("Failed to perform I2C transaction");
  58. }
  59. else
  60. {
  61. double objtemp = ((((RegisterValue[1] & 0x007f) << 8) + RegisterValue[0]) * 0.02) - 0.01;
  62. Debug.Print("Object temperature = " + objtemp.ToString("F2") + " K");
  63. }
  64. // create write buffer (we need one byte) value of 6 indicate we will read Register 6 for ambient temperature
  65. RegisterNum = new byte[1] { 6 };
  66. xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);
  67. // create read buffer to read the register
  68. RegisterValue = new byte[2];
  69. xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue);
  70. if (tempI2C.Execute(xActions,1000) ==0)
  71. {
  72. Debug.Print("Failed to perform I2C2 transaction");
  73. }
  74. else
  75. {
  76. double ambtemp = ((((RegisterValue[1] & 0x007f) << 8) + RegisterValue[0]) * 0.02) - 0.01;
  77. Debug.Print("Ambient temperature = " + ambtemp.ToString("F2") + " K");
  78. }
  79. }
  80. }
  81. }