Skip to content

Commit

Permalink
Upload MLX90614 Libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
Punuy authored Dec 15, 2022
1 parent b7043da commit d468b51
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
Binary file added Libraries/MLX90614_Demo.zip
Binary file not shown.
103 changes: 103 additions & 0 deletions Libraries/MLX90614_Demo/MLX90614.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//////////////////////////////////////////////////////////////////////////
#include "MLX90614.h"
#include <Wire.h>

// RAM
#define MLX90614_RAWIR1 0x04
#define MLX90614_RAWIR2 0x05
#define MLX90614_TA 0x06 // Ambient Temperature
#define MLX90614_TOBJ1 0x07 // Object Temperature 1
#define MLX90614_TOBJ2 0x08 // Object Temperature 2
// EEPROM
#define MLX90614_TOMAX 0x20 // Object Temperature Max.
#define MLX90614_TOMIN 0x21 // Object Temperature Min.
#define MLX90614_PWMCTRL 0x22
#define MLX90614_TARANGE 0x23
#define MLX90614_EMISS 0x24
#define MLX90614_CONFIG 0x25
#define MLX90614_ADDR 0x2E // I2C Address
#define MLX90614_ID1 0x3C
#define MLX90614_ID2 0x3D
#define MLX90614_ID3 0x3E
#define MLX90614_ID4 0x3F

static uint8_t _crc8( uint8_t *data, uint16_t len );

using namespace esl;

MLX90614::MLX90614( uint8_t slave_addr )
: i2c_addr(slave_addr), crc_error(false)
{
// empty
}

void MLX90614::init( ) {
digitalWrite( SDA, 0 ); // disable pull-up on SDA
digitalWrite( SCL, 0 ); // dsiable pull-up on SCL
}

uint8_t MLX90614::readSlaveAddress() {
uint8_t addr = read_reg( MLX90614_ADDR );
return addr;
}

uint16_t MLX90614::read_reg( uint8_t reg_addr ) {
uint16_t ret;
uint16_t count = 0;
crc_error = false;
Wire.beginTransmission( i2c_addr );
Wire.write( reg_addr );
Wire.endTransmission(false);
Wire.requestFrom( i2c_addr, (uint8_t) 3 );
uint8_t data[6];
while( Wire.available() < 3 ) {
if ( count++ > 100 ) break;
delayMicroseconds(100);
}
data[0] = (i2c_addr << 1);
data[1] = reg_addr;
data[2] = (i2c_addr << 1) | 1;
for (uint8_t i=0; i < 3; i++) {
data[i+3] = Wire.read();
}
Wire.endTransmission();
ret = data[3]; // low byte
ret |= data[4] << 8; // high byte
if ( _crc8( data, 5 ) != data[5] ) {
// Serial.println( "CRC error!");
ret = 0;
crc_error = true;
}
return ret;
}

float MLX90614::readAmbientTemperature() {
//temp = read_reg( MLX90614_TA );
//temp *= 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
//temp -= 273.15; // Kelvin
uint32_t t = read_reg( MLX90614_TA );
return ((t*2) - 27315) /100.0;
}

float MLX90614::readObjectTemperature() {
uint32_t t = read_reg( MLX90614_TOBJ1 );
return ((t*2) - 27315) /100.0;
}

static uint8_t _crc8( uint8_t *data, uint16_t len ) {
uint8_t crc = 0x00;
uint8_t b;
while ( len-- > 0 ) {
b = *data++;
b = b ^ crc;
for (uint8_t i=0; i < 8; i++) {
if (b & 0x80)
b = (b << 1) ^ 0x07;
else
b = (b << 1);
}
crc = b;
}
return crc;
}
//////////////////////////////////////////////////////////////////////////
34 changes: 34 additions & 0 deletions Libraries/MLX90614_Demo/MLX90614.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//////////////////////////////////////////////////////////////////////////
// Author: RSP @ Embedded System Lab (ESL), KMUTNB, Thailand
// File: MAX6675.h
// Last Modified: 2014-06-24
// Note:
// A C++ class for Arduino that provides methods for read ambient
// and object temperature values from the MLX90614 Infrared Thermometer chip.
//////////////////////////////////////////////////////////////////////////
#ifndef __MLX90614_H
#define __MLX90614_H

#include <Arduino.h>
#include <Wire.h>

namespace esl {
class MLX90614 {
public:
MLX90614( uint8_t slave_addr = 0x5A );
void init();
uint8_t readSlaveAddress();
float readAmbientTemperature();
float readObjectTemperature();
uint16_t read_reg( uint8_t reg_addr );
inline boolean is_crc_error() { return crc_error; }

private:
boolean crc_error;
uint8_t i2c_addr;

}; // end class
} // end namespace

#endif // __MLX90614_H
//////////////////////////////////////////////////////////////////////////
40 changes: 40 additions & 0 deletions Libraries/MLX90614_Demo/MLX90614_Demo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//////////////////////////////////////////////////////////////////////////////
/*
* Author: RSP @Embedded System Lab (ESL), KMUTNB, Bangkok/Thailand
* Date: 2015-06-11
*
* GY-906 MLX90614-BAA (B=3V version, A=single zone, A=filter inside)
* -> Connect Vcc=+3.3V, SDA = A4, SCL = A5, and GND to the Arduino Uno
*/
//////////////////////////////////////////////////////////////////////////////

#include <Wire.h>
#include "MLX90614.h"

esl::MLX90614 mlx90614;

void setup() {
Serial.begin(115200);
while (!Serial) ; // for Leonardo or Pro Micro
Wire.begin();
mlx90614.init( );
Serial.print( "Address = 0x" );
Serial.println( mlx90614.readSlaveAddress(), HEX );
}

void loop() {
float f;
Serial.print( "Ambient temp. (Ta): " );
f = mlx90614.readAmbientTemperature(); // ambient temperature
if ( !mlx90614.is_crc_error() ) {
Serial.println(f);
}
Serial.print( "Object1 temp. (To): " );
f = mlx90614.readObjectTemperature(); // object1 temperature
if ( !mlx90614.is_crc_error() ) {
Serial.println(f);
}
delay(1000);
}

//////////////////////////////////////////////////////////////////////////////

0 comments on commit d468b51

Please sign in to comment.