i2c init problem

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
inaugurator
Posts: 23
Joined: Tue Sep 30, 2014 4:02 pm

i2c init problem

Post by inaugurator » Sat Oct 29, 2016 9:39 pm

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?
Evgeny

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

Re: i2c init problem

Post by pythoncoder » Sun Oct 30, 2016 7:15 am

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.
Peter Hinch
Index to my micropython libraries.

shaoziyang
Posts: 363
Joined: Sun Apr 17, 2016 1:55 pm

Re: i2c init problem

Post by shaoziyang » Sun Oct 30, 2016 7:31 am

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'))

inaugurator
Posts: 23
Joined: Tue Sep 30, 2014 4:02 pm

Re: i2c init problem

Post by inaugurator » Sun Oct 30, 2016 7:34 am

Thank you a lot!
Evgeny

User avatar
cagiva
Posts: 22
Joined: Wed Dec 14, 2016 4:49 pm

Re: i2c init problem

Post by cagiva » Wed Jan 18, 2017 8:29 am

[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

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: i2c init problem

Post by dhylands » Wed Jan 18, 2017 4:10 pm

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.

User avatar
cagiva
Posts: 22
Joined: Wed Dec 14, 2016 4:49 pm

Re: i2c init problem

Post by cagiva » Wed Jan 18, 2017 4:13 pm

That make sense now. Thanks Dave for the clarification!

Post Reply