machine.I2C

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.
User avatar
cagiva
Posts: 22
Joined: Wed Dec 14, 2016 4:49 pm

machine.I2C

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

Is this the correct way to call the constructor for the machine.I2C module on the pyboard or is this only for the ESP8266?

MicroPython v1.8.7 on 2017-01-08; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> import machine
>>> i2c = machine.I2C(machine.Pin(5), machine.Pin(4))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert 'int' object to str implicitly
>>>

User avatar
deshipu
Posts: 1346
Joined: Thu May 28, 2015 5:54 pm

Re: machine.I2C

Post by deshipu » Wed Jan 18, 2017 4:04 pm

That module got changed recently (to make it work the same way on all platforms), and now you have to pass the peripheral id as the first argument. Pass a -1 for the software bit-banged implementation.

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

Re: machine.I2C

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

On the pyboard, you can choose to use hardware I2C or bitbanged SW I2C. To use hardware I2C then you should construct like this:

Code: Select all

i2c = machine.I2C(1)
To instead use SW I2C then you should construct like this:

Code: Select all

i2c = machine.I2C(-1, machine.Pin('X9'), machine.Pin('X10'))
although I prefer to identify the scl and sda pins expiicitly like this:

Code: Select all

i2c = machine.I2C(-1, scl=machine.Pin('X9'), sda=machine.Pin('X10'))
I'd recommend using the HW I2C unless you need all of the HW I2C pins for something else.

I tested the above using MicroPython 1.8.6

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

Re: machine.I2C

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

Thank you both for the clarification. That really helped.

I'm actually using the OpenMV board (M4); which doesn't use the latest Micropython firmware. Therefore, this is the constructor that works for me:

Code: Select all

i2c = I2C(sda=Pin('P5'), scl=Pin('P4'))
I'm trying to adapt Radomir's vl6180.py driver for the vl53l0x, but I'm getting this message "OSError: I2C bus error". Do I need to set the freq value in the constructor to a different value?

Code: Select all

def _set_reg8(self, address, value):
        data = ustruct.pack('>HB', address, value)
        self.i2c.writeto(self._address, data)
        
def init(self):
        self._set_reg8(0xFF, 0x01);   #OK
        self._set_reg8(0x00, 0x00);  #OK
        self._set_reg8(0xFF, 0x00);   #OK
        self._set_reg8(0x09, 0x00);  #OK
        self._set_reg8(0x10, 0x00);  #OK
        self._set_reg8(0x11, 0x00);  #OK
        self._set_reg8(0x24, 0x01);  #OK
        self._set_reg8(0x25, 0xFF);  #OK
        self._set_reg8(0x75, 0x00);  #OK
        self._set_reg8(0xFF, 0x01);  #OK
        self._set_reg8(0x4E, 0x2C);  #FAIL with "OSError: I2C bus error"

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

Re: machine.I2C

Post by dhylands » Wed Jan 18, 2017 5:03 pm

Possibly.

I also seem to recall that there were some issues with the SW I2C not supporting clock stretching properly (this has since been fixed).

User avatar
deshipu
Posts: 1346
Joined: Thu May 28, 2015 5:54 pm

Re: machine.I2C

Post by deshipu » Wed Jan 18, 2017 6:42 pm

Also the address is probably different. Use i2c.scan() to see.

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

Re: machine.I2C

Post by cagiva » Wed Jan 18, 2017 8:58 pm

It's also 0x29 (41 in hexadecimal is 0X29). I'm going to try to run it at a slower frequency (as Dave suggested) to see if that works.

Code: Select all

>>> from machine import I2C, Pin
>>> i2c = I2C(sda=Pin('P5'), scl=Pin('P4'))
>>> print(i2c.scan())
[41]
>>>

kpg
Posts: 6
Joined: Tue Jun 27, 2017 3:31 pm

Re: machine.I2C

Post by kpg » Tue Jun 27, 2017 4:05 pm

Hi guy, i really new in this type of development, but i am working on a small project with the vl53l0x sensor, and looking for info about it saw your forum, well i am using micropyhton to programming a esp8266 and vl53l0x, and i saw the Radomir Dopieralski has a library in micropython for this sensor, my questions is... Do have any documentation of how to use it?

Thank a lot

User avatar
deshipu
Posts: 1346
Joined: Thu May 28, 2015 5:54 pm

Re: machine.I2C

Post by deshipu » Fri Jun 30, 2017 9:00 am

There is no documentation yet, as this is work in progress and not finished, but you can use it with the following code:

Code: Select all

from machine import I2C, Pin
import vl53l0x

i2c = I2C(-1, Pin(5), Pin(4))
sensor = vl53l0x.VL53L0X(i2c)

distance = sensor.read()
print(distance)

kpg
Posts: 6
Joined: Tue Jun 27, 2017 3:31 pm

Re: machine.I2C

Post by kpg » Sat Jul 01, 2017 10:13 pm

Thanks, no problem for this sensor doesn´t exist information, and the documentation of ST is little confuse.

I was trying to run the library and i am getting this error

from machine import I2C, Pin
>>> import vl53l0x
>>> print(i2c.scan())
[41]
>>> i2c = I2C(-1, Pin(5), Pin(4))
>>> print(i2c)
<I2C>
>>> sensor = vl53l0x.VL53L0X(i2c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'VL53L0X'
>>

I am not sure if is the firmware version esp8266, actually i am using the last one.

MicroPython v1.9.1-8-g7213e78d on 2017-06-12; ESP module with ESP8266...

Post Reply