شِفر (کود) جستجو

Lesson 38: Using an MLX90614 Infrared Temperature Sensor with Arduino

Lesson 38: Using an MLX90614 Infrared Temperature Sensor with Arduino

This project demonstrates how to use the MLX90614 infrared temperature sensor with an Arduino. This sensor allows you to measure temperature without contact, making it suitable for various applications. The provided code simplifies the process of reading temperatures in Celsius, Fahrenheit, and Kelvin, and offers flexible functions for incorporating the sensor into your projects.

Possible applications and project ideas using this sensor include:

  • Contactless temperature measurement for people or objects.
  • Monitoring temperature in sensitive environments.
  • Building a thermal imaging camera (with additional hardware).
  • Creating a temperature-controlled fan or other appliance.
  • Detecting the presence of people or animals based on heat signatures.

Hardware/Components

  • Arduino Uno (or compatible board)
  • MLX90614 Infrared Temperature Sensor
  • Jumper wires
  • USB cable

Wiring Guide

The MLX90614 sensor communicates with the Arduino using the I2C protocol. (in video at 07:00)

%%WIRING%%

Code Explanation

The provided Arduino code utilizes the Adafruit MLX90614 library. (in video at 08:04) This library simplifies interaction with the sensor. The code includes two key functions, printTemp() and getTemp(), designed for easy temperature retrieval and display.

Key configurable elements within the code:


char *typeName[]={"Object","Ambient"};

This array defines the labels used when printing temperatures. You can customize these labels as needed.


void printTemp(char type)

This function prints the temperature reading to the Serial Monitor. The type parameter specifies the temperature unit and source (object or ambient). The following characters are used for different readings: (in video at 10:28)

  • 'C': Object temperature in Celsius
  • 'D': Ambient temperature in Celsius
  • 'F': Object temperature in Fahrenheit
  • 'G': Ambient temperature in Fahrenheit
  • 'K': Object temperature in Kelvin
  • 'L': Ambient temperature in Kelvin


float getTemp(char type)

This function returns the temperature value as a float. It uses the same type characters as printTemp(). This function is useful for using the temperature readings in calculations or displaying them on other devices. (in video at 11:36)

Example usage of the getTemp() function:


if( getTemp('C')>40)
{
  //do something here, e.g., activate a fan
}

Live Project/Demonstration

The video demonstrates the project using an Arduino Uno and an MLX90614 sensor. (in video at 14:06) The demonstration shows the effect of distance on readings and how to take measurements from various objects, including a heater. (in video at 18:08)

Chapters

  • [00:00] Introduction and Project Overview
  • [00:46] Hardware Overview
  • [06:53] Wiring Instructions
  • [08:04] Code Explanation
  • [14:06] Project Demonstration
509-Lesson 38: Using the MLX90614 Infrared Temperature Sensor with an Arduino
زبان: C++
/*************************************************** 
 * Robojax Arduino Step By Step Course
 * Part 4: Temperature Sensors
  This is a library example for the MLX90614 Temp Sensor
Original Library and code source: https://github.com/adafruit/Adafruit-MLX90614-Library

 The code has been updated. I have added two functions. Also, the Kelvin
 value added into the C and F units.
 1-First function to print the temperature on Serial Monitor
 2-Second function returns the temperature so it can be used for display or other purposes
 
  Please watch video instructions here https://youtu.be/548PLHgZkoQ
 This code is available at http://robojax.com/course1/?vid=lecture37
 
with over 100 lectures free on YouTube. Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339  
 * 

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
 ****************************************************/

#include <Wire.h>
#include <Adafruit_MLX90614.h>
char *typeName[]={"Object","Ambient"};

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void setup() {
	// Robojax.com Code YouTube Watch it here http://robojax.com/L/?id=338
  Serial.begin(9600);

  Serial.println("Robojax MLX90614 test");  

  mlx.begin();  
}

void loop() {
  //Robojax Example for MLX90614
  printTemp('C');
  printTemp('D');
  
  printTemp('F');  
  printTemp('G'); 
  if( getTemp('C')>40)
  {
    //do something here
  }
  
  printTemp('K');   
  printTemp('L');  
  Serial.println("======");

  delay(3000);
  //Robojax Example for MLX90614
}

/*
 * @brief returns temperature or relative humidity
 * @param "type" is 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.com MLX90614 Code
   	// Robojax.com Code YouTube Watch it here http://robojax.com/L/?id=338
  float value;
    float tempObjec = mlx.readObjectTempC();//in C object
    float tempAmbient = mlx.readAmbientTempC();
   if(type =='F')
   {
    value = mlx.readObjectTempF(); //Fah. Object
   }else if(type =='G')
   {
    value = mlx.readAmbientTempF();//Fah Ambient
   }else if(type =='K')
   {
    value = tempObjec + 273.15;// Object Kelvin
   }else if(type =='L')
   {
    value = tempAmbient + 273.15;//Ambient Kelvin
   }else if(type =='C')
   {
    value = tempObjec;
   }else if(type =='D')
   {
    value = tempAmbient;
   }
   return value;
    // Robojax.com MLX90614 Code
}//getTemp

/*
 * @brief nothing
 * @param "type" is 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)
{
  float tmp =getTemp(type);

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

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


}//printTemp(char type)

منابع و مراجع

فایل‌ها📁

هیچ فایلی موجود نیست.