Page 1 of 2

machine.I2C

Posted: Wed Jan 18, 2017 8:07 am
by cagiva
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
>>>

Re: machine.I2C

Posted: Wed Jan 18, 2017 4:04 pm
by deshipu
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.

Re: machine.I2C

Posted: Wed Jan 18, 2017 4:08 pm
by dhylands
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

Re: machine.I2C

Posted: Wed Jan 18, 2017 4:59 pm
by cagiva
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"

Re: machine.I2C

Posted: Wed Jan 18, 2017 5:03 pm
by dhylands
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).

Re: machine.I2C

Posted: Wed Jan 18, 2017 6:42 pm
by deshipu
Also the address is probably different. Use i2c.scan() to see.

Re: machine.I2C

Posted: Wed Jan 18, 2017 8:58 pm
by cagiva
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]
>>>

Re: machine.I2C

Posted: Tue Jun 27, 2017 4:05 pm
by kpg
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

Re: machine.I2C

Posted: Fri Jun 30, 2017 9:00 am
by deshipu
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)

Re: machine.I2C

Posted: Sat Jul 01, 2017 10:13 pm
by kpg
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...