Skip to content

Lesson09: SC7A20HTR Accelerometer and Motion Detection

1. Objectives

In this lesson, you will read data from the SC7A20HTR three-axis accelerometer via I2C, output acceleration along the X, Y, and Z axes, and determine whether the device has moved significantly based on changes in the total acceleration.

2. Prerequisites

  • Complete the ESP32-S3 environment setup in Lesson01.
  • This lesson uses the built-in Arduino-ESP32 Wire library. No additional third-party libraries are required.
  • This lesson includes the local driver files sc7a20h.h and sc7a20h.cpp.

How to add the library files:

  • No additional third-party libraries are required for this lesson. Use the Wire library included with the ESP32 3.3.3 board package.

3. Arduino IDE Instructions

  1. Open Lesson09_Accelerometer_Vibration.ino in the Arduino IDE. lesson_1

  2. Confirm that sc7a20h.h, sc7a20h.cpp, and Lesson09_Accelerometer_Vibration.ino are in the same sketch project directory. lesson_2

  3. Confirm that the board settings match those used in Lesson01. lesson_3

  4. Click Verify to compile the sketch. lesson_4

  5. Click Upload to flash the sketch, open the Serial Monitor, and set the baud rate to 115200. lesson_5

4. Hardware Instructions

  1. Connect the development board to the computer using a USB data cable, and keep the device lying flat.

lesson_6

  1. Open the Serial Monitor and first observe the X, Y, and Z acceleration data while the device is stationary.

  2. Gently pick up, tilt, or shake the device, and observe the changes in the acceleration values and motion status.

  3. Do not strike or drop the device, as this may damage the display, ribbon cable, or sensor.

5. Key Code Explanation

constexpr uint8_t kSensorAddress = 0x19;
Wire.begin(kI2cSdaPin, kI2cSclPin);
accelerometer.begin(Wire, kSensorAddress);

The SC7A20HTR communicates via I2C, and the verified address for this hardware is 0x19. The code first starts I2C on GPIO4/GPIO3, then passes the Wire object and address to the local driver. This allows the main program to control the hardware bus explicitly instead of requiring the driver files to determine the pins themselves.

return writeRegister(kControlRegister1, 0x47) &&
       writeRegister(kControlRegister2, 0x00) &&
       writeRegister(kControlRegister3, 0x00) &&
       writeRegister(kControlRegister4, 0x88);

This code configures the accelerometer in the driver. 0x47 enables the X, Y, and Z axes and sets the output data rate, 0x00 disables unnecessary filtering and interrupt output, and 0x88 enables high-resolution mode and block data update protection. The write operations are chained with &&, so initialization fails if any step fails.

const float magnitude = sqrtf(x * x + y * y + z * z);
const float delta = firstSample ? 0.0f : fabsf(magnitude - previousMagnitude);
const bool motionDetected = !firstSample && delta >= kMotionDeltaThreshold;

Combining the acceleration values from all three axes produces the total acceleration, magnitude. The program compares the current total acceleration with the previous total acceleration. If the difference exceeds the threshold, the device is considered to have moved significantly. This method is simple and intuitive, making it suitable for learning the basics of sensor event detection.

Download the Lesson 09 code