Skip to content
Boris Lovosevic edited this page Mar 3, 2018 · 16 revisions

Editing in progress...

machine Module

Class I2C


This class includes full support for using ESP32 I2C peripheral
Both master and slave modes are supported.



Create the I2C instance object

i2c = machine.I2C(id, mode, speed, sda, scl, slave_addr, slave_bufflen)

Argument Description
id The hardware I2C peripheral ID; 0 or 1 can be used
Default: 0
mode I2C interface mode; master or slave
Use the constants machine.I2C.MASTER or machine.I2C.SLAVE
Default: master
speed
freq
I2C clock frequency in Hz
Default: 100000
sda I2C sda pin; can be given as integer gpio number or Pin object
scl I2C scl pin; can be given as integer gpio number or Pin object
slave_address I2C slave address to be assigned to this i2c interface.
Only used if SLAVE mode is selected
7-bit address, do not use reserved adresses 0x00-0x07 & 0x78-0x7F

Default: 32 (0x20)
slave_bufflen Size of slave buffer used for master<->slave comunication in bytes;
Range: 256 - 4096
Default: 256
Only used if SLAVE mode is selected

Only sda and scl are required, all the others are optional and will be set to the default values if not given.

i2c = machine.I2C(0, sda=21, scl=22)
si2c = machine.I2C(1, mode=machine.I2C.SLAVE, sda=25, scl=26, slave_bufflen=512)



i2c.deinit()

Deinitialize the I2C object, free all used resources.

i2c.scan()

Scan for i2c devices on I2C bus.
Returns the list of detected addresses.
Can only be used in master mode.

į2c.readfrom(addr, nbytes)

Read nbytes bytes from i2c device with address addr.
Bytearray of read bytes is returned.
Can only be used in master mode.

į2c.readfrom_into(addr, buf)

Read from i2c device with address addr into buffer object buf.
Size of buf bytes are read.
Can only be used in master mode.

i2c.writeto(addr, buf [,stop=True])

Write the content of the buffer object buf to the i2c device with address adr
If optional stop argument is set to False, the stop signal is not issued.
Can only be used in master mode.

į2c.readfrom_mem(addr, memaddr, n, adrlen, stop)

Argument Description
addr i2c device address
memaddr memory address to be wtitten before read
n number of bytes to read
adrlen optional; number of addres bytes to write, 1 - 4
If not given, number of bytes to send is determined from the memaddr value
stop optional; Default: True
If True, stop signal is issued after address write
If False, repeated start signal is issued after address write

Write the address to the i2c device with address addr, then read n bytes from it.
Bytearray of read bytes is returned.
Can only be used in master mode.

į2c.readfrom_mem_into(addr, memaddr, buf, adrlen, stop)

Argument Description
addr i2c device address
memaddr memory address to be wtitten before read
buf Buffer object to read into
adrlen optional; number of addres bytes to write, 1 - 4
If not given, number of bytes to send is determined from the memaddr value
stop optional; Default: True
If True, stop signal is issued after address write
If False, repeated start signal is issued after address write

Write the address to the i2c device with address addr, then read from itinto buffer object buf.
Size of buf bytes are read.
Can only be used in master mode.

i2c.writeto_mem(addr, memaddr, buf, adrlen)

Argument Description
addr i2c device address
memaddr memory address to be wtitten before read
buf Buffer object to write from
adrlen optional; number of addres bytes to write, 1 - 4
If not given, number of bytes to send is determined from the memaddr value

Write the address to the i2c device with address addr, then write the content of the buffer object buf to the device
Can only be used in master mode.


SLAVE mode

The I2C device can be configured to run in slave mode

The master <-> slave communication is performed via the slave bufer, size of which is configured on creating the I2C object.
The master can perform three kinds of operations with MicroPython I2C slave:

  • read from slave buffer issuing the following command sequence:
    • write the 2-byte address & 2-byte length to MicroPython I2C slave. The address range and lenght must be set according to MicroPython I2C slave buffer size
    • read length bytes from MicroPython I2C slave
  • write to the slave buffer issuing the following command sequence:
    • write 2-byte address following with data to MicroPython I2C slave.
      The address range must be from 0x1000 to 0x1000 + slave_buffer_size - 1
      The 0x1000 address offset is used to distingush the read and write operations.
  • send command with optional data to the slave
    • The master can send any command in range 0x20 to 0xFF to the MicroPython I2C slave.
      This command (and optional data) can be anylized in callback function and some action taken

The content of the slave buffer san be set or read from MicroPython at any time.

Separate callback functions can be defined for read, write and command events.

i2c.setdata(buf, addr)

Set the content of the slave buffer at address addr from buffer object buf
Can only be used in slave mode.

i2c.getdata(addr, length)

Get the length bytes from the slave buffer at address addr
Bytearray of read bytes is returned.
Can only be used in slave mode.

i2c.slavewrite(buf)

Write the content of the buffer object buf to the I2C slave transimt buffer
The master must ready to read that data.
Can be used, for example, from command callback function as a response to some master command.
Can only be used in slave mode.

i2c.callback(type, func)

Can only be used in slave mode.

Clone this wiki locally