Example Code for Arduino-Read gyroscope, accelerometer and onboard temperature data

This tutorial shows how to read gyroscope, accelerometer, and onboard temperature data from the ICG-20660L in real time.

Hardware Preparation

Software Preparation

  1. Install the Arduino IDE
    Download from the official website:
    https://www.arduino.cc/en/software

  2. Download and install the ICG-20660L library and sample code
    GitHub repository:
    DFRobot_ICG20660L

  3. If needed, refer to the official guide on how to install Arduino libraries
    Arduino Library Installation Guide

  4. Open the getSensorData.ino example file from the downloaded library.

Wiring Diagram

Please connect the sensor to the M0 (or other mainboards) according to the wiring diagram.

M0 Wiring

Sample Code

/*!
 * @file getSensorData.ino
 * @brief 获取传感器的陀螺仪、加速度和板载温度。
 * @n connected table in SPI
 * -----------------------------------------------------------------------------------------------------
 *  sensor pin  |            MCU                    | ESP32 | ESP8266 |    M0   | micro:bit | Mega2560 |
 *    FSY       | not connected, floating           |   X   |    X    |    X    |     X     |     X    |
 *    INT       | not connected, floating           |   X   |    X    |    X    |     X     |     X    |
 *    CS        | connected to the IO pin of MCU    | 5/D8  |  5/D6   |    5    |     P8    |     5    |
 *    SDO       | connected to miso of mcu'spi      |19/MISO|  MISO   |  MISO   |  P14/MISO |  50/MISO |
 *    SDI       | connected to mosi of mcu'spi      |23/MOSI|  MOSI   |  MOSI   |  P15/MOSI |  51/MOSI |
 *    SCK       | connected to sck of mcu'spi       |18/SCK |  SCK    |  SCK    |  P13/SCK  |  52/SCK  |
 *    GND       | GND                               |  GND  |   GND   |   GND   |    GND    |    GND   |
 *    3V3/VCC   | 3V3/VCC                           |  3V3  |   3V3   |   3V3   |    3V3    |    5V    |
 * -----------------------------------------------------------------------------------------------------
 *
 * @n connected table in IIC
 * ---------------------------------------------------------------------------------------------------
 * sensor pin |            MCU                    | ESP32 | ESP8266 |    M0   | micro:bit | Mega2560 |
 *    FSY     | not connected, floating           |   X   |    X    |    X    |     X     |     X    |
 *    INT     | not connected, floating           |   X   |    X    |    X    |     X     |     X    |
 *    SDA     | connected to SDA of mcu'iic       | 21/SDA|   SDA   |   SDA   |  P20/SDA  |  20/SDA  |
 *    SCL     | connected to scl of mcu'iic       | 22/SCL|   SCL   |   SCL   |  P19/SCL  |  21/SCL  |
 *    GND     | GND                               |  GND  |   GND   |   GND   |    GND    |    GND   |
 *    3V3/VCC | 3V3/VCC                           |  3V3  |   3V3   |   3V3   |    3V3    |    5V    |
 * ---------------------------------------------------------------------------------------------------
 *
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [Arya]([email protected])
 * @version  V1.0
 * @data  2021-05-24
 * @get from https://www.dfrobot.com
 * @url https://github.com/DFRobot/DFRobot_ICG20660L
 */
#include "DFRobot_ICG20660L.h"

#ifdef ARDUINO_BBC_MICROBIT
#define CS_PIN      8                      //The CS pin of sensor which is connected to the 8 digital io pin of micro:bit,and also can connected to other pin.
#else
#define CS_PIN      5                      //The CS pin of sensor which is connected to the 5 digital io pin of MCU,and also can connected to other pin.
#endif
/**
 * @brief The constructor of the ICG20660L sensor using IIC communication.
 * @param addr:  7-bit IIC address, controlled by SDO pin.
 * @n     IIC_ADDR_SDO_H or 0x69:  SDO pull high.(default)
 * @n     IIC_ADDR_SDO_L or 0x68:  SDO pull down.
 * @param pWire:   TwoWire class pointer.
 */
DFRobot_ICG20660L_IIC icg(/*addr=*/IIC_ADDR_SDO_H, &Wire);
/**
 * @brief The constructor of the ICG20660L sensor using SPI communication.
 * @param csPin:  SPI chip select pin, connected to IO pin of MCU.
 * @param spi: SPIClass class pointer.
 */
//DFRobot_ICG20660L_SPI icg(/*csPin=*/CS_PIN, &SPI);
void setup() {
  Serial.begin(115200);
  while(!Serial){                                                     //Waiting for USB Serial COM port to open.
  }
  
  Serial.print("Initialization sensor...");
/**
 * @brief 初始化传感器,初始化后,所有传感器都被关闭,需通过enableSensor打开相应的配置. 
 * @param mode: Enum variable,from eDataReadMode_t,配置读取传感器数据是从FIFO还是从寄存器。
 * @n     eRegMode:  配置为从寄存器读取传感器数据
 * @n     eFIFOMode: 从512字节FIFO读取数据,注意:从FIFO读取,加速度,陀螺仪、温度必须全部使能,且将其内部采样率必须配置成一致
 * @return status:
 * @n      0 :   Initialization sucess.
 * @n      -1:   Interface Initialization failed(IIC or SPI).
 * @n      -2:   读取设备ID失败,ID不是0x91
 */
  while(icg.begin(/*mode=*/icg.eRegMode) != 0){
      Serial.println("failed. Please check whether the hardware connection is wrong.");
      delay(1000);
      Serial.print("Initialization sensor...");
  }
  Serial.println("done.");
  
  Serial.print("ICG20660L Device ID: 0x");
  Serial.println(icg.readID(), HEX);
  
/**
 * @brief Enable sensor, Include Accel of xyz axis, Gyro of xyz, temperature. 
 * @param bit: 8位字节数据,每一位都代表使能一个功能位,如下表所示:
 * @n -------------------------------------------------------------------------------------------------------------------
 * @n |       bit7      |     bit6     |      bit5   |    bit4     |     bit3    |     bit2   |    bit1    |    bit0    |
 * @n -------------------------------------------------------------------------------------------------------------------
 * @n |     reserve     |    reserve   | eAccelAxisX | eAccelAxisY | eAccelAxisZ | eGyroAxisX | eGyroAxisY | eGyroAxisZ |
 * @n |                                |            eAccelAxisXYZ                |           eGyroAxisXYZ               |
 * @n |                                |                                eAxisAll                                        |
 * @n -------------------------------------------------------------------------------------------------------------------
 * @n   bit0:  Z-axis of gyro and temperature.
 * @n   bit1:  Y-axis of gyro and temperature.
 * @n   bit2:  X-axis of gyro and temperature.
 * @n   bit3:  Z-axis of acceleration.
 * @n   bit4:  Z-axis of acceleration.
 * @n   bit5:  Z-axis of acceleration.
 * @n   bit6:  reserve.
 * @n   bit7:  reserve.
 * @n Note: 使能陀螺仪的任意轴,都会自动使能传感器板载温度传感器。
 * @n   eGyroAxisZ: The bit0 of the bit, enable gyro's z axis and temperature.
 * @n   eGyroAxisY: The bit1 of the bit, enable gyro's y axis and temperature.
 * @n   eGyroAxisX: The bit2 of the bit, enable gyro's X axis and temperature.
 * @n   eAccelAxisZ: The bit3 of the bit, enable accel's z axis.
 * @n   eAccelAxisY: The bit4 of the bit, enable Accel's y axis.
 * @n   eAccelAxisX: The bit5 of the bit, enable Accel's X axis.
 * @n   eGyroAxisXYZ or eGyroAxisX|eGyroAxisY|eGyroAxisZ: The bit0/bit1/bit2 of the bit, enable gyro's xyz axis and temperature.
 * @n   eAccelAxisXYZ or eAccelAxisX|eAccelAxisY|eAccelAxisZ: The bit3/bit4/bit5 of the bit, enable Accel's xyz axis.
 * @n   eAxisAll or eGyroAxisX|eGyroAxisY|eGyroAxisZ|eAccelAxisX|eAccelAxisY|eAccelAxisZ: The bit0/bit1/bit2/bit3/bit4/bit5 of the bit, enable temperature, Accel's and gyro's xyz axis. 
 */
  icg.enableSensor(icg.eAxisAll);
  //icg.enableSensor(icg.eGyroAxisXYZ|icg.eAccelAxisXYZ);
  //icg.enableSensor(icg.eGyroAxisX|icg.eGyroAxisY|icg.eGyroAxisZ|icg.eAccelAxisX|icg.eAccelAxisY|icg.eAccelAxisZ);
/**
 * @brief Config of gyro's full scale 、dlpf bandwidth and internal sample rate. 
 * @param scale  The full scale of gyro, unit: dps(Degrees per second).
 * @n     eFSR_G_125DPS:  The full scale range is ±125 dps.
 * @n     eFSR_G_250DPS:  The full scale range is ±250 dps.
 * @n     eFSR_G_500DPS:  The full scale range is ±500 dps.
 * @param bd  Set 3-db bandwidth.
 * @n     eGyro_DLPF_8173_32KHZ:    当信号等于或大于8173Hz时,会出现明显衰减,衰减3-db,内部采样率为32KHz
 * @n     eGyro_DLPF_3281_32KHZ: 当信号等于或大于3281Hz时,会出现明显衰减,衰减3-db,内部采样率为32KHz
 * @n     eGyro_DLPF_250_8KHZ:     当信号等于或大于250Hz时,会出现明显衰减,衰减3-db,内部采样率为8KHz
 * @n     eGyro_DLPF_176_1KHZ:     当信号等于或大于176Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz
 * @n     eGyro_DLPF_92_1KHZ:      当信号等于或大于92Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz
 * @n     eGyro_DLPF_3281_8KHZ:  当信号等于或大于3281Hz时,会出现明显衰减,衰减3-db,内部采样率为8KHz
 * @n 注意:当陀螺仪和加速度都使能的时候,如果通过FIFO读取传感器数据,必须保证陀螺仪和加速度的内部采样率一致
 */
  icg.configGyro(/*scale=*/icg.eFSR_G_250DPS, /*bd=*/icg.eGyro_DLPF_176_1KHZ);
/**
 * @brief Config of accel's full scale 、dlpf bandwidth and internal sample rate. 
 * @param scale  The full scale of accel, unit: g(1g = 9.80665 m/s²).
 * @n     eFSR_A_2G:  The full scale range is ±2g.
 * @n     eFSR_A_4G:  The full scale range is ±4g.
 * @n     eFSR_A_8G:  The full scale range is ±8g.
 * @n     eFSR_A_16G:  The full scale range is ±16g.
 * @param bd  Set 3-db bandwidth.
 * @n     eAccel_DLPF_5_1KHZ or 0:    当信号小于或等于5Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz
 * @n     eAccel_DLPF_10_1KHZ or 1:   当信号小于或等于10Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz
 * @n     eAccel_DLPF_21_1KHZ or 2:   当信号小于或等于21Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz
 * @n     eAccel_DLPF_44_1KHZ or 3:   当信号小于或等于44Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz
 * @n     eAccel_DLPF_99_1KHZ or 4:   当信号小于或等于99Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz
 * @n     eAccel_DLPF_218_1KHZ or 5:  当信号小于或等于218Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz,支持低功耗模式
 * @n     eAccel_DLPF_420_1KHZ or 6:  当信号小于或等于420Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz,支持低功耗模式
 * @n     eAccel_DLPF_1046_4KHZ or 7: 当信号小于或等于1046Hz时,会出现明显衰减,衰减3-db,内部采样率为4KHz,支持低功耗模式
 * @n     eAccel_DLPF_55_1KHZ or 8:   当信号小于或等于55Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz,仅支持低功耗模式
 * @n     eAccel_DLPF_110_1KHZ or 9:  当信号小于或等于110Hz时,会出现明显衰减,衰减3-db,内部采样率为1KHz,仅支持低功耗模式
 * @n 注意:当陀螺仪和加速度都使能的时候,如果通过FIFO读取传感器数据,必须保证陀螺仪和加速度的内部采样率一致
 * @param odr:  Sets the frequency of waking up the chip to take a sample of accel data – the low power accel Output Data Rate.
 * @n     eODR_125Hz or 9:    The low power accel Output Data Rate: 125Hz
 * @n     eODR_250Hz or 10:   The low power accel Output Data Rate: 250Hz
 * @n     eODR_500Hz or 11:   The low power accel Output Data Rate: 500Hz
 * @param lowPowerFlag:  Whether to configure the Acceleration to low power mode.
 * @n     true:          Enter low power mode.
 * @n     false:         Not configure the Acceleration to low power mode.(default)
 */
  icg.configAccel(/*scale=*/icg.eFSR_A_16G, /*bd=*/icg.eAccel_DLPF_218_1KHZ);
/**
 * @brief Set sample rate divider. 
 * @param div  Sample rate divider, the range is 0~255.
 * @n     采样率 = 内部采样率/(div+1)
 * @n Note: 如果加速度配置为低功耗模式,即configAccel函数的形参lowPowerFlag为true,则采样率必须和configAccel的形参odr输出率相匹配,如下表所示:
 * @n ----------------------------------------------------------------------------
 * @n |                           configAccel                    |  setSampleDiv  |
 * @n ----------------------------------------------------------------------------|
 * @n |            bd             |      odr      | lowPowerFlag |      div       |
 * @n ----------------------------------------------------------------------------|
 * @n |            X              |       X       |    false     |      0~255     |
 * @n ----------------------------------------------------------------------------|
 * @n |                           |  eODR_125Hz   |    true      |        7       |
 * @n |                           |-----------------------------------------------|
 * @n |  支持低功耗模式的bd       |  eODR_250Hz   |    true      |        3       |
 * @n |                           |-----------------------------------------------|
 * @n |                           |  eODR_500Hz   |    true      |        1       |
 * @n |---------------------------------------------------------------------------|
 */
  icg.setSampleDiv(19);
}

#define printAxisData(sAxis, str) \
  Serial.print(" x: "); \
  Serial.print(sAxis.x); \
  Serial.print(str); \
  Serial.print(" \ty: "); \
  Serial.print(sAxis.y); \
  Serial.print(str); \
  Serial.print(" \tz: "); \
  Serial.print(sAxis.z);\
  Serial.println(str); 

void loop() {
  sIcg20660SensorData_t gyro, accel;
  float t;
/**
 * @brief Get Sensor's accel, gyro and temperature data.
 * @param accel: sIcg20660SensorData_t structure pointer which point to accel or NULL.
 * @param gyro: sIcg20660SensorData_t structure pointer which point to gyro or NULL.
 * @param t:  A float pointer which point to temperature or NULL.
 */
  icg.getSensorData(&accel, &gyro, &t);
  Serial.print("Accel: ");printAxisData(accel, " g");
  Serial.print("Gyro:  ");printAxisData(gyro, "dps");
  Serial.print("Temperature: ");Serial.print(t);Serial.println(" C");
  Serial.println();
  delay(1000);
}


Result

Was this article helpful?

TOP