You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/hardware/06.nicla/boards/nicla-vision/tutorials/user-manual/content.md
+19-23Lines changed: 19 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -1370,62 +1370,58 @@ The example code above should output this:
1370
1370
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:
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.
1378
1378
1379
1379
1380
1380
#### With OpenMV
1381
1381
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.
1383
1383
1384
1384
```python
1385
-
frompybimportI2C
1385
+
frommachineimportI2C
1386
1386
```
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.
1388
1388
1389
1389
```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
1395
1391
```
1396
1392
1397
-
The basic methods are `send` and `recv` implemented as follows:
1393
+
The basic methods are `writeto` and `readfrom` implemented as follows:
1398
1394
1399
1395
```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
+
```
1402
1400
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:
1406
1402
1407
-
data = i2c.recv(3) # receive 3 bytes
1403
+
```python
1404
+
i2c.scan() # scan for peripherals, returning a list of 7-bit addresses
1408
1405
```
1409
1406
1410
1407
This is a simple example showing how to send data over I2C from a master to a slave.
1411
1408
1412
1409
```python
1413
-
frompybimportI2C
1410
+
frommachineimportI2C
1414
1411
1415
-
i2c = I2C(1, I2C.MASTER)
1412
+
i2c = I2C(scl="PB8",sda="PB9",freq=400000) # create I2C peripheral at frequency of 400kHz
1416
1413
1417
1414
buf =bytearray(2)
1418
-
1419
1415
buf[0] =0x00
1420
1416
buf[1] =0xFA
1421
1417
1422
-
i2c.send(buf, 0x35)
1418
+
i2c.writeto(0x35, buf)
1423
1419
```
1424
1420
The output data should look like the image below, where we can see the device address data frame:
1425
1421
1426
1422

1427
1423
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).
1429
1425
1430
1426
#### With Arduino IDE
1431
1427
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