Skip to content
ショップ

Accelerometer

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

Use the following leaf.

TypeNameQ’ty
AZ62Connector Cover2
AI014-Sensors1
AX07Back to back1
AP02ESP32 MCU1
AV042V~4.5V1
AZ63Nut Plate2
AAA battery holder1
AAA battery3
M2*8mm screw2
M2*12mm screw2
φ10x2mm magnet1

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

assemble1

Write the following program 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) 2020 Trillion-Node Study Group
// Released under the MIT license
// https://opensource.org/licenses/MIT
//
// Rev.00 2020/05/05 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 in the Arduino IDE and set the baud rate to 115200 bps to display the acceleration and tilt.