Skip to content
Shop

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.

Use the following leaves.

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

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

Write the following program in the Arduino IDE.

This sketch requires additional libraries. If they are not installed, follow the AVR Arduino IDE library instructions.

//=====================================================================
// 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

Open the serial monitor of the Arduino IDE and set the baud rate to 115200 bps and you will see the acceleration and tilt.