Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32-Based Motion Tracking System with ICM20948 Sensor

Image of ESP32-Based Motion Tracking System with ICM20948 Sensor

Circuit Documentation

Summary

This circuit integrates a SparkFun ESP32 Thing Plus microcontroller with an Adafruit ICM20948 9-axis motion sensor via an Adafruit TXB0104 4-channel bi-directional level shifter. The ESP32 microcontroller communicates with the ICM20948 sensor using I2C communication protocol, facilitated by the level shifter to match the voltage levels between the two devices. The microcontroller reads pitch, roll, yaw, and azimuth data from the sensor and outputs these values through the serial monitor.

Component List

SparkFun ESP32 Thing Plus

  • Description: A powerful microcontroller with WiFi and Bluetooth capabilities, based on the ESP32 chip.
  • Pins: V_BATT, EN, V_USB, 13, 12, 27, 33, 15, 32, 14, SCL, SDA, CHIP_PU, 3.3V, 3, GND, A0, A1, A2, A3, A4, A5, SCK, MOSI, MISO, 16, 17, 21.

Adafruit ICM20948

  • Description: A 9-axis motion sensor with a 3-axis gyroscope, 3-axis accelerometer, and 3-axis magnetometer.
  • Pins: VIN, 1.8V, GND, SCK/SCL, SDI/SDA, INT, CS, SDO/ADR, AUX_SCL, AUX_SDA, FSYNC.

Adafruit TXB0104 4-channel Bi-Directional Level Shifter

  • Description: A 4-channel level shifter used to safely step down or step up signal voltages between different logic levels.
  • Pins: HV, B1, B2, B3, B4, GND, OE, A4, A3, A2, A1, LV.

Wiring Details

SparkFun ESP32 Thing Plus

  • 3.3V connected to HV of the level shifter and VIN of the ICM20948.
  • GND connected to GND of the level shifter and GND of the ICM20948.
  • SCL connected to A1 of the level shifter.
  • SDA connected to A2 of the level shifter.

Adafruit ICM20948

  • VIN connected to 3.3V of the ESP32 Thing Plus.
  • GND connected to GND of the ESP32 Thing Plus.
  • SCK/SCL connected to B1 of the level shifter.
  • SDI/SDA connected to B2 of the level shifter.
  • 1.8V connected to LV of the level shifter.

Adafruit TXB0104 4-channel Bi-Directional Level Shifter

  • HV connected to 3.3V of the ESP32 Thing Plus.
  • GND connected to GND of the ESP32 Thing Plus.
  • OE not connected (assumed to be tied to HV for always-on operation).
  • A1 connected to SCL of the ESP32 Thing Plus.
  • A2 connected to SDA of the ESP32 Thing Plus.
  • B1 connected to SCK/SCL of the ICM20948.
  • B2 connected to SDI/SDA of the ICM20948.
  • LV connected to 1.8V of the ICM20948.

Documented Code

/*
 * This Arduino Sketch initializes the ICM20948 sensor, reads pitch, roll,
 * yaw, and azimuth data, and outputs these values via the serial monitor.
 */

#include <Wire.h>
#include <Adafruit_ICM20948.h>

Adafruit_ICM20948 icm;

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(10); // Wait for Serial to be ready

  Serial.println("ICM20948 Test");

  if (!icm.begin_I2C()) {
    Serial.println("Failed to find ICM20948 chip");
    while (1) { delay(10); }
  }
  Serial.println("ICM20948 Found!");

  icm.setAccelRange(ICM20948_ACCEL_RANGE_2_G);
  icm.setGyroRange(ICM20948_GYRO_RANGE_250_DPS);
  icm.setMagDataRate(AK09916_MAG_DATARATE_100_HZ);
}

void loop() {
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t mag;
  sensors_event_t temp;

  icm.getEvent(&accel, &gyro, &temp, &mag);

  float pitch = atan2(accel.acceleration.y, accel.acceleration.z) * 180 / PI;
  float roll = atan2(-accel.acceleration.x, sqrt(accel.acceleration.y * accel.acceleration.y + accel.acceleration.z * accel.acceleration.z)) * 180 / PI;
  float yaw = atan2(mag.magnetic.y, mag.magnetic.x) * 180 / PI;
  float azimuth = atan2(mag.magnetic.z, sqrt(mag.magnetic.x * mag.magnetic.x + mag.magnetic.y * mag.magnetic.y)) * 180 / PI;

  Serial.print("Pitch: "); Serial.print(pitch);
  Serial.print(" Roll: "); Serial.print(roll);
  Serial.print(" Yaw: "); Serial.print(yaw);
  Serial.print(" Azimuth: "); Serial.println(azimuth);

  delay(500);
}

This code is designed to be uploaded to the SparkFun ESP32 Thing Plus microcontroller. It initializes the ICM20948 sensor and continuously reads and outputs the pitch, roll, yaw, and azimuth data to the serial monitor.