Skip to content

Commit 4bbd39a

Browse files
committed
I2C fixed
1 parent 89c2c2b commit 4bbd39a

File tree

1 file changed

+19
-23
lines changed
  • content/hardware/06.nicla/boards/nicla-vision/tutorials/user-manual

1 file changed

+19
-23
lines changed

content/hardware/06.nicla/boards/nicla-vision/tutorials/user-manual/content.md

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,62 +1370,58 @@ The example code above should output this:
13701370
The Nicla Vision supports I2C communication, which allows data transmission between the board and other I2C-compatible devices. The pins used in the Nicla Vision for the I2C communication protocol are the following:
13711371

13721372
| **Microcontroller Pin** | **Arduino Pin Mapping** |
1373-
|:-----------------------:|:-----------------------:|
1374-
| PB_8 | I2C_SCL or 12 |
1375-
| PB_9 | I2C_SDA or 11 |
1373+
| :---------------------: | :---------------------: |
1374+
| PB_8 / SCL | PB8 |
1375+
| PB_9 / SDA | PB9 |
13761376

13771377
Please, refer to the [board pinout section](#pinout) of the user manual to localize them on the board. The I2C pins are also available through the onboard ESLOV connector of the Nicla Vision.
13781378

13791379

13801380
#### With OpenMV
13811381

1382-
To use I2C communication with OpenMV, import `I2C` from the `pyb` module as follows.
1382+
To use I2C communication with OpenMV, import `I2C` from the `machine` module as follows.
13831383

13841384
```python
1385-
from pyb import I2C
1385+
from machine import I2C
13861386
```
1387-
Create the I2C objects and initialize them attached to a specific bus, below are some of the available commands to do it.
1387+
Create the I2C object and initialize it attached to defined pins, below is the available command to do it.
13881388

13891389
```python
1390-
i2c = I2C(1) # create on bus 1
1391-
i2c = I2C(1, I2C.MASTER) # create and init as a master
1392-
i2c.init(I2C.MASTER, baudrate=20000) # init as a master
1393-
i2c.init(I2C.SLAVE, addr=0x42) # init as a slave with given address
1394-
i2c.deinit() # turn off the peripheral
1390+
i2c = I2C(scl="PB8",sda="PB9",freq=400000) # create I2C peripheral at frequency of 400kHz on defined pins
13951391
```
13961392

1397-
The basic methods are `send` and `recv` implemented as follows:
1393+
The basic methods are `writeto` and `readfrom` implemented as follows:
13981394

13991395
```python
1400-
# For Masters / Controllers (must include addr in send)
1401-
i2c.send('abc', 0x42) # send 3 bytes to device on address 0x42
1396+
i2c.writeto(42, b'123') # write 3 bytes to peripheral with 7-bit address 42
1397+
1398+
i2c.readfrom(42, 4) # read 4 bytes from peripheral with 7-bit address 42
1399+
```
14021400

1403-
# For Slaves / Peripherals
1404-
i2c.send('abc') # send 3 bytes
1405-
i2c.send(0x42) # send a single byte, given by the number
1401+
You can scan for peripherals with the following function:
14061402

1407-
data = i2c.recv(3) # receive 3 bytes
1403+
```python
1404+
i2c.scan() # scan for peripherals, returning a list of 7-bit addresses
14081405
```
14091406

14101407
This is a simple example showing how to send data over I2C from a master to a slave.
14111408

14121409
```python
1413-
from pyb import I2C
1410+
from machine import I2C
14141411

1415-
i2c = I2C(1, I2C.MASTER)
1412+
i2c = I2C(scl="PB8",sda="PB9",freq=400000) # create I2C peripheral at frequency of 400kHz
14161413

14171414
buf = bytearray(2)
1418-
14191415
buf[0] = 0x00
14201416
buf[1] = 0xFA
14211417

1422-
i2c.send(buf, 0x35)
1418+
i2c.writeto(0x35, buf)
14231419
```
14241420
The output data should look like the image below, where we can see the device address data frame:
14251421

14261422
![I2C output data](assets/i2c.png)
14271423

1428-
To learn more about the I2C class on MicroPython, continue [here](https://docs.openmv.io/library/pyb.I2C.html).
1424+
To learn more about the I2C class on MicroPython, continue [here](https://docs.micropython.org/en/latest/library/machine.I2C.html).
14291425

14301426
#### With Arduino IDE
14311427
To use I2C communication, include the `Wire` library at the top of your sketch. The `Wire` library provides functions for I2C communication:

0 commit comments

Comments
 (0)