I2C — Two wire serial protocol

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

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.

Parameters:
  • scl (Pin) – The clock pin
  • sda (Pin) – The data pin
  • frequency (int) – The clock frequency of the bus
  • timeout (int) – The maximum clock stretching timeout in microseconds
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. A device responds if it pulls the SDA line low after its address (including a read bit) is sent on the bus.

try_lock()

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

unlock()

Releases the I2C lock.

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

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=None, stop=True)

Write the bytes from buffer to the slave specified by address and then transmits a stop bit. Use writeto_then_readfrom when needing a write, no stop and repeated start before a 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 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. Deprecated. Will be removed in 6.x and act as stop=True.
writeto_then_readfrom(address, out_buffer, in_buffer, *, out_start=0, out_end=None, in_start=0, in_end=None)

Write the bytes from out_buffer to the slave specified by address, generate no stop bit, generate a repeated start and read into in_buffer. out_buffer and in_buffer can be the same buffer because they are used sequentially.

If start or end is provided, then the corresponding 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
  • out_buffer (bytearray) – buffer containing the bytes to write
  • in_buffer (bytearray) – buffer to write into
  • out_start (int) – Index to start writing from
  • out_end (int) – Index to read up to but not include. Defaults to len(buffer)
  • in_start (int) – Index to start writing at
  • in_end (int) – Index to write up to but not include. Defaults to len(buffer)