Home / Projects / ESP32 Projects / Interfacing MLX90614 Non-Contact Infrared Temperature Sensor with ESP32
pcbway
Arduino MLX90614

Interfacing MLX90614 Non-Contact Infrared Temperature Sensor with ESP32

Introduction

Ever wanted to measure temperature without actually touching anything? Meet the MLX90614, a cool little infrared sensor that lets you do just that! Whether you’re building a DIY thermometer, automating your home, or monitoring industrial equipment, this sensor has got you covered. We have already covered how to use the MLX90614 with an Arduino. In this guide, we’ll hook up the MLX90614 to an ESP32, walk through the wiring, dive into the code, and throw in some handy troubleshooting tips along the way.

MLX90614 Specifications

  • Wide Temperature Range: -70°C to 380°C
  • Super Accurate: ±0.5°C (in the 0°C to 50°C range)
  • Tiny Details Matter: 0.02°C resolution
  • Uses I2C for Easy Communication
  • Plays Well with 3.3V and 5V Devices
  • Decent Field of View (FoV): Around 35°

Materials

  • An ESP32 board
  • An MLX90614 infrared temperature sensor
  • A bunch of jumper wires
  • A breadboard (optional but makes life easier)

Wiring Diagram

Connecting the MLX90614 to the ESP32 is a breeze. Just match up these pins:

MLX90614 Pin ESP32 Pin
VCC 3.3V
GND GND
SDA GPIO21
SCL GPIO22

Required Libraries

Before jumping into the code, let’s grab some libraries in the Arduino IDE:

  1. Open Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. Search for Adafruit MLX90614 and hit install.
  4. If needed, install Adafruit BusIO as well.

Reading Temperature

#include <Wire.h>
#include <Adafruit_MLX90614.h>


Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void setup() {
    Serial.begin(115200);
    Serial.println("MLX90614 Sensor Check...");
    if (!mlx.begin()) {
        Serial.println("Uh-oh! Can't find the MLX90614. Check your wiring!");
        while (1);
    }
}


void loop() {
    Serial.print("Ambient Temp: ");
    Serial.print(mlx.readAmbientTempC());
    Serial.println(" °C");
    Serial.print("Object Temp: ");
    Serial.print(mlx.readObjectTempC());
    Serial.println(" °C"); 

    delay(1000);
}

Code Explanation:

  1. Library Setup: We include Wire.h for I2C and Adafruit_MLX90614.h for sensor functions.
  2. Sensor Check: mlx.begin() ensures we can communicate with the MLX90614.
  3. Reading Temperatures:
    • mlx.readAmbientTempC() gives the surrounding temperature.
    • mlx.readObjectTempC() gets the temperature of whatever it’s pointed at.
  4. Serial Monitor Fun: Readings are printed every second for easy debugging.

 

Creating a Web Server to Display Temperature Readings

If you want to view the temperature readings from any device on your network, let’s create a simple web server on the ESP32.

#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <ESPAsyncWebServer.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

Adafruit_MLX90614 mlx = Adafruit_MLX90614();
AsyncWebServer server(80);

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }

    Serial.println("Connected to WiFi");


    if (!mlx.begin()) {
        Serial.println("Failed to find MLX90614 sensor!");
        while (1);
    }


    server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request) {
        String response = "Ambient Temp: " + String(mlx.readAmbientTempC()) + " °C\n";
        response += "Object Temp: " + String(mlx.readObjectTempC()) + " °C";
        request->send(200, "text/plain", response);
    });

    server.begin();
}


void loop() {
    // Nothing needed here, the server handles everything!
}

Code Explanation:

  1. WiFi Setup: Connects the ESP32 to your WiFi network.
  2. Web Server Setup: Creates a simple web server that responds to requests.
  3. Temperature Data on Request: Visiting http://your-esp32-ip/temperature will display the temperature readings.
  4. No Looping Needed: Since the web server runs in the background, there’s no need for code in loop().

Tips for More Accurate Readings

  • Know Your Emissivity: Most organic materials work well with the default 0.95 setting, but metals or shiny surfaces may need adjustments.
  • Keep the Right Distance: Too close or too far can affect accuracy.
  • Avoid Direct Heat Sources: Bright lights or strong infrared sources can throw off readings.

What If Things Go Wrong?

  • No readings? Double-check your wiring, especially SDA/SCL connections.
  • Weird errors? Use an I2C scanner script to verify the sensor’s address.
  • Inconsistent numbers? Let the sensor stabilize before trusting the values.

Project Ideas for the MLX90614

  • DIY Contactless Thermometer: Perfect for checking object or body temperatures.
  • Smart Home Integration: Use temperature readings to trigger fans, AC, or heating.
  • Machine Health Monitoring: Detect overheating components in real-time.
  • Greenhouse or Animal Monitoring: Keep track of temperature conditions easily.

Handy Resources

Wrapping It Up

And there you have it! The MLX90614 + ESP32 combo is a powerful tool for measuring temperatures without contact. Whether you’re making a thermometer or automating home gadgets, this sensor is a great addition to your projects. Now, with the added web server, you can monitor temperatures remotely! So go ahead, start experimenting, and have fun coding!

Check Also

ESP32 Solenoid Lock

ESP32 Solenoid Lock Controller

A solenoid lock is an electric actuator that converts electrical signals to motion. It is …

Index