@Victor153764
Got my Pi Pico working and see that most of my concern about the error message was over my lack of experience using a Pi Pico.
Looking more closely at the way Python works and the existing PiicoDev libraries, I think I made a type definition mistake. Which is really what the error message is saying.
This statement writes to an I2C address, 3 bytes. Which starts the Mico Pressure sensor operating.
self.i2c.writeto_mem(self.addr, 0, bytes([0xAA,0x00,0x00]))
The 2nd parameter in the command is a register specification. I left it as zero as the Micro Pressure sensor has no registers. But some of the other PiicoDev libraries predefined it as a byte.
In C++ you have to define variable types before you use them or as you use them.
Python tries to determine what type you want and assigns it.
Using ‘0’ I think Python is defining the parameter as an integer not a byte.
I wonder if simply changing it to ‘0x00’ would fix it. Python would then see it is a byte.
Regards
Jim
Modified code.
"""
Honeywell Micro Pressure Sensor - MPR
Device in Standby Mode normally, send command, device enters Operating Mode, sensors pressure, when done flags EOC pin and Status byte.
Send 0xAA 0x00 0x00 to start reading.
Wait for status or EOC pin or at least 5ms.
Read 4 bytes, status, data 24:16, data 15:8, data 7:0
"""
from PiicoDev_Unified import *
compat_str = '\nUnified PiicoDev library out of date. Get the latest module: https://piico.dev/unified \n'
_MPR = 0x18
MAXIMUM_PSI = 25
MINIMUM_PSI = 0
BUSY_FLAG = 0x20
INTEGRITY_FLAG = 0x04
MATH_SAT_FLAG = 0x01
OUTPUT_MAX = 0xE66666
OUTPUT_MIN = 0x19999A
class PiicoDev_MPR(object):
def __init__(self, bus=None, freq=None, sda=None, scl=None, addr=_MPR):
try:
if compat_ind >= 1:
pass
else:
print(compat_str)
except:
print(compat_str)
self.i2c = create_unified_i2c(bus=bus, freq=freq, sda=sda, scl=scl)
self.addr = addr
def read(self):
self.i2c.writeto_mem(self.addr, 0x00, bytes([0xAA,0x00,0x00]))
sleep_ms(10)
status = self.i2c.readfrom_mem(self.addr, 0x00, 1)
while ((status & BUSY_FLAG) and (status != 0xFF)):
sleep_ms(1)
status = self.i2c.readfrom_mem(self.addr, 0x00, 1)
data = self.i2c.readfrom_mem(self.addr, 0x00, 4)
status = data[0]
if((status & INTEGRITY_FLAG) or (status & MATH_SAT_FLAG)):
return NAN;
reading = data[1]<<8 & data[2]<<8 & data[3]
pressure = (reading - OUTPUT_MIN) * (MAXIMUM_PSI - MINIMUM_PSI)
pressure = (pressure / (OUTPUT_MAX - OUTPUT_MIN)) + MINIMUM_PSI
return pressure
"""