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:
1 2 |
// initialize the button pin as a input pinMode(buttonPin, INPUT); |
1 2 3 4 5 6 7 8 9 10 |
//Skeleton of the if-else logic implemented with the button if (buttonState == HIGH) { //Turn on Red LED // if the current state is HIGH then the button is being pressed // Code to query temp sensor and print to LCD display } else { // if the current state is LOW then the button is not being pressed // Code to clear the lcd display } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//Importing the necessary Libraries #include <Wire.h> // I2C library, required for MLX90614 #include <SparkFunMLX90614.h> // IR Temperature Sensor Library from Sparkfun IRTherm therm; // Create an instantiated IRTherm object from the IRTherm class // This object will be used throughout the code to interact with the sensor //Initialize the object within the setup and set the following parameters therm.begin() therm.setEmissivity(0.98); //Sets the emissivity used for temperature calibration //emissivity is the measure of an object's ability to emit infrared energy //0.98 is commonly accepted for human skin therm.setUnit(TEMP_F); // Sets the units of the temperature readings to Fahrenheit // 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(). therm.object() therm.ambient() |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// include the Arduino LCD library #include <LiquidCrystal.h> // Initialize an instantiated lcd object from the library // with the numbers of the interface pins given as int variables const int rs = 7, en = 6, d4 = 5, d5 =4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // set up the LCD's number of columns and rows: lcd.begin(16, 2); // set the cursor to column 0, line 0 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 0); // print the object temperature in Fahrenheit lcd.print("Object: " + String(therm.object(), 2)); lcd.print("F"); // set the cursor to column 0, line 1 lcd.setCursor(0, 1); // print the ambient sensor temperature in Fahrenheit lcd.print("Ambient: " + String(therm.ambient(), 2)); lcd.print("F"); |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
// 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
Component | Quantity | Link |
Arduino Uno R3 Board | 1 | To Buy |
MLX90614 IR Temperature Sensor | 1 | To Buy Datasheet |
LCD1602 Module Display Screen | 1 | To Buy ELEGOO Starter Kit |
830 Tie-Points Breadboard | 1 | To Buy |
Red LED | 1 | LED Kit To Buy ELEGOO Starter Kit |
220 Ohm, 1 kOhm, 10 kOhm Resistor | 1 of each | ELEGOO Resistor Kit ELEGOO Starter Kit |
Button | 1 | To Buy ELEGOO Starter Kit |
Breadboard Jumper Wires | 12 m-m, 4 m-f | Jumper 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