Page 1 of 1

i2c init problem

Posted: Sat Oct 29, 2016 9:39 pm
by inaugurator
Hi,

MicroPython v1.8.5-75-g6a2c609 on 2016-10-29; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> from machine import I2C
>>> i2c = I2C(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'sda' argument required

What I do wrong?

Re: i2c init problem

Posted: Sun Oct 30, 2016 7:15 am
by pythoncoder
The machine.i2c module is poorly documented, notably the constructor which is undocumented for the Pyboard. It requires two arguments, both Pin objects, the first scl being the clock pin, and the second sda being the data pin.

Re: i2c init problem

Posted: Sun Oct 30, 2016 7:31 am
by shaoziyang
inaugurator wrote:Hi,

MicroPython v1.8.5-75-g6a2c609 on 2016-10-29; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> from machine import I2C
>>> i2c = I2C(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'sda' argument required

What I do wrong?

I2C usage is different between module machine and pyb.

if you using pyb, you may using like this
>>> from machine import I2C
>>> i2c = I2C(1)

but if using machine, you may using like below:
>>> from machine import I2C, Pin
>>> i2c=I2C(sda=Pin('B7'),scl=Pin('B6'))

Re: i2c init problem

Posted: Sun Oct 30, 2016 7:34 am
by inaugurator
Thank you a lot!

Re: i2c init problem

Posted: Wed Jan 18, 2017 8:29 am
by cagiva
[quote="shaoziyang"][quote="inaugurator"]
>>> i2c=I2C(sda=Pin('B7'),scl=Pin('B6'))[/quote]

Shouldn't scl be the first parameter and sda the second?

http://docs.micropython.org/en/latest/p ... achine.I2C

Re: i2c init problem

Posted: Wed Jan 18, 2017 4:10 pm
by dhylands
When you use keyword arguments, the order of the arguments doesn't matter. For postional arguments (i.e. non-keyword arguments), then the order is important.

See this post for the different ways to construct an I2C object on the pyboard using the machine module.

Re: i2c init problem

Posted: Wed Jan 18, 2017 4:13 pm
by cagiva
That make sense now. Thanks Dave for the clarification!