Using machine.I2C

Discussion related to "under the hood" OpenMV topics.
User avatar
deshipu
Posts: 74
Joined: Sat Jan 09, 2016 5:36 am
Location: Zürich
Contact:

Using machine.I2C

Postby deshipu » Sat Apr 08, 2017 4:19 am

I have my servo shield assembled and ready for testing, so I of course wanted to try it with the PCA9685 library from https://github.com/adafruit/micropython ... it-pca9685

Unfortunately, I can't figure out how to properly use machine.I2C on the OpenMV board. I tried this (with nothing connected):

Code: Select all

>>> from machine import I2C, Pin
>>> i2c = I2C(sda=Pin('PB5'), scl=Pin('PB4'))
>>> i2c.scan()
[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]
And this:

Code: Select all

>>> i2c = I2C(-1, sda=Pin('PB5'), scl=Pin('PB4'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: extra keyword arguments given
Any hints? I can't use pyb.I2C, because that library is written for the new "machine" API.
User avatar
kwagyeman
Posts: 4658
Joined: Sun May 24, 2015 2:10 pm

Re: Using machine.I2C

Postby kwagyeman » Sat Apr 08, 2017 12:45 pm

Do you have pull ups on the I/O pins?

Anyway, Ibrahim, can you take a look at this?
Nyamekye,
User avatar
iabdalkader
Posts: 1245
Joined: Sun May 24, 2015 3:53 pm

Re: Using machine.I2C

Postby iabdalkader » Sat Apr 08, 2017 1:43 pm

I think you meant to use P4 and P5 not PB4 and PB5.
User avatar
deshipu
Posts: 74
Joined: Sat Jan 09, 2016 5:36 am
Location: Zürich
Contact:

Re: Using machine.I2C

Postby deshipu » Sat Apr 08, 2017 3:23 pm

You are right, that works perfectly, sorry!
kidswong999
Posts: 36
Joined: Sun Apr 02, 2017 1:12 pm

Re: Using machine.I2C

Postby kidswong999 » Tue May 09, 2017 12:35 am

Hello, what is the difference between machine.I2C and pyb.I2C?
why there are two class, but one thing? :?:
User avatar
kwagyeman
Posts: 4658
Joined: Sun May 24, 2015 2:10 pm

Re: Using machine.I2C

Postby kwagyeman » Tue May 09, 2017 1:01 am

Machine is like software bit banging which pyb uses the MCU hardware.

Since MP is single threaded machine is actually easier to use since the hardware does weird stuff on error conditions and you have to carefully wrap the code in try/catch statements.
Nyamekye,
User avatar
deshipu
Posts: 74
Joined: Sat Jan 09, 2016 5:36 am
Location: Zürich
Contact:

Re: Using machine.I2C

Postby deshipu » Tue May 09, 2017 4:10 am

Actually the machine module is the new, cross-port-compatible API, and pyb is the legacy API originally only for the PyBoard (but it has been blindly copied to other ports). If you are writing libraries, you should be using the machine module, because this way they will be compatible with all ports of MicroPython (as much as possible, anyways), and not just the single board which you happen to have.

On the software/hardware distinction, I don't know about OpenMV specifically, but on all other ports you can use both software implementation (on any pins), by specifying a negative id for the peripheral, or hardware (if available on the given port) by specifying a positive id (the pins then need to match what that particular peripheral is connected to).

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest