i2c read fails with OSError 5

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
DirkHartkopf
Posts: 7
Joined: Mon Mar 22, 2021 7:32 pm

i2c read fails with OSError 5

Post by DirkHartkopf » Mon Mar 22, 2021 8:14 pm

I try to read data with a Raspberry Pi Pico from a tcs34725 with following code:

Code: Select all

from machine import Pin,I2C

sda = Pin(16)
scl = Pin(17)
i2c = I2C(0,sda=sda,scl=scl,freq=200000)

register = 0x80 | 0x12
id = i2c.readfrom_mem(0x29,register,1)
print(id)
This simple program fails with OSError 5 and I have no clue why?
I'm new in microcontroller programming. Can anybody help?

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

Re: i2c read fails with OSError 5

Post by Roberthh » Mon Mar 22, 2021 8:35 pm

Do you have pull-up resistors on SDA and SCL?

DirkHartkopf
Posts: 7
Joined: Mon Mar 22, 2021 7:32 pm

Re: i2c read fails with OSError 5

Post by DirkHartkopf » Mon Mar 22, 2021 9:30 pm

Thanks for the quick answer!
I thought that these pull-up resistors are part of the Pico board?!
Which values do they need to have?

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

Re: i2c read fails with OSError 5

Post by Roberthh » Tue Mar 23, 2021 6:19 am

4.7 to 10 kOhm.
Edit: If you are using the Adafruit breakout: that one has the pull-up resistors on the breakout.

DirkHartkopf
Posts: 7
Joined: Mon Mar 22, 2021 7:32 pm

Re: i2c read fails with OSError 5

Post by DirkHartkopf » Tue Mar 23, 2021 9:02 am

I have the ADAFRUIT breakout board. This means on the other hand that the pull-up resistor is not my problem, right?
Nevertheless I tried to insert pull-up resistors, but without success.

By the way: The following scan work without problems and sends 0x29 as the correct adress of the board.

Code: Select all

import utime
from machine import Pin,I2C

sda = Pin(16)
scl = Pin(17)
i2c = I2C(0,sda=sda,scl=scl,freq=400000)

print(i2c.scan())
Do you have any other ideas?

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

Re: i2c read fails with OSError 5

Post by Roberthh » Tue Mar 23, 2021 10:13 am

If scan() returns a proper value, than the wiring is fine. So the result of the scan is [41]? Scan returns decimal values.

DirkHartkopf
Posts: 7
Joined: Mon Mar 22, 2021 7:32 pm

Re: i2c read fails with OSError 5

Post by DirkHartkopf » Tue Mar 23, 2021 2:33 pm

As I mentioned before the scan runs properly and returns the correct value.
But the "writeto_mem" and "readfrom_mem" commands don't work.

In parallel I try to get a DOGS104-A display runnning. In this case a writeto_mem statement is failing.

Does the Pico have general problems with the I2C interface?

(Currently I'm quite desperate and disappointed about this not working capability)

taPIQoLEHUMA
Posts: 15
Joined: Thu Mar 04, 2021 2:59 am

Re: i2c read fails with OSError 5

Post by taPIQoLEHUMA » Tue Mar 23, 2021 6:21 pm

Dirk, can you do a regular i2c.readfrom() with the TCS34725? Just wondering; it would be another data point.

Edit: I am able to do 'i2c.readfrom_mem()' with the HT16K33 and is31fl3731 matrix displays and an SSD1306 OLED. I guess that means it can work with some I2C devices.

DirkHartkopf
Posts: 7
Joined: Mon Mar 22, 2021 7:32 pm

Re: i2c read fails with OSError 5

Post by DirkHartkopf » Wed Mar 24, 2021 10:44 am

Thanks for your effort on this topic.
In the meantime somebody told me that with SoftI2C this problem could disappear and it does!

Obviously some sensors are not able to communicate with RB Pico via I2C, because the Pico has a (fast) hardware support for this protocol.

taPIQoLEHUMA
Posts: 15
Joined: Thu Mar 04, 2021 2:59 am

Re: i2c read fails with OSError 5

Post by taPIQoLEHUMA » Sat Mar 27, 2021 9:02 pm

Turns out this is a known problem, and even happens on other boards: https://github.com/micropython/micropython/issues/6891

Post Reply