Skip to content
Shop

STM32: Read the accelerometer

Explore with your favorite AI

Gemini & other AI

Copy this page’s context into your AI to explore how it works and what you can build.

Open Gemini

Copy the prompt, then paste it into Gemini’s message box.

View prompt

This sample reads the value of the accelerometer on the 4-Sensors Leaf and displays it on a serial monitor.

Complete the Arduino IDE setup for AP03 and select Leafony STM32 MCU as the board. The sensor is the LIS3DH on AI01 4-Sensors. Install Adafruit LIS3DH and the dependencies offered during installation through the Library Manager.

The sketch uses I2C address 0x19. If you use a leaf with a changed address setting, check the I2C address map and match LIS3DH_ADDRESS to it. See How to run an example for obtaining the source, uploading, and checking the result.

Use the following leaves.

TypeNameQ’ty
AZ62Connector Cover1
AI014-Sensors1
AZ01USB1
AP03STM32 MCU1
AV01CR20321
CR2032 coin cell battery1
M2*15mm screw2

Let’s assemble leaves as shown in the figure below.

assemble1

Write the following program1 in the Arduino IDE.

In order to make this sketch work, you need to install the libraries. If you haven’t installed it yet, refer to Environment to install it.

//=====================================================================
// Accelerometer
//
// (c) 2021 LEAFONY SYSTEMS Co., Ltd
// Released under the MIT license
// https://opensource.org/licenses/MIT
//
// Rev.00 2021/04/01 First release
//=====================================================================
#include <Adafruit_LIS3DH.h>
#define LIS3DH_ADDRESS 0x19
Adafruit_LIS3DH accel = Adafruit_LIS3DH();
void setup() {
// initialize serial communication at 115200 second per second:
Serial.begin(115200);
// initialize i2c communication with LIS3DH:
accel.begin(LIS3DH_ADDRESS);
accel.setClick(0, 0); // Disable Interrupt
accel.setRange(LIS3DH_RANGE_2_G); // Full scale +/- 2G
accel.setDataRate(LIS3DH_DATARATE_10_HZ); // Data rate = 10Hz
delay(100);
}
void loop() {
accel.read();
Serial.print("X [g] = " + String(accel.x_g));
Serial.print(", ");
Serial.print("Y [g] = " + String(accel.y_g));
Serial.print(", ");
Serial.print("Z [g] = " + String(accel.z_g));
Serial.println("");
delay(100);
}

Accelerometer.ino

After uploading, set the AP03 program switch to Run and press its reset switch. Open the Arduino IDE serial monitor at 115200 bps. Values labeled X [g], Y [g], and Z [g] appear repeatedly. This sketch does not calculate an angle.

Gently tilt the leaves and check that the axis values change. Gravity contributes to the readings even when the leaves are stationary, so all three axes do not need to read zero.

SymptomWhat to check
Adafruit_LIS3DH.h is not foundCheck that Adafruit LIS3DH and its dependencies are installed
Garbled text or no outputCheck the serial port, 115200 bps setting, Run switch position, and reset
Values do not change when tiltedTurn off the power before checking the AI01 connection, then compare its I2C address with LIS3DH_ADDRESS

Turn off the power before attaching or removing leaves. If connection or upload fails, see the FAQ.

  1. The program is the same as AVR MCU Leaf. ↩