The Pico I2C Device Library provides a straightforward interface for communicating with I2C devices using the Raspberry Pi Pico. It supports both read and write operations on I2C devices, making it easy to interface with a wide range of I2C-enabled components.
- Initialize I2C communication with configurable baud rate, interface and GPIO pins.
- Perform read and write operations to interact with I2C devices.
Add the Pico I2C Device Library as a submodule in your project.
git submodule add https://github.com/julienfdev/pico-i2c-device.git lib/pico-i2c-device
Modify your CMakeLists.txt to include the Pico I2C Device Library.
-
After the
pico_sdk_init
calladd_subdirectory(lib/pico-i2c-device)
-
Add the library to your project
target_link_libraries(your_project pico_i2c_device)
To use the I2C Device library, create an instance of the I2CDevice class by specifying the I2C address, I2C interface, baud rate, and the GPIO pins for SDA and SCL. Only the first parameter is required, and the rest have default values: i2c0
, 100000
and {4, 5}
.
#include "i2c_device.h"
int main() {
// I2C address 0x68, default I2C0, default baudrate 100000, pins 4 (SDA) and 5 (SCL)
I2CDevice device(0x68);
return 0;
}
To write data to a specific register of the I2C device, use the write method.
uint8_t data[2] = { 0x01, 0x02 };
device.write(0x10, data, 2); // Write 2 bytes to register 0x10
To read data from a specific register of the I2C device, use the read method.
uint8_t result[4];
device.read(0x20, result, 4); // Read 4 bytes from register 0x20
You can customize the I2C pins and baud rate when creating an instance of the I2CDevice class. Please check the pinout diagram to make sure the selected pins are valid for I2C communication based on the interface you provided (or i2c0 by default).
I2CPins custom_pins = { 8, 9 }; // SDA = GPIO 8, SCL = GPIO 9
I2CDevice custom_device(0x68, i2c0, 400000, custom_pins); // I2C0, 400kHz baudrate, custom pins
This project is licensed under the MIT License. See the LICENSE file for more details.