Skip to main content
Read, write, and scan I2C (Inter-Integrated Circuit) devices connected to a Lager Box.

Import

Methods

Method Reference

Net.get(name, type=NetType.I2C)

Get an I2C net by name.
Parameters: Returns: I2C Net instance

config(frequency_hz, pull_ups)

Configure I2C bus parameters. Only explicitly-provided parameters are changed; omitted parameters retain their stored values.

scan(start_addr, end_addr)

Scan the I2C bus for connected devices.
Returns: list[int] - List of 7-bit addresses that responded with ACK

read(address, num_bytes, output_format, overrides)

Read bytes from an I2C device.
Returns: list[int] - Received bytes as integers (when output_format="list")

write(address, data, overrides)

Write bytes to an I2C device.

write_read(address, data, num_bytes, output_format, overrides)

Write then read in a single I2C transaction using a repeated start condition. This is the standard pattern for reading device registers.
Returns: list[int] - Received bytes as integers (when output_format="list")

get_config()

Get the raw net configuration dictionary.
Returns: dict - Full net configuration including name, role, instrument, and params

Output Formats

The output_format parameter on read() and write_read() controls how data is returned:

Examples

Basic Device Read

Register Read/Write

Bus Configuration

Multi-Device Setup

Per-Call Configuration Override

An override changes the net’s driver for the rest of the script, not only the one call. Set the value back with config() when the next transaction needs the old one. See #848.

Supported Hardware

Multi-channel FTDI adapters

An FTDI net takes its channel from params.interface on the net record, accepting A-D or 0-3. Set it with lager nets add --interface. A net with no interface uses channel A, the only choice on a single-channel FT232H. I2C runs over the FTDI part’s MPSSE engine, and on an FT4232H only channels A and B have one. lager nets add refuses C or D for an I2C net. A net record edited by hand with C or D fails when the net opens, and the error names the channel. See Nets for how channels are assigned across net types on one chip.

LabJack U3

  • I2C on a U3 needs U3 hardware version 1.21 or later. The U3 is a bus master only.
  • The U3 has no pull-up resistors. Connect external resistors from SDA and SCL to VS; LabJack recommends 4.7 kΩ. Without them, every address sends a NAK and scan() returns an empty list.
  • pull_ups has no effect on a U3.
  • A transaction writes at most 50 bytes and reads at most 52 bytes. A larger transaction raises an error.
  • The clock runs from about 10 kHz to 150 kHz, and it is never faster than frequency_hz. A request outside that range is clamped, with a warning on the standard error output of the script.
  • write() and write_read() check the acknowledgement of the address and of each byte. The error names the first byte that the device refused. For a write of 31 bytes or more, the error does not name the byte.
  • FIO0-FIO3 cannot carry I2C. The driver raises an error for a net that uses them.
See I2C for the pins and for how to create a U3 net with other pins.

Notes

  • Net must be configured as NetType.I2C
  • Addresses are 7-bit format (0x00-0x7F), not left-shifted
  • pull_ups only works on the Aardvark adapter; ignored on LabJack T7 and LabJack U3
  • write_read() uses a repeated start condition for atomic register reads
  • Default scan range (0x08-0x77) skips reserved addresses
  • Configuration changes persist to saved_nets.json for subsequent commands
  • LabJack T7 maps the requested frequency to a clock throttle, from about 130 Hz to 450 kHz
  • LabJack U3 runs from about 10 kHz to 150 kHz