I2C with Raspberry pico

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ss5622759
Posts: 3
Joined: Mon Jul 18, 2022 10:24 am

I2C with Raspberry pico

Post by ss5622759 » Mon Jul 18, 2022 10:48 am

Code: Select all

import machine
import sys
import utime

ADDR = 0x38


def reg_write(i2c,addr,reg,data):
    
    msg = bytearray(data)
    
    i2c.writeto_mem(addr, reg, msg, addrsize = 16)

def reg_read(i2c,addr,reg,nbytes = 1):
    
    if nbytes < 1:
        return bytearray()
    
    data = i2c.readfrom_mem(addr,reg,nbytes, addrsize = 16)
    
    return data

# Main
i2c = machine.I2C(0, scl = machine.Pin(17), sda = machine.Pin(16), freq = 400000)

reg_write(i2c,ADDR,0x00FE,0xAD) #Unlocking register 0x0120 for optimal configuration


reg_write(i2c,ADDR,0x0120,0x30)


data = reg_read(i2c,ADDR,0x0120,nbytes = 2)

if(data != bytearray(0x30)):
    print("Error")
    sys.exit()

else:
    print("Successful.")
    sys.exit()
I'm trying to write and read operations from the memory of an IC using I2C protocol on Raspberry pi pico using the above code, but I'm getting the following error.

Code: Select all

OSError: [Errno 5] EIO
What is this error and how to solve this?

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

Re: I2C with Raspberry pico

Post by Roberthh » Mon Jul 18, 2022 11:00 am

Error 5 indicates a wiring problem. Please check:
- The Pin numbers given by Pin(xx) are GPIO numbers, not boarfd pin numbers.
- is GND connected
- are Pull-up resistors in place?
- what is the result of i2c.scan() after instantiating i2c?

ss5622759
Posts: 3
Joined: Mon Jul 18, 2022 10:24 am

Re: I2C with Raspberry pico

Post by ss5622759 » Tue Jul 19, 2022 9:36 am

Dear Roberthh

Thanks for the prompt reply.

I just want to know, do we have to provide external pull-up resistors with Pico?

Thanks

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

Re: I2C with Raspberry pico

Post by Roberthh » Tue Jul 19, 2022 9:46 am

The Pico board has no pull-up resistors. They would interfere with other usage of the Pins. And the configurable internal pull-up of the 2040 MCU's GPIO port is usually not strong enough. So you have to provide external ones. Many breakout boards have them, so you may not be the need to add further ones.

ss5622759
Posts: 3
Joined: Mon Jul 18, 2022 10:24 am

Re: I2C with Raspberry pico

Post by ss5622759 » Tue Jul 19, 2022 10:58 am

Dear Roberthh
  • The PINs given by Pin(xx) are GPIO numbers, not board PINs. : SDA - GPIO12 and SCL - GPIO13
  • Is Ground Connected: Yes
  • are Pull-up resistors in place: Yes
  • what is the result of i2c.scan() after instantiating i2c: d58 or 0x38
Still, I'm getting the same error.

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 50, in <module>
  File "<stdin>", line 33, in reg_write
OSError: [Errno 5] EIO
What is the problem now?

Thanks.

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

Re: I2C with Raspberry pico

Post by Roberthh » Tue Jul 19, 2022 12:14 pm

d58 is 0x3a.
If that was just a typo, there may still be an issue with clock stretching, which is not support well by all ports. But that requires looking at the signals e.g. with a logic analyzer. A 10$ device is sufficient.

Post Reply