Example Code for Arduino-Read the x, y, z data of accelerometer

This tutorial shows how to read the accelerometer’s x, y, and z-axis 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 getAccelData.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 getAccelData.ino
 * @brief 获取传感器的加速度数据,仅适用于寄存器模式(此demo不支持FIFO读取模式)。
 *
 * @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    |
 * ---------------------------------------------------------------------------------------------------
 */

#include "DFRobot_ICG20660L.h"

#ifdef ARDUINO_BBC_MICROBIT
#define CS_PIN      8
#else
#define CS_PIN      5
#endif

DFRobot_ICG20660L_IIC icg(/*addr=*/IIC_ADDR_SDO_H, &Wire);
// DFRobot_ICG20660L_SPI icg(/*csPin=*/CS_PIN, &SPI);

float G = 9.80665; // 1G = 9.80665 m/s²

void setup() {
  Serial.begin(115200);
  while(!Serial){}

  Serial.print("Initialization sensor...");
  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);

  icg.enableSensor(icg.eAccelAxisXYZ);
  icg.configAccel(icg.eFSR_A_16G, icg.eAccel_DLPF_99_1KHZ);
  icg.setSampleDiv(19);
}

void loop() {
  float x, y, z;
  sIcg20660SensorData_t accel;

  accel.x = icg.getAccelDataX();
  accel.y = icg.getAccelDataY();
  accel.z = icg.getAccelDataZ();

  Serial.print("Accel: unit(g) ");
  Serial.print("x: ");Serial.print(accel.x);
  Serial.print(",\ty: ");Serial.print(accel.y);
  Serial.print(",\tz: ");Serial.println(accel.z);

  Serial.print("Accel: unit(m/s2)");
  Serial.print("x: ");Serial.print(accel.x*G);
  Serial.print(",\ty: ");Serial.print(accel.y*G);
  Serial.print(",\tz: ");Serial.println(accel.z*G);

  Serial.println();
  delay(1000);
}

Result

Was this article helpful?

TOP