I2C — Two wire serial protocol

class busio.I2C(scl, sda, *, frequency=400000)

I2C is a two-wire protocol for communicating between devices. At the physical level it consists of 2 wires: SCL and SDA, the clock and data lines respectively.

See also

Using this class directly requires careful lock management. Instead, use I2CDevice to manage locks.

See also

Using this class to directly read registers requires manual bit unpacking. Instead, use an existing driver or make one with Register data descriptors.

Parameters:
  • scl (Pin) – The clock pin
  • sda (Pin) – The data pin
  • frequency (int) – The clock frequency in Hertz
  • timeout (int) – The maximum clock stretching timeut - only for bitbang
deinit()

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

__enter__()

No-op used in Context Managers.

__exit__()

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

scan()

Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of those that respond.

Returns:List of device ids on the I2C bus
Return type:list
try_lock()

Attempts to grab the I2C lock. Returns True on success.

Returns:True when lock has been grabbed
Return type:bool
unlock()

Releases the I2C lock.

readfrom_into(address, buffer, *, start=0, end=len(buffer))

Read into buffer from the slave specified by address. The number of bytes read will be the length of buffer. At least one byte must be read.

If start or end is provided, then the buffer will be sliced as if buffer[start:end]. This will not cause an allocation like buf[start:end] will so it saves memory.

Parameters:
  • address (int) – 7-bit device address
  • buffer (bytearray) – buffer to write into
  • start (int) – Index to start writing at
  • end (int) – Index to write up to but not include
writeto(address, buffer, *, start=0, end=len(buffer), stop=True)

Write the bytes from buffer to the slave specified by address. Transmits a stop bit if stop is set.

If start or end is provided, then the buffer will be sliced as if buffer[start:end]. This will not cause an allocation like buffer[start:end] will so it saves memory.

Writing a buffer or slice of length zero is permitted, as it can be used to poll for the existence of a device.

Parameters:
  • address (int) – 7-bit device address
  • buffer (bytearray) – buffer containing the bytes to write
  • start (int) – Index to start writing from
  • end (int) – Index to read up to but not include
  • stop (bool) – If true, output an I2C stop condition after the buffer is written