Accelerometer
#Description
This is the sensor that allows your smartphone to recognize if it is tilted, e.g., for a leveling app. Use this one to work with the angles of your prototype.
#Links
#Units
- Degree°
#Functions
#JavaScript / Node.js
Accelerometerextends I2cComponentBaseAccelerometer component for reading pitch and roll from LIS3DH sensor. This is an I2C component that emits change events when orientation changes.
#Value type
interface AccelerometerValue {
pitch: number;
roll: number;
}#Constructor options
| Option | Type | Required | Description | Inherited from |
|---|---|---|---|---|
address | number | optional | — | |
pin | I2CPinID | required | — | I2cComponentBase |
#Events
| Event | Parameters | Description |
|---|---|---|
change | value: AccelerometerValue | — |
#Methods
get pitch→ numberGet current pitch in degrees.
get roll→ numberGet current roll in degrees.
get i2cId→ I2CPinIDfrom I2cComponentBaseThe resolved I2C pin ID (e.g. "i2c-0"). Useful for logging or passing back to the Arduino sketch.
#Example
import { Board, Accelerometer } from "@blokdots/components";
const board = new Board("/dev/ttyACM0");
await board.open();
const accelerometer = new Accelerometer({ board, pin: "example", address: 0x19 });
accelerometer.on("change", (value) => {
console.log("Accelerometer changed:", value);
});#C++ (Arduino)
3-axis accelerometer using Adafruit LIS3DH Emits ONE event when pitch/roll (or axes) change significantly (atomic snapshot)
#Required libraries
#include <Adafruit_LIS3DH.h>— Adafruit LIS3DH v1.3.0#include <Adafruit_Sensor.h>— Adafruit Unified Sensor v1.1.15#include <Wire.h>— Wire#include <SPI.h>— SPI
#Setup
#include "blokdots.h"
Accelerometer myComponent(eventBus, MY_EVENT);#Constructor
Accelerometer(bus, evtMove, i2cAddress, threshold, angleThresholdDeg, intervalMs, minEmitIntervalMs)| Parameter | Type | Default |
|---|---|---|
bus | EventBus | required |
evtMove | uint8_t | required |
i2cAddress | uint8_t | LIS3DH_DEFAULT_ADDRESS |
threshold | int | 50 |
angleThresholdDeg | float | 1.0f |
intervalMs | uint16_t | 10 |
minEmitIntervalMs | uint16_t | 30 |
#Methods
pitchDeg()→ floatrollDeg()→ floatrawPitchDeg()→ floatOptional: most recent sample (may be newer than last event)
rawRollDeg()→ floatx()→ intOptional: keep axis access too (in case you need it later)
y()→ intz()→ intrawX()→ intrawY()→ intrawZ()→ intaddress()→ uint8_tOptional debug helper (non-breaking addition)
emittedValue()→ const char*Format: "pitch:roll" (e.g., "45:-30")