I2C Device Library (i2cdevlib)
repository·master·Indexed 26 days ago
https://github.com/jrowberg/i2cdevlibA collection of uniform classes providing intuitive interfaces for various I2C devices. It uses a layered architecture with a central I2Cdev class to abstract low-level communication, enabling porting across hardware platforms including Arduino, ESP-IDF, Raspberry Pi Pico (RP2040), STM32, PIC18, dsPIC30F, and Jennic. Supported devices include the MPU6050, MPU-9250, BMP180, BMP085, and HMC5883L.
What's inside i2cdevlib
- The I2C Device Library for Jennic is a peripheral library designed for use with JenOS. It provides support for I2C devices, specifically targeting the MPU6050 sensor.
Overview of I2C Device Library architecture
masterThe
i2cdevlibprovides a uniform interface for I2C devices using a layered architecture:I2Cdevclass: A generic class that abstracts I2C bit- and byte-level communication. It is designed to be used statically, meaning you only need one instance for multiple devices in a project, which reduces memory overhead. It also supports passing non-defaultWireobjects in Arduino to allow using multiple I2C transceivers simultaneously.- Device Classes: Specific classes (e.g.,
MPU6050) that build uponI2Cdev. These classes provide complete coverage of the device's functionality as described in its datasheet and include convenience functions.
This abstraction allows you to port I2C communication to different platforms (Arduino, PIC, MSP430, etc.) by only modifying the
I2Cdevimplementation.Overview of the IMU_10DOF library
masterThe
IMU_10DOFlibrary is designed for the Grove - IMU 10DOF and Xadow - IMU 10DOF modules. These modules combine an MPU-9250 (Inertial Measurement Unit) and a BMP180 (Barometric Pressure Sensor).The library is a composite of two primary components:
- MPU9250 Library: Provides functionality for the MPU-9250 sensor.
- BMP180 Library: Provides functionality for the BMP180 sensor.
Use the I2Cdev library for PIC18
masterThe I2Cdev library for PIC18 is designed to work with Microchip's XC8 compiler peripheral libraries (
_plib_). Note that read timeout functionality is not implemented in this version.Currently, the library supports the MPU6050 sensor, but only for reading raw data; DMP (Digital Motion Processor) functions are not supported. To get started, you can use the provided MPLABX project example to see how to read raw data from the MPU.
Supported Devices in STM32 HAL Port
masterThe STM32 HAL port of the I2C Device Library currently supports the following devices (optimized for GY-87 boards):
- MPU6050 (including DMP support)
- HMC5883L
- BMP180 / BMP085
Note: ADXL345 support is planned.
Supported STM32 Devices
masterThe current STM32 implementation of this library supports the following devices:
- MPU6050 (without DMP)
- HMC5883 (Note: HMC5983 may also work as register differences were not identified).
Other devices can be ported to this library.
Use the dsPIC30F I2C Device Library
masterThe dsPIC30F implementation of the I2C device library is currently under construction. It utilizes the XC16 peripheral libraries (_plib_) for I2C communication. Note that read timeout functionality is not currently implemented.Use BMP085/BMP180 with STM32 HAL
masterTo use the BMP085/BMP180 sensor with an STM32 microcontroller using the HAL library, follow these steps:
- Configure I2C Hardware: Set up your GPIO pins (e.g., SDA and SCL) in Alternate Function Open Drain mode with pull-ups.
- Initialize HAL I2C: Initialize the
I2C_HandleTypeDefstructure with your desired clock speed (e.g., 400,000 Hz). - Initialize i2cdevlib: Call
I2Cdev_init(&hi2c_handle)to link the library to your STM32 I2C instance. - Sensor Setup: Verify connection with
BMP085_testConnection()and initialize withBMP085_initialize(). - Data Acquisition:
- Set the mode using
BMP085_setControl(mode). - Wait for the required measurement time using
BMP085_getMeasureDelayMilliseconds(mode). - Retrieve data using
BMP085_getTemperatureC(),BMP085_getPressure(), orBMP085_getAltitude(pressure, sea_level_pressure).
- Set the mode using
#include "stm32f4xx.h" #include "stm32f4xx_hal.h" #include <stdint.h> #include <stdio.h> #include <string.h> #include "I2Cdev.h" #include "BMP085.h" I2C_HandleTypeDef hi2c3; int main(void) { SystemInit(); HAL_Init(); // ... GPIO and I2C3 Configuration ... HAL_I2C_Init(&hi2c3); I2Cdev_init(&hi2c3); while(!BMP085_testConnection()) ; BMP085_initialize(); while (1) { // Temperature BMP085_setControl(BMP085_MODE_TEMPERATURE); HAL_Delay(BMP085_getMeasureDelayMilliseconds(BMP085_MODE_TEMPERATURE)); float t = BMP085_getTemperatureC(); // Pressure BMP085_setControl(BMP085_MODE_PRESSURE_3); HAL_Delay(BMP085_getMeasureDelayMilliseconds(BMP085_MODE_PRESSURE_3)); float p = BMP085_getPressure(); // Altitude float a = BMP085_getAltitude(p, 101325); } }Build MPU6050 examples for Raspberry Pi Pico
masterTo build the MPU6050 examples for the Raspberry Pi Pico, follow these steps:
- Navigate to the specific example folder you wish to build.
- Dependency Management: If the library files (
I2Cdev.h,I2Cdev.cpp,MPU6050.h,MPU6050.cpp,MPU6050_6Axis_MotionApps_V6_12.h,helper_3dmath.h) are not in a common include directory, copy them into your example folder or modifyCMakeLists.txtto specify the correct path. - Pico W Users: If using a PICO W (with Infineon CYW43439 wireless chip), you must uncomment 3 lines in the
CMakeLists.txtfile. - Run the build commands:
mkdir build && cd build cmake .. make - Copy the resulting
.uf2file to your Pico board.
Implement I2Cdev for STM32 using Keil MDK Pack
masterTo implement
I2Cdevon STM32, use the Keil MDK Pack. This approach provides better compatibility than HAL/SPL, allowingI2Cdevto work with any device listed on the MDK Pack software page.To set it up:
- Download the appropriate pack for your specific STM32 device.
- Enable your device driver.
- Specify the peripheral you are using in
I2Cdev.h.
Monitor serial output from Raspberry Pi Pico
masterAfter flashing the
.uf2file to your Pico, you can monitor the serial output via USB.On Linux: Use
minicomwithsudoto ensure the device opens correctly:sudo minicom -D /dev/ttyACM0On Windows: Use PuTTY and select the COM port assigned to your device (verify this in the Device Manager). Set the baudrate to
115200.Calibrate the MPU6050 sensor
masterFor accurate sensor readings, you should calibrate the gyro and accelerometer offsets. You can do this in two ways within your code (e.g., in
mpu6050_DMP_port.cpp) after callingmpu.dmpInitialize():- Manual Offset Setting: Use the specific offset methods (e.g.,
mpu.setXAccelOffset()) with values obtained from a calibration example. - Automated Calibration: Call the calibration methods directly in your setup code. Running them for 6 loops is generally sufficient.
Methods:
mpu.CalibrateAccel(6)mpu.CalibrateGyro(6)
- Manual Offset Setting: Use the specific offset methods (e.g.,