I2C Device Library (i2cdevlib)

repository·master·Indexed 26 days ago

https://github.com/jrowberg/i2cdevlib

A 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.

Tokens
2.4K
Snippets
3
Records
17
Agent score
88%

What's inside i2cdevlib

  1. Overview of I2C Device Library architecture

    master

    The i2cdevlib provides a uniform interface for I2C devices using a layered architecture:

    • I2Cdev class: 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-default Wire objects in Arduino to allow using multiple I2C transceivers simultaneously.
    • Device Classes: Specific classes (e.g., MPU6050) that build upon I2Cdev. 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 I2Cdev implementation.

  2. Overview of the IMU_10DOF library

    master

    The IMU_10DOF library 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:

    1. MPU9250 Library: Provides functionality for the MPU-9250 sensor.
    2. BMP180 Library: Provides functionality for the BMP180 sensor.
  3. Use the I2Cdev library for PIC18

    master

    The 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.

  4. Use BMP085/BMP180 with STM32 HAL

    master

    To use the BMP085/BMP180 sensor with an STM32 microcontroller using the HAL library, follow these steps:

    1. Configure I2C Hardware: Set up your GPIO pins (e.g., SDA and SCL) in Alternate Function Open Drain mode with pull-ups.
    2. Initialize HAL I2C: Initialize the I2C_HandleTypeDef structure with your desired clock speed (e.g., 400,000 Hz).
    3. Initialize i2cdevlib: Call I2Cdev_init(&hi2c_handle) to link the library to your STM32 I2C instance.
    4. Sensor Setup: Verify connection with BMP085_testConnection() and initialize with BMP085_initialize().
    5. 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(), or BMP085_getAltitude(pressure, sea_level_pressure).
    #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);
        }
    }
  5. Build MPU6050 examples for Raspberry Pi Pico

    master

    To build the MPU6050 examples for the Raspberry Pi Pico, follow these steps:

    1. Navigate to the specific example folder you wish to build.
    2. 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 modify CMakeLists.txt to specify the correct path.
    3. Pico W Users: If using a PICO W (with Infineon CYW43439 wireless chip), you must uncomment 3 lines in the CMakeLists.txt file.
    4. Run the build commands:
      mkdir build && cd build
      cmake ..
      make
    5. Copy the resulting .uf2 file to your Pico board.
  6. Implement I2Cdev for STM32 using Keil MDK Pack

    master

    To implement I2Cdev on STM32, use the Keil MDK Pack. This approach provides better compatibility than HAL/SPL, allowing I2Cdev to work with any device listed on the MDK Pack software page.

    To set it up:

    1. Download the appropriate pack for your specific STM32 device.
    2. Enable your device driver.
    3. Specify the peripheral you are using in I2Cdev.h.
  7. Monitor serial output from Raspberry Pi Pico

    master

    After flashing the .uf2 file to your Pico, you can monitor the serial output via USB.

    On Linux: Use minicom with sudo to ensure the device opens correctly:

    sudo minicom -D /dev/ttyACM0

    On Windows: Use PuTTY and select the COM port assigned to your device (verify this in the Device Manager). Set the baudrate to 115200.

  8. Calibrate the MPU6050 sensor

    master

    For 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 calling mpu.dmpInitialize():

    1. Manual Offset Setting: Use the specific offset methods (e.g., mpu.setXAccelOffset()) with values obtained from a calibration example.
    2. 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)