Search Code

Using a Non-Contact, Long-Range MLX90614-DCI Temperature Sensor with Arduino

Using a Non-Contact, Long-Range MLX90614-DCI Temperature Sensor with Arduino

In this tutorial, we learn how to use the long-range infrared non-contact MLX90614ESF-DCI temperature sensor with medical accuracy to measure temperature from 20 cm up to 50 cm. This is the medical-accuracy version, which needs special code and power-supply dependency compensation, and we will use 3.3V power from the Arduino and read the voltage and compensate the temperature reading.

342-Resources for this sketch
Language: C++
++
/*************************************************** 
  This is a library example for the MLX90614 Temp Sensor
Original Library and code source: https://github.com/adafruit/Adafruit-MLX90614-Library

This is Arduino code used from the above library for MLX90614 and has been heavily updated.
This code uses the MLX90614-DCI long-range medical-accuracy infrared temperature sensor to measure
the temperature with accuracy and compensation of power supply dependency. 
Please watch the video for details. We will achieve a reading range from 20cm to 49cm where the 
object size is 4.3cm or 43mm.

 * 
 * Watch video instructions for this code: https://youtu.be/jN86PSAHMbw
Updated/written by Ahmad Shamshiri on July 18, 2020
 
 * in Ajax, Ontario, Canada. www.robojax.com
 * 

 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in a structured course with all materials, wiring diagrams, and libraries
all in one place. 

If you found this tutorial helpful, please support me so I can continue creating 
content like this. 

or make a donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

Origin
  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/
const int votageInputPin =A0;
const float arduinoVoltage = 5.0;//Operating voltage of Arduino. either 3.3V or 5.0V 
#include <Wire.h>
#include <Adafruit_MLX90614.h>
char *typeName[]={"Object","Ambient"};

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

float tempObjecC, tempObjecF, tempAmbientC ,tempAmbientF, tempObjecK, tempAmbientK;

void setup() {
  Serial.begin(9600);

  Serial.println("Robojax MLX90614 test");  

  mlx.begin();  
}

void loop() {
  //Robojax Example for MLX90614-DCI medical accuracy long range version
  printTemp('C');
//  printTemp('D');
//  
//  printTemp('F');  
//  printTemp('G'); 
 // if( getTemp('C')>40)
 // {
    //do something here
 // }
  
//  printTemp('K');   
//  printTemp('L');  
 // Serial.println("======");
  //printVoltage();// this line will read the input voltage and dispalys it on serial monitor
  delay(1000);
  //Robojax Example for MLX90614-DCI medical accuracy long range version
}

/*
 * @brief returns temperature or relative humidity
 * @param "type" is a character
 *     C = Object Celsius
 *     D = Ambient Celsius
 *     
 *     K = Object Kelvin
 *     L = Ambient in Kelvin
 *     
 *     F = Object Fahrenheit
 *     G = Ambient in Fahrenheit

 * @return returns one of the values above
 * Usage: to get Fahrenheit type: getTemp('F')
 * to print it on serial monitor Serial.println(getTemp('F'));
 * Written by Ahmad Shamshiri on Mar 30, 2020. 
 * in Ajax, Ontario, Canada
 * www.Robojax.com 
 */
float getTemp(char type)
{
   //Robojax Example for MLX90614-DCI medical accuracy long range version
  float value;
   tempObjecC = mlx.readObjectTempC();//in C object
   tempAmbientC = mlx.readAmbientTempC();

   tempObjecF = mlx.readObjectTempF(); //Fah. Object
   tempAmbientF = mlx.readAmbientTempF();//Fah Ambient

    dependencyComp();
      
   if(type =='F')
   {
    value = tempObjecF;
   }else if(type =='G')
   {
    value = tempAmbientF;
   }else if(type =='K')
   {
    value = tempObjecC + 273.15;// Object Kelvin
   }else if(type =='L')
   {
    value = tempAmbientC + 273.15;//Ambient Kelvin
   }else if(type =='C')
   {
    value = tempObjecC;
   }else if(type =='D')
   {
    value = tempAmbientC;
   }
   return value;
    //Robojax Example for MLX90614-DCI medical accuracy long range version
}//getTemp


/*
 * fixes the voltage dependency of measurement
 * This function updates the reading of temperature by reading 
 * the power supply voltage and compensating it.
 * see page 37 of datasheet here: https://robojax.comrobojax_MLX90614_Datasheet-Melexis.pdf
 * T=T0 - (VCC-3) *0.6
 */
void dependencyComp()
{
  //Robojax Example for MLX90614-DCI medical accuracy long range version
  int value = analogRead(votageInputPin);// read the input
  float voltage =  value * (arduinoVoltage / 1023.0);//get the voltage from the value above
  //Serial.println(voltage);
  tempObjecC = tempObjecC - (voltage -3) * 0.6;
  tempAmbientC = tempAmbientC - (voltage -3) * 0.6;  

  tempObjecF = tempObjecF - (voltage -3) * 0.6;
  tempAmbientF = tempAmbientF - (voltage -3) * 0.6;    

  tempObjecK = tempObjecK - (voltage -3) * 0.6;
  tempAmbientK = tempAmbientK - (voltage -3) * 0.6;     
  //Robojax Example for MLX90614-DCI medical accuracy long range version
}//dependencyComp() ends here


/*
 * @brief nothing
 * @param "type" is a character
 *     C = Object Celsius
 *     D = Ambient Celsius
 *     
 *     K = Object Kelvin
 *     L = Ambient in Kelvin
 *     
 *     F = Object Fahrenheit
 *     G = Ambient in Fahrenheit

 * @return prints temperature value in serial monitor
 * Usage: to get Fahrenheit type: getTemp('F')
 * to print it on serial monitor Serial.println(getTemp('F'));
 * Written by Ahmad Shamshiri on Mar 30, 2020 at 21:51
 * in Ajax, Ontario, Canada
 * www.Robojax.com 
 */
void printTemp(char type)
{
  //Robojax Example for MLX90614-DCI medical accuracy long range version
  float tmp =getTemp(type);

  if(type =='C')
  {
    Serial.print(typeName[0]);
    Serial.print(" ");    
    Serial.print(tmp);
    printDegree();     
    Serial.println("C");
  }else if(type =='D')
  {
    Serial.print(typeName[1]);
    Serial.print(" ");     
    Serial.print(tmp);
    printDegree();     
    Serial.println("C");
  }else if(type =='F')
  {
    Serial.print(typeName[0]);
    Serial.print(" ");     
    Serial.print(tmp);
    printDegree();     
    Serial.println("F");
  }else if(type =='G')
  {
    Serial.print(typeName[1]);
    Serial.print(" ");     
    Serial.print(tmp);
    printDegree();      
    Serial.println("F");
  }

  else if(type =='K')
  {
    Serial.print(typeName[0]);
    Serial.print(" ");     
    Serial.print(tmp);  
    printDegree();      
    Serial.println(" K");
  }  
  else if(type =='L')
  {
    Serial.print(typeName[1]);
    Serial.print(" ");     
    Serial.print(tmp);  
    printDegree();      
    Serial.println(" K");
  }

//Robojax Example for MLX90614-DCI medical accuracy long range version
}//printTemp(char type)

/*
 * printDegree()
 * prints degree symbol on serial monitor
 * written by Ahmad Shamshiri for Robojax.com 
 * 
 */
void printDegree()
{
     Serial.print("\\xC2\\xB0");     
}

/*
 *  printVoltage()
 *  prints voltage at pin "voltageInputPin"
 *  on serial monitor.
 */
void printVoltage()
{
  //Robojax Example for MLX90614-DCI medical accuracy long range version
  int value = analogRead(votageInputPin);// read the input
  float voltage =  value * (arduinoVoltage / 1023.0);//get the voltage from the value above
  Serial.print("Voltage: ");
  Serial.print(voltage);  
  Serial.println("V");
  //Robojax Example for MLX90614-DCI medical accuracy long range version
}// printVoltage()

Resources & references

Files📁

No files available.