MLX90614 IR Temp Sensor Library

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

MLX90614 IR Temp Sensor Library

Post by mcauser » Sat Sep 24, 2016 3:52 pm

I created a library for my MLX90614 IR temp sensor.
https://github.com/mcauser/micropython-mlx90614

It's a GY module, labelled GY-906.
They go for around $4.50 USD on AliExpress.com

MauroDiam
Posts: 7
Joined: Thu Jul 23, 2020 1:38 pm

Re: MLX90614 IR Temp Sensor Library

Post by MauroDiam » Fri Oct 01, 2021 8:04 pm

Hello, I'm trying to use your module but I'm getting huge temperature values, around 1000ºC. Which could be the reason? Does this module work with single zone MLX90614 sensor?

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: MLX90614 IR Temp Sensor Library

Post by davef » Fri Oct 01, 2021 9:23 pm

Can't help with that particular module, but I have seen many references to needing pull-up resistors for I2C devices.

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: MLX90614 IR Temp Sensor Library

Post by mcauser » Fri Oct 01, 2021 11:47 pm

I haven’t seen a +1k jump like that before.
Is it consistently +1k?

It was originally designed for the single zone sensor (the one I have).
A contributor added support for the dual zone version.
Same driver works with both.

It is an I2C device so needs pull ups.
Are you using a GY-906 module? It should have them on the back.

Could there be a newer MLX sensor on the market with higher specs/precision? Do you have any further details about your sensor?

MauroDiam
Posts: 7
Joined: Thu Jul 23, 2020 1:38 pm

Re: MLX90614 IR Temp Sensor Library

Post by MauroDiam » Thu Oct 14, 2021 2:34 am

Hi everybody, thanks for your help.

The temperature values ​​weren't consistently, I got the same value (1037.55) no matter the object which was front of the sensor. I was using the GY-906 module, exactly the same that you show in the github repository.

After searching around the web I found a very interesting post which is very related to the problem I was experiencing. I share it with you if this could help someone:
https://stackoverflow.com/questions/648 ... ure-sensor
However, the solutions which are proposed in that post didn't help me.

After that, I decided to add pull-up resistors to the circuit to check if that could solve the problem, but I didn't get any difference.

So, as a last option, I decided to read the sensor datasheet and I found that the maximum frequency for the I2C interface (SMBus 2-wire interface) is 100 KHz and the micropython module for I2C set the frequency to 400 KHz by default, so I explicitly set it up to 100 KHz and it solved the problem. :D I think it would be better to change the class initializer to receive the pin numbers where the sensor is connected and internally build the I2C object with the correct parameters, even the internal pull-up resistors could be enabled to avoid to have to put external resistors.

PD: I still got wrong temperature values ​​with some sensors even after changing the frequency. It seemed that they were broken. And with the other devices, sometimes I got wrong values or the ENODEV error, which seemed to be caused by the electrical noise.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: MLX90614 IR Temp Sensor Library

Post by mattyt » Fri Oct 15, 2021 1:40 am

I'm glad you found your problem!
MauroDiam wrote:
Thu Oct 14, 2021 2:34 am
I think it would be better to change the class initializer to receive the pin numbers where the sensor is connected and internally build the I2C object with the correct parameters, even the internal pull-up resistors could be enabled to avoid to have to put external resistors.
I'm afraid this is an anti-pattern. Low-level drivers such as machine.I2C should be passed in to device drivers because they can be set up quite differently depending on hardware (for example, different boards will use different pins, pull-up configuration can differ and some may use software I2C etc). The device driver should not configure I2C directly. Further, consider unit testing; it's very common to pass in a mock I2C device to test the driver.

It would be nice to check the I2C frequency but unfortunately it's not possible to query freq from an I2C instance. It is visible when converting to string:

Code: Select all

>>> str(I2C(1))
'I2C(1, scl=25, sda=26, freq=400000)'
but it's fragile to parse the string.

Unless freq is exposed in I2C then the best option is to clearly document this constraint in the micropython-mlx90614 readme. Maybe raise a PR against the repo?

mdh2o
Posts: 1
Joined: Thu Jan 13, 2022 11:46 am

Re: MLX90614 IR Temp Sensor Library

Post by mdh2o » Thu Jan 13, 2022 12:09 pm

Hello, I have used this code:

Code: Select all

import time
import mlx90614

from machine import I2C, Pin

sda=machine.Pin(2)
scl=machine.Pin(3)

i2c = I2C(1, sda = sda, scl = scl, freq=100000)

#I2C_MLX90614     = 0x5a
sensor = mlx90614.MLX90614(i2c)

while True:
    print(sensor.read_ambient_temp(), sensor.read_object_temp())
    time.sleep_ms(500)
the result has been something like this:
19.14999 1037.55
19.14999 1037.55
19.14999 1037.55


Putting some time between readings...

Code: Select all

while True:
    amb = sensor.read_ambient_temp()
    time.sleep_ms(500)
    obj = sensor.read_object_temp()
    time.sleep_ms(500)
    
    print(amb, obj)
    time.sleep_ms(500)
The new result looks like this:
19.13 18.54999
19.19 18.69
19.14999 1037.55
19.13 1037.55
19.19 34.82999
19.13 35.42999

The result 1037.55 keeps appearing

How can I solve that?

Thanks by advance

Manuel

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: MLX90614 IR Temp Sensor Library

Post by mcauser » Thu Jan 13, 2022 11:16 pm

Hi @mdh20

I believe it's related to this:
https://github.com/mcauser/micropython- ... 4/issues/7

When the sensor returns 0xFF 0xFF for the temperature, it's converted to 1037.55
The sensor does not support readings >1000 deg C, so it's an invalid response.

I think I should update the driver so that it throws when it gets an invalid response on the I2C bus, rather than let these invalid high temps pass through.

Which board/port are you using? STM32, ESP32, RP Pico?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: MLX90614 IR Temp Sensor Library

Post by dhylands » Fri Jan 14, 2022 12:46 am

I wonder if the issue is related to clock stretching?

See this post: https://github.com/micropython/micropython/issues/8167 (which is RPi Pico specific - so if you're not using a pico you can ignore this) (and the corresponding forum post: viewtopic.php?f=21&t=11745&hilit=Circui ... =40#p64134)

It would be interesting to see if you get the same behaviour when using SoftI2C rather than HardI2C

Post Reply