Page 2 of 2
Re: MCP9808 High Precision Temperature Sensor
Posted: Tue Apr 27, 2021 2:12 pm
by curlydonkey
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.
Re: MCP9808 High Precision Temperature Sensor
Posted: Tue Apr 27, 2021 2:37 pm
by Roberthh
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?")
Re: MCP9808 High Precision Temperature Sensor
Posted: Tue Apr 27, 2021 2:41 pm
by curlydonkey
Sorry, yes, it's the driver you referenced. I will try your suggestion, thanks.
Re: MCP9808 High Precision Temperature Sensor
Posted: Tue Apr 27, 2021 3:03 pm
by curlydonkey
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...
Re: MCP9808 High Precision Temperature Sensor
Posted: Tue Apr 27, 2021 3:16 pm
by Roberthh
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.
Re: MCP9808 High Precision Temperature Sensor
Posted: Tue May 04, 2021 9:07 am
by curlydonkey
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.
Re: MCP9808 High Precision Temperature Sensor
Posted: Tue May 04, 2021 9:26 am
by Roberthh
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.