avatar

Pravar93

Member Since: January 27, 2015

Country: United States

  • We've struggled quite a bit to establish communication between an Arduino UNO and MLX90614AAA using the Wire Library for I2C. We finally got it running though. Here's the sketch!

    /*******************************************
    Title: MLX90614 driver for Arduino
    Author: Varun Nalam & Pravar Jain
    *******************************************/
    
    #include <Wire.h>  //Including Wire library to communicate using I2C protocol//
    
    int device=0x5A;  //MLX90614 device address//
    byte buffer[3];  //3 bytes buffer for data read from the device (data_high, data_low and pec)//
    float temp;  //Variable storing the temperature data acquired//
    
    //---------------- Functions ----------------//
    
    //Read data function//
    void readFrom(int device, byte address, int num)
     {
       Wire.beginTransmission(device);  //Start transmission to device (Initiating again)//
       Wire.write(address);  //RAM address for ambient or object temperature//
       Wire.endTransmission(false);  //To make sure bus is not released, restart message given out//
       Wire.requestFrom(device, num);  //Request 3 bytes of data from device//
    
       int i = 0;
       while(Wire.available())  //Device may send less than requested (abnormal)//
       { 
          buffer[i] = Wire.read();  //Receive a byte//
           i++;
        }
        Wire.endTransmission();  //End transmission and release bus//
      }
    
    //Write data function//
    void writeTo(int device, byte address)
    {
      Wire.beginTransmission(device);  //Start transmission to device//
      Wire.write(address);  //Send register address//
      Wire.endTransmission();  //End transmission//
     }
    
    //---------------------------------------------//
    
    void setup()
    {
      Serial.begin(57600);  //Serial for output//
      Wire.begin();  //Joining i2c bus//
    }
    //End of setup//
    
    void loop() 
    {
      int address=0x07;  //Choosing RAM address for ambient or object temperature//
      readFrom(device,address,3);  //Read the raw temperature data and PEC from the MLX90614//
      temp=(buffer[1]<<8)|(buffer[0]);  //Adjusting the two bytes of temperature data//
      temp = (temp * 0.02)-0.01;
      temp = temp - 273.15;
    
      Serial.print("Celcius: ");
      Serial.println(temp);
    
      delay(100);
    }
    

No public wish lists :(