I2C Master-Slave

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
Post Reply
MarkTk
Posts: 8
Joined: Tue Mar 24, 2020 8:01 am

I2C Master-Slave

Post by MarkTk » Tue Mar 24, 2020 8:24 am

Hi all,
i´m starting to work with Micropython and i´m having some questions about it.
I was trying to test the I2C communication in my Pyboard, and for that, I joined the sda ​​pins (X10, Y10) together, and also for the scl(pins X9 and Y9), and I was trying to configure one as master and the other as slave, but its not working.
-------------------------------------------------------------
i2c = I2C(1)
i2c = I2C(1, I2C.MASTER)
i2c.init(I2C.MASTER, baudrate=400000)

i2c1 = I2C(2)
i2c1.init(I2C.SLAVE, addr=0x11)

i2c.scan()
[]

i2c.send('abc', addr=0x11)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 110] ETIMEDOUT



I also configured the pull-up resistance for that:
------------------------------------------------
scl = Pin('X9', Pin.IN, Pin.PULL_UP)
sda = Pin('X10', Pin.IN, Pin.PULL_UP)
------------------------------------------------------


When I scan it, its empty, and i am getting always an error in sending...Could you help me with this, i am configuring something wrong?

Thanks so much in advance!!!
Last edited by MarkTk on Tue Mar 24, 2020 11:16 am, edited 2 times in total.

User avatar
pythoncoder
Posts: 4241
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: I2C Master-Slave

Post by pythoncoder » Tue Mar 24, 2020 8:31 am

This is a somewhat non-trivial problem. See my library which explains the difficulties and offers a solution.
Peter Hinch

MarkTk
Posts: 8
Joined: Tue Mar 24, 2020 8:01 am

Re: I2C Master-Slave

Post by MarkTk » Tue Mar 24, 2020 8:53 am

Hello pythoncoder,

thanks for the quick reply. Is it possible to do it, with the same pyboard as master-slave with the two I2C? or it is needed and extra device as in your case the (ESP8266)??

User avatar
pythoncoder
Posts: 4241
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: I2C Master-Slave

Post by pythoncoder » Wed Mar 25, 2020 6:45 am

I'm not quite sure what you're asking here. It can be used to communicate between Pyboards: the only restriction is that one end of the link must be a Pyboard, the other end can be any MicroPython device. That is because only Pyboards support slave mode.

If your query is about using both I2C interfaces concurrently to communicate with two targets, I haven't tested this but I would expect it to work. If you do this successfully please let me know and I will add a note to the docs.
Peter Hinch

User avatar
tve
Posts: 210
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: I2C Master-Slave

Post by tve » Wed Mar 25, 2020 4:51 pm

I believe the OP is trying to set-up a loop-back situation with one device being master and its own slave, presumably for testing purposes?

BenY
Posts: 2
Joined: Wed Mar 25, 2020 1:52 pm

Re: I2C Master-Slave

Post by BenY » Wed Mar 25, 2020 8:05 pm

Hi

Master/slave on 1 pyboard-d, problem is the master scan() only gets an answer when the slave_i2c is in .recv() mode,
not when the slave_i2c is created, but then when the .recv() is active you can not run the scan.

Communication works fine when using 2 or more pyboards, tested between pybD and pyb (v1.*) [1 master/many slaves]
# pyboard-d setup
from pyb import Pin
from machine import I2C
pyb.Pin('EN_3V3').on()
# pybD : pull up only for X [ not available for Y (untested) ]
pSCL = Pin('PULL_SCL', Pin.OUT, value=Pin.PULL_UP) # enable 5.6kOhm W59-B8-X9/SCL
pSDA = Pin('PULL_SDA', Pin.OUT, value=Pin.PULL_UP) # enable 5.6kOhm W55-B9-X10/SDA

i2c = pyb.I2C("X", pyb.I2C.MASTER, baudrate=400000) # X/Y preferred, not 1/2 or 1/3
slave_i2c = pyb.I2C("Y", pyb.I2C.SLAVE, addr=0x12)

MarkTk
Posts: 8
Joined: Tue Mar 24, 2020 8:01 am

Re: I2C Master-Slave

Post by MarkTk » Tue Mar 31, 2020 5:10 pm

Hi,
As you indicated I was trying to work with Master/Slave on 1 pyboard-d.
Thanks so much for the answer and the information. It helped me a lot!!

Post Reply