# I2C Bus Overview
I2C is a packet-switched serial communication protocol that allows multiple master devices to connect to multiple slave devices using only 2 wires per connection. It is intended for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication.
Pixhawk/PX4 support it for:
- Connecting off board components that require greater data rates than provided by a strict serial UART: e.g. rangefinders.
- Compatibility with peripheral devices that only support I2C.
- Allowing multiple devices to attach to a single bus (useful for conserving ports). For example, LEDs, Compass, rangefinders etc.
TIP
IMUs (accelerometers/gyroscopes) should not be attached via I2C (typically the SPI (opens new window) bus is used). The bus is not fast enough even with a single device attached to allow vibration filtering (for instance), and the performance degrades further with every additional device on the bus.
# Integrating I2C Devices
Drivers should #include <drivers/device/i2c.h>
and then provide an implementation of the abstract base class I2C
defined in I2C.hpp for the target hardware (i.e. for NuttX here (opens new window)).
A small number of drivers will also need to include headers for their type of device (drv_*.h) in /src/drivers/ (opens new window) - e.g. drv_airspeed.h (opens new window).
To include a driver in firmware you must add the driver to the board-specific cmake file that corresponds to the target you want to build for. You can do this for a single driver:
drivers/distance_sensor/lightware_laser_i2c
You can also include all drivers of a particular type.
distance_sensor # all available distance sensor drivers
TIP
For example, you can see/search for distance_sensor
in the px4_fmu-v4_default (opens new window) configuration.
# I2C Driver Examples
To find I2C driver examples, search for i2c.h in /src/drivers/ (opens new window).
Just a few examples are:
- drivers/distance_sensor/lightware_laser_i2c (opens new window) - I2C driver for Lightware SF1XX LIDAR.
- drivers/distance_sensor/lightware_laser_serial (opens new window) - Serial driver for Lightware SF1XX LIDAR.
- drivers/ms5611 (opens new window) - I2C Driver for the MS5611 and MS6507 barometric pressure sensor connected via I2C (or SPI).
# Further Information
- I2C (opens new window) (Wikipedia)
- I2C Comparative Overview (opens new window) (learn.sparkfun.com)
- Driver Framework