Pi Pico erroring EIO when reading I2C sensor

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
thecomeback_king
Posts: 6
Joined: Tue Aug 03, 2021 12:31 pm

Pi Pico erroring EIO when reading I2C sensor

Post by thecomeback_king » Tue Aug 03, 2021 12:38 pm

I am using the new Raspberry Pi Pico and having issues reading the I2C Sensor . The address is 0x36. When running the below code I just receive error code EIO. Can anyone help guide me in reading data from my I2C sensor? Any help is appreciated!

Below is my code:

Code: Select all

from machine import I2C
import utime

i2c = I2C(0,freq=9600)
id = i2c.scan()
print(id)
utime.sleep_ms(2000)
data = i2c.readfrom(54, 4)
print(data)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Pi Pico erroring EIO when reading I2C sensor

Post by jimmo » Wed Aug 04, 2021 5:47 am

thecomeback_king wrote:
Tue Aug 03, 2021 12:38 pm
Can anyone help guide me in reading data from my I2C sensor? Any help is appreciated!
This error is because the device is NAKing the request. In this case because you need to tell it what you're reading. Generally devices like this you read from a specific register (i.e. you write the register address then read the result).

There's a CircuitPython driver from Adafruit here: https://github.com/adafruit/Adafruit_Ci ... /seesaw.py

In particular look at the sw_reset and get_temp methods (and the supporting read/write methods).

(You might find it easier to use CircuitPython on your Pico instead, and then you can just use the Adafruit driver directly)

thecomeback_king
Posts: 6
Joined: Tue Aug 03, 2021 12:31 pm

Re: Pi Pico erroring EIO when reading I2C sensor

Post by thecomeback_king » Wed Aug 04, 2021 12:40 pm

This helps. Thank you. Is the register the second argument in the read command? Where can the register be found?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Pi Pico erroring EIO when reading I2C sensor

Post by jimmo » Wed Aug 04, 2021 2:05 pm

thecomeback_king wrote:
Wed Aug 04, 2021 12:40 pm
Is the register the second argument in the read command?
In the Adafruit driver:

def read(self, reg_base, reg, buf, delay=0.008):

So "base" is like which "group" of registers, and "reg" is the particular register in that group. So for example in get_temp, it uses read(_STATUS_BASE, _STATUS_TEMP...)
thecomeback_king wrote:
Wed Aug 04, 2021 12:40 pm
Where can the register be found?
Usually you'd look at the data sheet (i.e. a given chip would have a reference of all the registers and commands it supports).

However, what's going on here that the chip you're talking to is an Adafruit Seesaw (basically this is a X to I2C converter, where in this case I'm guessing X is an analog voltage from the moisture sensor). The idea is that they can build a whole range of sensors and devices that all support a similar I2C protocol, even though they could be implemented in all sorts of different ways. The repo is https://github.com/adafruit/seesaw but I don't see any protocol documentation.

So probably the best reference is that driver. It might be worth asking in the Adafruit discord though.

thecomeback_king
Posts: 6
Joined: Tue Aug 03, 2021 12:31 pm

Re: Pi Pico erroring EIO when reading I2C sensor

Post by thecomeback_king » Wed Aug 04, 2021 3:54 pm

I have been looking through the seesaw.py and came up with the following code to try and read the data however I just get the same value.

Code: Select all

# Read Capactity and Temperature from Soil Sensor
    ss_wetness = i2c.readfrom(54, 0x1F)
    ss_temp =  i2c.readfrom(54, 0x4)    
    ss_wetness = unpack('<I', ss_wetness)[0]
    ss_temp = unpack('<I', ss_temp)[0]
OUTPUT:
Wetness: 4294967295
Temperature: 4294967295

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Pi Pico erroring EIO when reading I2C sensor

Post by jimmo » Fri Aug 06, 2021 2:08 am

thecomeback_king wrote:
Wed Aug 04, 2021 3:54 pm
came up with the following code
I don't think this is equivalent to the seesaw code.

If you look at for example get_temp, it calls read(_STATUS_BASE, _STATUS_TEMP...), which will first write [_STATUS_BASE, _STATUS_TEMP], and wait for the data ready pin, then attempt to read 4 bytes. (You're using 0x04 which is _STATUS_TEMP).

Post Reply