MLX90640 Thermal Camera with ESP32
Thermal cameras are now more affordable, letting hobbyists and professionals explore various applications. One great option is the MLX90640 IR array with a 32x24 pixel resolution. By pairing it with an ESP32 board, you can make a thermal imaging device.
This guide will walk you through the process of connecting the MLX90640 to the ESP32, setting up the firmware, installing the necessary libraries, and obtaining temperature readings.
I was working on MLX90640 with different libraries and I had to make some adjustments to get some kind of reliability and accuracy. I’m so happy to share my experience with you.
Materials Needed
Wiring the MLX90640 to the ESP32
Here’s a simple explanation of the wiring,
Installing Necessary Libraries
For this project, you’ll need the Adafruit MLX90640 library.
Follow these steps to install them:
- Go to Documents > Arduino> libraries.
- Download the library, unzip it, and add it to the “libraries” Folder.
Firmware
- Copy the following code
#include <Wire.h>
#include <Adafruit_MLX90640.h>
// Addresses and parameters
const byte MLX90640_address = 0x33;
#define TA_SHIFT 8
Adafruit_MLX90640 mlx;
float frame[32 * 24];
void setup() {
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000);
// Ensure the MLX90640 is properly reset and initialized
if (!isConnected()) {
Serial.println("MLX90640 not detected at default I2C address. Please check wiring. Freezing.");
while (1);
}
Serial.println("MLX90640 online!");
// Perform a soft reset of the MLX90640
MLX90640_I2CWrite(0x33, 0x800D, 6401);
delay(100);
// Initialize MLX90640 using Adafruit's method
if (!mlx.begin()) {
Serial.println("Failed to initialize MLX90640!");
while (1);
}
mlx.setResolution(MLX90640_ADC_18BIT);
mlx.setRefreshRate(MLX90640_4_HZ);
mlx.setMode(MLX90640_CHESS);
Serial.println("MLX90640 initialized successfully.");
}
void loop() {
// Get frame using Adafruit method
delay(500);
if (mlx.getFrame(frame) != 0) {
Serial.println("Failed to get frame");
return;
}
float tempMinRead = 1000; // some high value
float tempMaxRead = -1000; // some low value
float tempCenter = 0.0; // Temperature at center
// Update the minimum, maximum, and center temperatures read from the sensor
for (int i = 0; i < 32 * 24; i++) {
if (frame[i] < tempMinRead) {
tempMinRead = frame[i];
}
if (frame[i] > tempMaxRead) {
tempMaxRead = frame[i];
}
if (i == 32 * 12 + 16) {
tempCenter = frame[i];
}
}
Serial.print("T Min: ");
Serial.print(tempMinRead);
Serial.print(" C, T Max: ");
Serial.print(tempMaxRead);
Serial.print(" C, Tc: ");
Serial.print(tempCenter);
Serial.println(" C");
}
// Check if the MLX90640 is connected
boolean isConnected() {
Wire.beginTransmission((uint8_t)MLX90640_address);
if (Wire.endTransmission() != 0)
return (false); // Sensor did not ACK
return (true);
}
// Custom I2C read and write functions
int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t regAddr, uint16_t len, uint16_t *data) {
Wire.beginTransmission(slaveAddr);
Wire.write(regAddr >> 8);
Wire.write(regAddr & 0xFF);
Wire.endTransmission(false);
Wire.requestFrom(slaveAddr, (uint8_t)(len * 2));
for (uint16_t i = 0; i < len; i++) {
data[i] = Wire.read() << 8;
data[i] |= Wire.read();
}
return 0;
}
void MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t regAddr, uint16_t data) {
Wire.beginTransmission(slaveAddr);
Wire.write(regAddr >> 8);
Wire.write(regAddr & 0xFF);
Wire.write(data >> 8);
Wire.write(data & 0xFF);
Wire.endTransmission();
}
- Paste the code on an Arduino Script.
- And save and compile the code.
- Upload the code to Esp32
Output
Hope you enjoyed our article.
Contact us for any consultations or projects related to thermal cameras.
Email: udara@protonest.co
Protonest for more details.
Protonest specializes in transforming IoT ideas into reality. We offer prototyping services from concept to completion. Our commitment ensures that your visionary IoT concepts become tangible, innovative, and advanced prototypes.
Our Website: https://www.protonest.co/
If you enjoyed this article and would like to show some support, consider buying me a coffee on Ko-Fi.
Your support not only helps me keep creating content like this but also fuels me! ☕️📚
Thank you for being an amazing reader!
Here’s my Ko-Fi link: https://ko-fi.com/udarakumarasena
Cheers!