Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 127 additions & 6 deletions DFRobot_MLX90614.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ void DFRobot_MLX90614::setEmissivityCorrectionCoefficient(float calibrationValue
uint16_t emissivity = round(65535 * calibrationValue);
DBG(emissivity, HEX);

uint16_t emissivity_reg = getEmissivityReg();
if (emissivity == emissivity_reg) {
DBG("Same emissivity to set");
return;
}

uint8_t buf[2] = { 0 }; // Avoid endianness
uint16_t curE = 0;
uint16_t data = 0;
Expand Down Expand Up @@ -92,23 +98,122 @@ void DFRobot_MLX90614::setEmissivityCorrectionCoefficient(float calibrationValue
}
}

float DFRobot_MLX90614::getEmissivityCorrectionCoefficient(void)
{
return ((float)getEmissivityReg())/65535.0;
}

uint16_t DFRobot_MLX90614::getEmissivityReg(void)
{
uint16_t emissivity = 0;
uint8_t buf[2] = { 0 };
readReg(MLX90614_EMISSIVITY, buf);
emissivity = ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8));
DBG(emissivity, HEX);
delay(10);
return emissivity;
}

void DFRobot_MLX90614::setMeasuredParameters(eIIRMode_t IIRMode, eFIRMode_t FIRMode)
{
uint8_t buf[2] = { 0 };
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);

buf[0] &= 0xF8;
buf[1] &= 0xF8;
writeReg(MLX90614_CONFIG_REG1, buf);
if ( (buf[0]&0x07)==IIRMode && (buf[1]&0x07)==FIRMode ) {
DBG("Same filters to set; abort writing");
return;
}

//First write 0 in the EEPROM; see 8.3.3.1 in datasheet pg 17
uint8_t wbuf[2] = {0}; //write 16bits buffer with 0
writeReg(MLX90614_CONFIG_REG1, wbuf);
delay(10);

buf[0] |= IIRMode;
buf[1] |= FIRMode;
buf[0] = (buf[0] & 0xF8) | IIRMode;
buf[1] = (buf[1] & 0xF8) | FIRMode;
writeReg(MLX90614_CONFIG_REG1, buf);
delay(10);
}

uint16_t DFRobot_MLX90614::getConfigRegister1(void)
{
uint8_t buf[2] = {0};
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
return ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8));
}

uint8_t DFRobot_MLX90614::getFIRBits(void)
{
uint8_t buf[2] = {0};
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
return (buf[1]&0x07);
}

uint16_t DFRobot_MLX90614::getFIRLength(void)
{
uint16_t FIRLengths[]={8,16,32,64,128,256,512,1024};
return FIRLengths[getFIRBits()];
}

uint8_t DFRobot_MLX90614::getIIRBits(void) {
uint8_t buf[2] = {0};
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
return (buf[0]&0x07);
}

uint8_t DFRobot_MLX90614::getIIRSpikeLimit(void)
{
uint8_t SpikeLimits[]={50,25,17,13,100,80,67,57};
return SpikeLimits[getIIRBits()];
}

uint8_t DFRobot_MLX90614::getGainBits(void)
{
uint8_t buf[2] = {0};
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
return ((buf[1]>>3)&0x07);
}

uint8_t DFRobot_MLX90614::getGainValue(void)
{
uint8_t vGAIN[]={1,3,9,12,25,50,100,100};
return vGAIN[getGainBits()];
}

void DFRobot_MLX90614::setGainBits(eGAINMode_t GAINMode)
{
uint8_t gainbits = (uint8_t)GAINMode;
uint8_t buf[2] = {0};
DBG(gainbits);
readReg(MLX90614_CONFIG_REG1, buf);
delay(10);
if (gainbits != ((buf[1]>>3)&0x07)) {
uint8_t wbuf[2] = {0};
writeReg(MLX90614_CONFIG_REG1, wbuf);
delay(10);
buf[1] = (buf[1] & 0xC7) | (gainbits<<3);
DBG((uint16_t)buf[0] | (uint16_t)(buf[1] << 8),HEX)
writeReg(MLX90614_CONFIG_REG1, buf);
delay(10);
}
}

uint8_t DFRobot_MLX90614::setGainValue(uint8_t gainvalue) {
uint8_t vGAIN[]={1,3,9,12,25,50,100,100};
uint8_t gainbits=0;
while ((gainbits<8) && (gainvalue>=vGAIN[gainbits])) gainbits++;
if (gainbits) {
setGainBits((eGAINMode_t)(--gainbits));
return vGAIN[gainbits];
}
return 0;
}

float DFRobot_MLX90614::getAmbientTempCelsius(void)
{
uint8_t buf[2];
Expand All @@ -118,6 +223,14 @@ float DFRobot_MLX90614::getAmbientTempCelsius(void)
return temp; // Get celsius temperature of the ambient
}

uint16_t DFRobot_MLX90614::getAmbientTemp(void)
{
uint8_t buf[3];
readReg(MLX90614_TA, buf);
uint16_t temp = ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8));
return temp; // Get raw temperature of the ambient
}

float DFRobot_MLX90614::getObjectTempCelsius(void)
{
uint8_t buf[2];
Expand All @@ -128,13 +241,21 @@ float DFRobot_MLX90614::getObjectTempCelsius(void)
return temp; // Get celsius temperature of the object
}

uint16_t DFRobot_MLX90614::getObjectTemp(void)
{
uint8_t buf[3];
readReg(MLX90614_TOBJ1, buf);
// DBG((buf[0] | buf[1] << 8), HEX);
uint16_t temp = ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8));
return temp; // Get raw temperature of the object
}

float DFRobot_MLX90614::getObject2TempCelsius(void)
{
uint8_t buf[2];
readReg(MLX90614_TOBJ2, buf);
// DBG((buf[0] | buf[1] << 8), HEX);
float temp = ((uint16_t)buf[0] | (uint16_t)(buf[1] << 8)) * 0.02 - 273.15;

return temp; // Get celsius temperature of the object
}

Expand Down
115 changes: 113 additions & 2 deletions DFRobot_MLX90614.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @license The MIT License (MIT)
* @author [qsjhyy](yihuan.huang@dfrobot.com)
* @version V1.0
* @date 2021-07-29
* @date 2024-02-07
* @url https://github.com/DFRobot/DFRobot_MLX90614
*/
#ifndef __DFROBOT_MLX90614_H__
Expand All @@ -16,7 +16,7 @@
#include <Arduino.h>
#include <Wire.h>

#define ENABLE_DBG //!< open this macro and you can see the details of the program
//#define ENABLE_DBG //!< open this macro and you can see the details of the program
#ifdef ENABLE_DBG
#define DBG(...) {Serial.print("[");Serial.print(__FUNCTION__); Serial.print("(): "); Serial.print(__LINE__); Serial.print(" ] "); Serial.println(__VA_ARGS__);}
#else
Expand Down Expand Up @@ -86,6 +86,22 @@ class DFRobot_MLX90614
eFIR1024,
}eFIRMode_t;

/**
* @enum eGAINMode_t
* @brief GAIN mode
*/
typedef enum
{
eGAIN1 = 0,
eGAIN3,
eGAIN6,
eGAIN12,
eGAIN25,
eGAIN50,
eGAIN100,
eGAIN100b
}eGAINMode_t;

public:
/**
* @fn DFRobot_MLX90614
Expand Down Expand Up @@ -114,6 +130,22 @@ class DFRobot_MLX90614
*/
void setEmissivityCorrectionCoefficient(float calibrationValue, bool set0X0F = false);

/**
* @fn getEmissivityCorrectionCoefficient
* @brief get the emissivity calibration coefficient, users need to calculate the ratio of the temperature measured before the sensor changes emissivity to the true temperature of the object,
* @n upload the ratio to the api as a parameter, and the deviation of the object absolute temperature measured by the sensor will be lower
* @return calibrationValue new calibration coefficient, the ratio of the temperature measured before the sensor changes emissivity to the true temperature of the object, range: [0.1, 1.0]
*/
float getEmissivityCorrectionCoefficient(void);

/**
* @fn getEmissivityReg
* @brief get the emissivity calibration coefficient, users need to calculate the ratio of the temperature measured before the sensor changes emissivity to the true temperature of the object,
* @n upload the ratio to the api as a parameter, and the deviation of the object absolute temperature measured by the sensor will be lower
* @return calibrationValue new calibration coefficient RAW 16bits, the ratio of the temperature measured before the sensor changes emissivity to the true temperature of the object, range: [6553, 65535]
*/
uint16_t getEmissivityReg(void);

/**
* @fn setMeasuredParameters
* @brief set the measurement parameters, including IIR (Infinite Impulse Response Digital Filter) and FIR (Finite Impulse Response Digital Filter)
Expand All @@ -123,13 +155,85 @@ class DFRobot_MLX90614
*/
void setMeasuredParameters(eIIRMode_t IIRMode=eIIR100, eFIRMode_t FIRMode=eFIR1024);

/**
* @fn getConfigRegister1
* @brief get the ConfigRegister1 value including measurement parameters i.e. IIR (Infinite Impulse Response Digital Filter), FIR (Finite Impulse Response Digital Filter) and the gain of the amplifier (see datasheet page 15)
* @return ConfigRegister1 value
*/
uint16_t getConfigRegister1(void);

/**
* @fn getFIRBits
* @brief get the FIR bits of the sensor from the ConfigRegister1 (see datasheet page 15)
* @return FIR bits of the sensor (see datasheet page 15)
*/
uint8_t getFIRBits(void);

/**
* @fn getFIRLength
* @brief get the FIR length of the sensor (see datasheet page 15)
* @return FIR length of the sensor (see datasheet page 15)
*/
uint16_t getFIRLength(void);

/**
* @fn getIIRBits
* @brief get FIR bits of the sensor from the ConfigRegister1 (see datasheet page 15)
* @return IIR bits of the sensor (see datasheet page 15)
*/
uint8_t getIIRBits(void);

/**
* @fn getIRRSpikeLimit
* @brief get the IIR spike limit of the sensor in purcentage (see datasheet page 15)
* @return IIR spike limit of the sensor in purcentage (see datasheet page 15)
*/
uint8_t getIIRSpikeLimit(void);

/**
* @fn getGainBits
* @brief get the gain bits of the sensor from the ConfigRegister1 (see datasheet page 15)
* @return Gain bits of the sensor (see datasheet page 15)
*/
uint8_t getGainBits(void);

/**
* @fn getGainValue
* @brief get the gain of the amplifier i.e. 1,3,9,12(.5),25,50 or 100
* @return Gain of the amplifier
*/
uint8_t getGainValue(void);

/**
* @fn setGainBits
* @brief set the gain bits of the sensor from the ConfigRegister1 (see datasheet page 15)
* @param GAINMode: eGAIN1, eGAIN3, eGAIN6, eGAIN12, eGAIN25, eGAIN50, eGAIN100
* @return None
*/
void setGainBits(eGAINMode_t GAINMode=eGAIN100);

/**
* @fn setGainValue
* @brief set the gain of the amplifier to a fixed value if possible or just below this value.
* @param the gain value
* @return the gain of the amplifier that was set or 0 if not set
*/
uint8_t setGainValue(uint8_t gainvalue=0);

/**
* @fn getAmbientTempCelsius
* @brief get ambient temperature, unit is Celsius
* @return return value range: -40.01 °C ~ 85 °C
*/
float getAmbientTempCelsius(void);

/**
* @fn getAmbientTemp
* @brief get raw ambient temperature from sensor
* @return return temperature in 16bits format
*/
uint16_t getAmbientTemp(void);

/**
* @fn getObjectTempCelsius
* @brief get temperature of object 1, unit is Celsius
Expand All @@ -139,6 +243,13 @@ class DFRobot_MLX90614
*/
float getObjectTempCelsius(void);

/**
* @fn getObjectTemp
* @brief get raw temperature of object 1
* @return return temperature in 16bits format
*/
uint16_t getObjectTemp(void);

protected:
/***************** register read/write ports ******************************/

Expand Down
Loading