Categories
TUTORIALS

How to make an Infrared Thermometer with Arduino

Learn how to wire and code a basic DIY button-triggered thermometer that can measure and display temperature readings from an MLX90614 IR temperature sensor on an LCD screen

To make the thermometer you will need to integrate 3 individual components, a button, IR temperature sensor, and an LCD display. First, you will set up a button that will trigger your temperature measurement. Second, you will set up and communicate with the MLX90614 temperature sensor via an I2C connection. Third, you will connect to an LCD screen to displau the temperature readings.

Button Setup and Code

You will need to connect four male to male wires from your Arduino to the breadboard. Connect the voltage source 5V pin on Arduino to one of the long vertical red ‘+’ bus rows on the breadboard and the ground GND pin on the Arduino to the one of the other long vertical blue ‘-‘ bus rows. This will allow you to create multiple connections to +5V supply and ground on the breadboard. Then, connect the third wire from digital pin 10 to one leg of the button. Through the same leg but on the other side of the button connect the leg to ground through a pull-down resistor (10 kOhm). The pulldown resistor allows us to define the OFF state of the button as the LOW voltage reading since the circuit directly connects digital pin 10 to ground through the resistor. When the button is pressed down it connects the circuit through the other leg of the button which is wired to the +5V supply, resulting in a voltage reading of HIGH, which we denote ON.

In addition to the button wiring, a red LED on Arduino digital pin 13 is connected through to ground, so that the button state can be visualized.

Here is how we implement that in the Arduino code:

MLX90614 IR Sensor Setup and Code

It is assumed that the MLX90614 sensor you are using is mounted on a printed circuit board with 4 male header pins soldered on for connectors. You will need 4 male to female jumper wires to make all the necessary connections. Connect the Vin pin on the sensor to the +5V power supply ‘+’ rail on the breadboard and the GND pin on the sensor to the ground ‘-‘ rail on the breadboard. Then connect the two I2C connectors, SCL and SDA, on the sensor to the SCL and SDA pins on the Arduino.

There is a very nice tutorial on I2C communication protocol on the Arduino Docs site, which I quote from here, “The I2C protocol involves using two lines to send and receive data: a serial clock pin (SCL) that the Arduino Controller board pulses at a regular interval, and a serial data pin (SDA) over which data is sent between the two devices.”

Here is how we implement that in the Arduino code:

Click here for the Wire/I2C library from Arduino

Click here for the MLX90614 IR Temperature Sensor library from Sparkfun

LCD Display Setup and Code

The wiring for the LCD Display is the most involved, but your hard work will pay off since we can use the Arduino LiquidCrystal Library for super easy coding. There is a nice detailed tutorial on Arduino Docs for using Liquid Crystal Displays (LCD) with Arduino.

The pins you will need to connect include

  • Register select (RS), which allows you to digitally control which memory register you are communicating with. There is a data register for storing characters to display and an instruction register for storing instructions for the LCD
  • Read/Write (RW), which controls whether the display is in read or write mode
  • Enable (E), which digitally enables writing characters to the display
  • Data Bit pins 0-7 (DB0…DB7), which correspond to the bits of data you actually write to the LCD data register
  • V0 – display contrast which is wired in series with a current limiting 1 kOhm resistor
  • VSS and VDD are the +5V power supply and ground pins respectively
  • A (LED-) and K (LED+) are the LED backlight voltage supply and ground pins respectively, a current limiting 220 Ohm resistor is placed in series with the LED voltage supply in

Here is some basic code for the LCD Display:

Click here for the LiquidCrystal Library from Arduino

Putting it all together in a complete circuit

Congrats! you have now set up all the individual components and are ready to integrate them together. Below, are complete circuit diagrams and schematics along with the complete plug and play code to get the sensor up and running.

Complete Code

// Noah Dylan Johnson
// April 5th, 2023

//This code, when run, creates a button triggered IR temperature sensor 
// that measures object and ambient temperature
// and displays those values on an LCD display

// This code uses code and ideas from:

// IR Temperature Sensor Library
// Jim Lindblom @ SparkFun Electronics
// October 23, 2015
// https://github.com/sparkfun/SparkFun_MLX90614_Arduino_Library

// LCD Tutorial  
//Library originally added 18 Apr 2008
//  by David A. Mellis
//  library modified 5 Jul 2009
//  by Limor Fried (http://www.ladyada.net)
//  example added 9 Jul 2009
//  by Tom Igoe
//  modified 22 Nov 2010
//  by Tom Igoe
//  This example code is in the public domain.
//  http://www.arduino.cc/en/Tutorial/LiquidCrystal

// Button Tutorial 
//  created 2005
//   by DojoDave <http://www.0j0.org>
//   modified 30 Aug 2011
//   by Tom Igoe
//   This example code is in the public domain.
//   https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button


// Hardware Hookup (if you're not using the eval board):

//Button
// Button Leg 1
// Digital Pin 10 through one leg to +5V
// Button Leg 2
// open on the side with opposite leg connected to digital pin 10 and 
// then connected through a 10 kOhm resistor to ground on other side

// MLX90614 ------------- Arduino
//   VDD ------------------ 3.3V
//   VSS ------------------ GND
//   SDA ------------------ SDA (A4 on older boards)
//   SCL ------------------ SCL (A5 on older boards)

//  LCD Display
//  * LCD RS pin to digital pin 7
//  * LCD Enable pin to digital pin 6
//  * LCD D4 pin to digital pin 5
//  * LCD D5 pin to digital pin 4
//  * LCD D6 pin to digital pin 3
//  * LCD D7 pin to digital pin 2
//  * LCD R/W pin to ground
//  * LCD VSS pin to ground
//  * LCD VDD pin to 5V
//  * K/LED- to ground
//  * A/LED+ to +5V
//  * LCD VO pin through 1 kOhm resistor to ground (pin 3)

//Libraries to include

#include <Wire.h> // I2C library, required for MLX90614
#include <SparkFunMLX90614.h> // IR Temperature Sensor Library by SparkFun
#include <LiquidCrystal.h> // Arduino Liquid Crystal Library

// constants to set the buttona and LED pins
const int buttonPin = 10;  // the pin that the pushbutton is attached to
const int ledPin = 13;    // the pin that the LED is attached to

// initialize the lcd object with the 
// numbers of the interface pins denoted by constants
const int rs = 7, en = 6, d4 = 5, d5 =4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Create an IRTherm object to interact with throughout
IRTherm therm; 

//Setup will run once and includes most of the initilization of the objects
void setup() 
{
  Serial.begin(115200); // Initialize Serial to log output
  Wire.begin(); //Joing I2C bus
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  
  if (therm.begin() == false){ // Initialize thermal IR sensor
    Serial.println("Qwiic IR thermometer did not acknowledge! Freezing!");
    while(1);
  }
  Serial.println("Qwiic IR Thermometer did acknowledge.");
  
  therm.setEmissivity(0.98); //Set the sensor's emisaivity value to 0.98
  // This is the commonly used value for human skin and enables more accurate temperature readings 
  therm.setUnit(TEMP_F); // Set the library's units to Farenheit
  // Alternatively, TEMP_F can be replaced with TEMP_C for Celsius or
  // TEMP_K for Kelvin.

  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED pin as output
  pinMode(ledPin, OUTPUT);
}

//This part of the code will run incessantly after the setup code is run
//Within this loop function there are a series of if-else statements that are the 
// backbone of the thermometer code
// If the button is pressed down then the Arduino tells the ir sensor to 
void loop() 
{

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    // if the current state is HIGH then the button went from off to on:
    //Turn on the LED
    digitalWrite(ledPin, HIGH);
    
    // Call therm.read() to read object and ambient temperatures from the sensor.
    if (therm.read()) // On success, read() will return 1, on fail 0.
    {
      // set the cursor to column 0, line 0
      // (note: line 1 is the second row, since counting begins with 0):
      lcd.setCursor(0, 0);    
        // Use the object() and ambient() functions to grab the object and ambient
      // temperatures.
      // They'll be floats, calculated out to the unit you set with setUnit().
      lcd.print("Object: " + String(therm.object(), 2));
      lcd.print("F");
      // set the cursor to column 0, line 1
      lcd.setCursor(0, 1); 
      lcd.print("Ambient: " + String(therm.ambient(), 2));
      lcd.print("F");
    }
    delay(1000);
    //delay long enough so that each indiviudal temperature reading can be viewed   
    else{
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);    
      //Print an error message to the LCD
      lcd.print("No reading");
    }
  }
  else {
      // if the current state is LOW then the button went from on to off:
      lcd.clear();
      Serial.println("off");
      digitalWrite(LED_BUILTIN, LOW);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
}

Parts List

ComponentQuantityLink
Arduino Uno R3 Board 1To Buy
MLX90614 IR Temperature Sensor1To Buy
Datasheet
LCD1602 Module Display Screen1To Buy
ELEGOO Starter Kit
830 Tie-Points Breadboard1To Buy
Red LED1LED Kit To Buy
ELEGOO Starter Kit
220 Ohm, 1 kOhm, 10 kOhm Resistor1 of eachELEGOO Resistor Kit
ELEGOO Starter Kit
Button1To Buy
ELEGOO Starter Kit
Breadboard Jumper Wires12 m-m, 4 m-fJumper Wire Kit
ELEGOO Starter Kit

Video Demonstration

References

Libraries:

https://www.arduino.cc/reference/en/language/functions/communication/wire/

https://www.arduino.cc/reference/en/libraries/liquidcrystal/

https://github.com/sparkfun/SparkFun_MLX90614_Arduino_Library

Other Tutorials:

I2C –https://github.com/sparkfun/SparkFun_MLX90614_Arduino_Library

LCD-https://docs.arduino.cc/learn/electronics/lcd-displays

Wire and Program a Button-https://docs.arduino.cc/built-in-examples/digital/Button

Make a noncontact IR thermometer-https://circuitdigest.com/microcontroller-projects/ir-thermometer-using-arduino-and-ir-temperature-sensor

Interface MLX90614 Non-contact Infrared Temperature Sensor with Arduino-https://lastminuteengineers.com/mlx90614-ir-temperature-sensor-arduino-tutorial/#how-do-infrared-thermometers-work

Arduino Style Guide-https://docs.arduino.cc/learn/contributions/arduino-writing-style-guide

Extra Reading:

PullUp Resistors https://learn.sparkfun.com/tutorials/pull-up-resistors/all

Leave a Reply

Your email address will not be published. Required fields are marked *