Viewing a Heat Map Data Stream in Python From MLX90640
by eawariyah in Circuits > Arduino
206 Views, 3 Favorites, 0 Comments
Viewing a Heat Map Data Stream in Python From MLX90640
In this tutorial, we will explore how to visualize real-time temperature data from the MLX90640 thermal sensor using Python. We'll use pyserial for serial communication and matplotlib for plotting a dynamic heatmap. This project is ideal for anyone interested in monitoring thermal changes or integrating thermal sensing into their projects.
Supplies
- MLX90640 Thermal Sensor
- ESP32 or Raspberry Pi or Alternative (for interfacing with the sensor)
- USB Cable
- Computer with Python installed
- pyserial, numpy, and matplotlib Python libraries installed (pip install pyserial numpy matplotlib...etc)
Setting Up the Hardware
Connect the MLX90640 sensor to your Arduino or Raspberry Pi according to the manufacturer's instructions.
Ensure the Arduino or Raspberry Pi is connected to your computer via USB.
(In my case, I 3-D printed a body to have a Serial Camera and a VL53LOX for contour detection by depth)
Traditional I2C configuration of your interface
Installing Required Libraries
Open a terminal or command prompt.
Install the necessary Python libraries:
pip install pyserial numpy matplotlib
Writing the Python Script
Below is the Python script to read data from the sensor and plot it as a heatmap:
import serial
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Serial port configuration
serial_port = 'COM3' # Replace with your serial port
baud_rate = 115200
# Initialize serial port
ser = serial.Serial(serial_port, baud_rate, timeout=1)
ser.flush()
# Initialize temperature array
temps = np.zeros(768)
max_temp = 0
min_temp = 500
# Create the figure for plotting
fig, ax = plt.subplots()
heatmap = ax.imshow(np.zeros((24, 32)), cmap='hsv', vmin=160, vmax=360)
plt.colorbar(heatmap)
def read_serial_data():
global max_temp, min_temp
if ser.in_waiting > 5000:
line = ser.read_until(b'\r')
if len(line) > 4608:
line = line[:4608]
split_string = line.decode().strip().split(',')
# Update min and max temperatures
max_temp = 0
min_temp = 500
for q in range(768):
try:
value = float(split_string[q])
if value > max_temp:
max_temp = value
if value < min_temp:
min_temp = value
except (ValueError, IndexError):
pass
# Map temperatures to colors
for q in range(768):
try:
value = float(split_string[q])
mapped_value = np.clip(np.interp(value, [min_temp, max_temp], [180, 360]), 160, 360)
temps[q] = mapped_value
except (ValueError, IndexError):
temps[q] = 0
def update_heatmap(*args):
read_serial_data()
heatmap.set_array(temps.reshape((24, 32)))
return heatmap,
ani = animation.FuncAnimation(fig, update_heatmap, interval=100)
plt.show()
Running the Script
Replace serial_port with the appropriate port for your setup (COM3 on Windows, /dev/ttyUSB0 on Linux, etc.).
Run the script in your Python environment (python heatmap_script.py).
Observing the Heatmap
Once the script is running, you should see a dynamic heatmap updating in real-time based on the temperature data from the MLX90640 sensor.
Conclusion: Congratulations! You've successfully created a real-time thermal heatmap using the MLX90640 sensor and Python. This project opens up possibilities for monitoring thermal trends in various applications, from home automation to industrial monitoring.
Experiment further by modifying the color mapping, adding data logging, or integrating this with other projects. Enjoy exploring the world of thermal sensing and visualization!
Additional Resources: