Page 1 of 1
I2C with Raspberry pico
Posted: Mon Jul 18, 2022 10:48 am
by ss5622759
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.
What is this error and how to solve this?
Re: I2C with Raspberry pico
Posted: Mon Jul 18, 2022 11:00 am
by Roberthh
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?
Re: I2C with Raspberry pico
Posted: Tue Jul 19, 2022 9:36 am
by ss5622759
Dear Roberthh
Thanks for the prompt reply.
I just want to know, do we have to provide external pull-up resistors with Pico?
Thanks
Re: I2C with Raspberry pico
Posted: Tue Jul 19, 2022 9:46 am
by Roberthh
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.
Re: I2C with Raspberry pico
Posted: Tue Jul 19, 2022 10:58 am
by ss5622759
Dear Roberthh
- The PINs given by Pin(xx) are GPIO numbers, not board PINs. : SDA - GPIO12 and SCL - GPIO13
- 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.
Re: I2C with Raspberry pico
Posted: Tue Jul 19, 2022 12:14 pm
by Roberthh
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.