View Source Circuits.I2C (circuits_i2c v1.2.2)

Circuits.I2C lets you communicate with hardware devices using the I2C protocol.

Link to this section Summary

Types

I2C device address

I2C bus

Function to report back whether a device is present

Functions

Return a list of available I2C bus names. If nothing is returned, it's possible that the kernel driver for that I2C bus is not enabled or the kernel's device tree is not configured. On Raspbian, run raspi-config and look in the advanced options.

close the I2C bus

Convenience method to scan all I2C buses for devices

Scan the I2C bus for devices by performing a read at each device address and returning a list of device addresses that respond.

Return whether a device is present

Scan all I2C buses for one or more devices

Scans all I2C buses for one specific device

Return info about the low level I2C interface

Open an I2C bus

Initiate a read transaction to the I2C device at the specified address

Initiate a read transaction and raise on error

Write data to the I2C device at address.

Write data to the I2C device at address and raise on error

Write data to an I2C device and then immediately issue a read.

Write data to an I2C device and then immediately issue a read. Raise on errors.

Link to this section Types

@type address() :: 0..127

I2C device address

This is a "7-bit" address for the device. Some devices specify an "8-bit" address in their documentation. You can tell if you have an "8-bit" address if it's greater than 127 (0x7f) or if the documentation talks about different read and write addresses. If you have an 8-bit address, divide it by 2.

@type bus() :: reference()

I2C bus

Call open/1 to obtain an I2C bus reference and then pass it to the read and write functions for interacting with devices.

@type opt() :: {:retries, non_neg_integer()}
@type present?() :: (bus(), address() -> boolean())

Function to report back whether a device is present

See discover/2 for how a custom function can improve device detection when the type of device being looked for is known.

Link to this section Functions

@spec bus_names() :: [binary()]

Return a list of available I2C bus names. If nothing is returned, it's possible that the kernel driver for that I2C bus is not enabled or the kernel's device tree is not configured. On Raspbian, run raspi-config and look in the advanced options.

iex> Circuits.I2C.bus_names()
["i2c-1"]
@spec close(bus()) :: :ok

close the I2C bus

@spec detect_devices() :: :"do not show this result in output"

Convenience method to scan all I2C buses for devices

This is only intended to be called from the IEx prompt. Programs should use detect_devices/1.

@spec detect_devices(bus() | binary()) :: [address()] | {:error, term()}

Scan the I2C bus for devices by performing a read at each device address and returning a list of device addresses that respond.

WARNING: This is intended to be a debugging aid. Reading bytes from devices can advance internal state machines and might cause them to get out of sync with other code.

iex> Circuits.I2C.detect_devices("i2c-1")
[4]

The return value is a list of device addresses that were detected on the specified I2C bus. If you get back 'Hh' or other letters, then IEx converted the list to an Erlang string. Run i v() to get information about the return value and look at the raw string representation for addresses.

If you already have a reference to an open device, then you may pass its reference to detect_devices/1 instead.

Link to this function

device_present?(bus, address)

View Source
@spec device_present?(bus(), address()) :: boolean()

Return whether a device is present

This function performs a simplistic check for an I2C device on the specified bus and address. It's not perfect, but works enough to be useful. Be warned that it does perform an I2C read on the specified address and this may cause some devices to actually do something.

Link to this function

discover(possible_addresses, present? \\ &device_present?/2)

View Source
@spec discover([address()], present?()) :: [{binary(), address()}]

Scan all I2C buses for one or more devices

This function takes a list of possible addresses and an optional detection function. It only scans addresses in the possible addresses list to avoid disturbing unrelated I2C devices.

If a detection function is not passed in, a default one that performs a simple read and checks whether it succeeds is used. If the desired device has an ID register or other means of identification, the optional function should try to query that. If passing a custom function, be sure to return false rather than raise if there are errors.

A list of bus name and address tuples is returned. The list may be empty.

See also discover_one/2.

Link to this function

discover_one(possible_addresses, present? \\ &device_present?/2)

View Source
@spec discover_one([address()], present?()) ::
  {:ok, {binary(), address()}}
  | {:error, :not_found | :multiple_possible_matches}

Scans all I2C buses for one specific device

This function and discover_one!/2 are convenience functions for the use case of helping a user find a specific device. They both call discover/2 with a list of possible I2C addresses and an optional function for checking whether the device is present.

This function returns an :ok or :error tuple depending on whether one and only one device was found. See discover_one!/2 for the raising version.

Link to this function

discover_one!(possible_addresses, present? \\ &device_present?/2)

View Source
@spec discover_one!([address()], present?()) :: {binary(), address()}

Same as discover_one/2 but raises on error

@spec info() :: map()

Return info about the low level I2C interface

This may be helpful when debugging I2C issues.

@spec open(binary() | charlist()) :: {:ok, bus()} | {:error, term()}

Open an I2C bus

I2C bus names depend on the platform. Names are of the form "i2c-n" where the "n" is the bus number. The correct bus number can be found in the documentation for the device or on a schematic. Another option is to call Circuits.I2C.bus_names/0 to list them for you.

I2c buses may be opened more than once. There is no need to share an I2C bus reference between modules.

On success, this returns a reference to the I2C bus. Use the reference in subsequent calls to read and write I2C devices

Link to this function

read(i2c_bus, address, bytes_to_read, opts \\ [])

View Source
@spec read(bus(), address(), pos_integer(), [opt()]) ::
  {:ok, binary()} | {:error, term()}

Initiate a read transaction to the I2C device at the specified address

Options:

  • :retries - number of retries before failing (defaults to no retries)
Link to this function

read!(i2c_bus, address, bytes_to_read, opts \\ [])

View Source
@spec read!(bus(), address(), pos_integer(), [opt()]) :: binary()

Initiate a read transaction and raise on error

Link to this function

write(i2c_bus, address, data, opts \\ [])

View Source
@spec write(bus(), address(), iodata(), [opt()]) :: :ok | {:error, term()}

Write data to the I2C device at address.

Options:

  • :retries - number of retries before failing (defaults to no retries)
Link to this function

write!(i2c_bus, address, data, opts \\ [])

View Source
@spec write!(bus(), address(), iodata(), [opt()]) :: :ok

Write data to the I2C device at address and raise on error

Options:

  • :retries - number of retries before failing (defaults to no retries)
Link to this function

write_read(i2c_bus, address, write_data, bytes_to_read, opts \\ [])

View Source
@spec write_read(bus(), address(), iodata(), pos_integer(), [opt()]) ::
  {:ok, binary()} | {:error, term()}

Write data to an I2C device and then immediately issue a read.

This function is useful for devices that want you to write the "register" location to them first and then issue a read to get its contents. Many devices operate this way and this function will issue the commands back-to-back on the I2C bus. Some I2C devices actually require that the read immediately follows the write. If the target supports this, the I2C transaction will be issued that way. On the Raspberry Pi, this can be enabled globally with File.write!("/sys/module/i2c_bcm2708/parameters/combined", "1")

Options:

  • :retries - number of retries before failing (defaults to no retries)
Link to this function

write_read!(i2c_bus, address, write_data, bytes_to_read, opts \\ [])

View Source
@spec write_read!(bus(), address(), iodata(), pos_integer(), [opt()]) :: binary()

Write data to an I2C device and then immediately issue a read. Raise on errors.

Options:

  • :retries - number of retries before failing (defaults to no retries)