DEV Community

Cover image for How to Connect MLX90614 Infrared to Raspberry Pi Pico
Shilleh
Shilleh

Posted on

How to Connect MLX90614 Infrared to Raspberry Pi Pico

In this blog post, we’ll guide you through connecting the MLX90614 infrared temperature sensor to a Raspberry Pi Pico W using MicroPython. The MLX90614 sensor allows for non-contact temperature measurements, making it ideal for various applications. We’ll provide a step-by-step tutorial, including wiring, code, and testing.

Before we delve into the topic, we invite you to support our ongoing efforts and explore our various platforms dedicated to enhancing your IoT projects:

  • Subscribe to our YouTube Channel: Stay updated with our latest tutorials and project insights by subscribing to our channel at YouTube — Shilleh.
  • Support Us: Your support is invaluable. Consider buying me a coffee at Buy Me A Coffee to help us continue creating quality content.
  • Hire Expert IoT Services: For personalized assistance with your IoT projects, hire me on UpWork.
  • ShillehTek Website (Exclusive Discounts):

https://shillehtek.com/collections/all

ShillehTekAmazon Store for MLX90614 Pre-Soldered:

ShillehTek Amazon Store — US

ShillehTek Amazon Store — Canada

ShillehTek Amazon Store — Japan

Components Needed

  • Raspberry Pi Pico W
  • MLX90614 Infrared Temperature Sensor
  • Breadboard (optional)
  • 4 Jumper wires

Wiring Diagram
Connect the MLX90614 to the Raspberry Pi Pico W as follows:

Image description

MLX90614 VIN to Pico W 3.3V
MLX90614 GND to Pico W GND
MLX90614 SCL to Pico W GP1 (SCL)
MLX90614 SDA to Pico W GP0 (SDA)

Library Code
Save the following library code in mlx90614.py and upload it to your Raspberry Pi Pico W.

"""
MicroPython MLX90614 IR temperature sensor driver
https://github.com/mcauser/micropython-mlx90614MIT License
Copyright (c) 2016 Mike CauserPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""import ustructclass SensorBase:def read16(self, register):
  data = self.i2c.readfrom_mem(self.address, register, 2)
  return ustruct.unpack('<H', data)[0]def read_temp(self, register):
  temp = self.read16(register);
  # apply measurement resolution (0.02 degrees per LSB)
  temp *= .02;
  # Kelvin to Celsius
  temp -= 273.15;
  return temp;def read_ambient_temp(self):
  return self.read_temp(self._REGISTER_TA)def read_object_temp(self):
  return self.read_temp(self._REGISTER_TOBJ1)def read_object2_temp(self):
  if self.dual_zone:
   return self.read_temp(self._REGISTER_TOBJ2)
  else:
   raise RuntimeError("Device only has one thermopile")@property
 def ambient_temp(self):
  return self.read_ambient_temp()@property
 def object_temp(self):
  return self.read_object_temp()@property
 def object2_temp(self):
  return self.read_object2_temp()class MLX90614(SensorBase):_REGISTER_TA = 0x06
 _REGISTER_TOBJ1 = 0x07
 _REGISTER_TOBJ2 = 0x08def __init__(self, i2c, address=0x5a):
  self.i2c = i2c
  self.address = address
  _config1 = i2c.readfrom_mem(address, 0x25, 2)
  _dz = ustruct.unpack('<H', _config1)[0] & (1<<6)
  self.dual_zone = True if _dz else Falseclass MLX90615(SensorBase):_REGISTER_TA = 0x26
 _REGISTER_TOBJ1 = 0x27def __init__(self, i2c, address=0x5b):
  self.i2c = i2c
  self.address = address
  self.dual_zone = False
Enter fullscreen mode Exit fullscreen mode

Save the following main code in a file named main.py and upload it to your Raspberry Pi Pico W.

import time
import machine
from mlx90614 import MLX90614# Initialize I2C bus
i2c = machine.I2C(0, scl=machine.Pin(1), sda=machine.Pin(0), freq=100000)# Scan for I2C devices
devices = i2c.scan()if devices:
    print("I2C devices found:", [hex(device) for device in devices])
else:
    print("No I2C devices found")# Initialize the MLX90614 sensor
sensor = MLX90614(i2c)while True:
    ambient_temp = sensor.ambient_temp
    object_temp = sensor.object_tempprint(f"Ambient Temperature: {ambient_temp:.2f}°C")
    print(f"Object Temperature: {object_temp:.2f}°C")
    time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

Running the Code

Upload both mlx90614.py and main.py to your Raspberry Pi Pico W.
Run the main.py script using Thonny IDE or another suitable MicroPython environment.
Checking the Output
After running the script, you should see the ambient and object temperatures printed in the console every second. Ensure the temperatures are within realistic ranges to confirm the sensor is working correctly. You can point it at your skin closely to see if it changes the temperature reading. Goodluck on your project!

Conclusion

By following this guide, you can easily connect the MLX90614 infrared temperature sensor to your Raspberry Pi Pico W and read temperature data using MicroPython. This setup allows for non-contact temperature measurements, which can be useful in various projects. Do not forget to subscribe or book a consulting slot on buymeacoffee if you have any questions!

Top comments (0)