I2CSlave — Two wire serial protocol slave

class i2cslave.I2CSlave(scl, sda, addresses, smbus=False)

I2C is a two-wire protocol for communicating between devices. This implements the slave side.

  • scl (Pin) – The clock pin
  • sda (Pin) – The data pin
  • addresses (tuple) – The I2C addresses to respond to (how many is hw dependent).
  • smbus (bool) – Use SMBUS timings if the hardware supports it

Releases control of the underlying hardware so other classes can use it.


No-op used in Context Managers.


Automatically deinitializes the hardware on context exit. See Lifetime and ContextManagers for more info.


Wait for an I2C request from a master.

Parameters:timeout (float) – Timeout in seconds. Zero means wait forever, a negative value means check once
Returns:I2C Slave Request or None if timeout=-1 and there’s no request
Return type:I2CSlaveRequest

I2CSlaveRequest — I2C Slave Request

class i2cslave.I2CSlaveRequest(slave, address, is_read, is_restart)

I2C transfer request from a master. This cannot be instantiated directly, but is returned by I2CSlave.request().

  • slave (I2CSlave) – The I2C Slave receiving this request
  • address (int) – I2C address
  • is_read (bool) – I2C Master read request
  • is_restart (bool) – Repeated Start Condition

No-op used in Context Managers.


Close the request.


The I2C address of the request.


The I2C master is reading from the device.


Is Repeated Start Condition.

read(n=-1, ack=True)

Read data. If ack=False, the caller is responsible for calling I2CSlaveRequest.ack().

  • n (int) – Number of bytes to read (negative means all)
  • ack (bool) – Whether or not to send an ACK after the n’th byte

Bytes read

Return type:



Write the data contained in buffer.

Parameters:buffer (bytearray) – Write out the data in this buffer
Returns:Number of bytes written

Acknowledge or Not Acknowledge last byte received. Use together with I2CSlaveRequest.read() ack=False.

Parameters:ack (bool) – Whether to send an ACK or NACK