Page 1 of 1

Using machine.I2C

Posted: Sat Apr 08, 2017 4:19 am
by deshipu
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.

Re: Using machine.I2C

Posted: Sat Apr 08, 2017 12:45 pm
by kwagyeman
Do you have pull ups on the I/O pins?

Anyway, Ibrahim, can you take a look at this?

Re: Using machine.I2C

Posted: Sat Apr 08, 2017 1:43 pm
by iabdalkader
I think you meant to use P4 and P5 not PB4 and PB5.

Re: Using machine.I2C

Posted: Sat Apr 08, 2017 3:23 pm
by deshipu
You are right, that works perfectly, sorry!

Re: Using machine.I2C

Posted: Tue May 09, 2017 12:35 am
by kidswong999
Hello, what is the difference between machine.I2C and pyb.I2C?
why there are two class, but one thing? :?:

Re: Using machine.I2C

Posted: Tue May 09, 2017 1:01 am
by kwagyeman
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.

Re: Using machine.I2C

Posted: Tue May 09, 2017 4:10 am
by deshipu
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).