MCP9808 High Precision Temperature Sensor

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
curlydonkey
Posts: 4
Joined: Tue Apr 27, 2021 2:01 pm

Re: MCP9808 High Precision Temperature Sensor

Post by curlydonkey » Tue Apr 27, 2021 2:12 pm

Hi Folks

I'm trying to use this library with the Pi Pico running MicroPython v1.15. My code currently looks like this:

Code: Select all

from mcp9808 import MCP9808
from machine import I2C
from machine import Pin

i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=10000)
sensor = MCP9808(i2c)
However, I'm getting the following error:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
  File "/lib/mcp9808.py", line 52, in __init__
  File "/lib/mcp9808.py", line 86, in _check_device
  File "/lib/mcp9808.py", line 60, in _send
TypeError: object with buffer protocol required
I'm very new to Python so any guidance would be gratefully received.

User avatar
Roberthh
Posts: 2759
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: MCP9808 High Precision Temperature Sensor

Post by Roberthh » Tue Apr 27, 2021 2:37 pm

Which mcp9808 driver do you use? Is it the one from this source: https://github.com/kfricke/micropython-mcp9808
I looks like a variant, which allows an integer as argument to i2c.writeto(). You could try to replace __init__ and _send of that class with the following (untested) code:

Code: Select all

    def __init__(self, i2c=None, addr=0x18):
        """
        Initialize a sensor object on the given I2C bus and accessed by the
        given address.
        """
        if i2c == None or i2c.__class__ != I2C:
            raise ValueError('I2C object needed as argument!')
        self._i2c = i2c
        self._addr = addr
        self._buf1 = bytearray(1)
        self._check_device()

    def _send(self, buf):
        """
        Sends the given buffer object over I2C to the sensor.
        """
        if hasattr(self._i2c, "writeto"):
            # Micropython
            if type(buf) is int:
                self._buf1[0] = buf
                self._i2c.writeto(self._addr, self._buf1)
            else:
                self._i2c.writeto(self._addr, buf)
        elif hasattr(self._i2c, "send"):
            # PyBoard Micropython
            if type(buf) is int:
                self._buf1[0] = buf
                self._i2c.send(self._addr, self._buf1)
            else:
                self._i2c.send(self._addr, buf)
        else:
            raise Exception("Invalid I2C object. Unknown Micropython/platform?")

curlydonkey
Posts: 4
Joined: Tue Apr 27, 2021 2:01 pm

Re: MCP9808 High Precision Temperature Sensor

Post by curlydonkey » Tue Apr 27, 2021 2:41 pm

Sorry, yes, it's the driver you referenced. I will try your suggestion, thanks.

curlydonkey
Posts: 4
Joined: Tue Apr 27, 2021 2:01 pm

Re: MCP9808 High Precision Temperature Sensor

Post by curlydonkey » Tue Apr 27, 2021 3:03 pm

I think that may have worked, as I'm now getting:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
  File "/lib/mcp9808.py", line 53, in __init__
  File "/lib/mcp9808.py", line 94, in _check_device
  File "/lib/mcp9808.py", line 63, in _send
OSError: [Errno 5] EIO
I guess maybe I haven't connected my sensor correctly. Will investigate...

User avatar
Roberthh
Posts: 2759
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: MCP9808 High Precision Temperature Sensor

Post by Roberthh » Tue Apr 27, 2021 3:16 pm

you could simply use i2c.scan() with the instantiated i2c object to check the connection. It should return a list containing the address of the sensor as decimal value. An empty list or long list indicates an error.

curlydonkey
Posts: 4
Joined: Tue Apr 27, 2021 2:01 pm

Re: MCP9808 High Precision Temperature Sensor

Post by curlydonkey » Tue May 04, 2021 9:07 am

I managed to get I2C.scan() to find my temp sensor at address 0x18. However, when I pass the same I2C object to the MCP9808 constructor, I still get:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "/lib/mcp9808.py", line 53, in __init__
  File "/lib/mcp9808.py", line 95, in _check_device
  File "/lib/mcp9808.py", line 83, in _recv
OSError: [Errno 5] EIO
Seems to object to:

Code: Select all

return self._i2c.readfrom(self._addr, n)
I can't find any proper documentation regarding the EIO error.

User avatar
Roberthh
Posts: 2759
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: MCP9808 High Precision Temperature Sensor

Post by Roberthh » Tue May 04, 2021 9:26 am

EIO error just means that no data could be received. Since the send worked, it's most likely not a connection problem. And the python instruction also seems to be the right one.

Post Reply