Skip to main content

Accelerometer

I2C
input
Medium RAM
0.4.1

#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.

#Units

  • Degree°

#Functions



#JavaScript / Node.js

ClassAccelerometerextends I2cComponentBase

Accelerometer component for reading pitch and roll from LIS3DH sensor. This is an I2C component that emits change events when orientation changes.

#Value type

TypeScript
interface AccelerometerValue {
  pitch: number;
  roll: number;
}

#Constructor options

OptionTypeRequiredDescriptionInherited from
addressnumberoptional
pinI2CPinIDrequiredI2cComponentBase

#Events

EventParametersDescription
changevalue: AccelerometerValue

#Methods

get pitchnumber

Get current pitch in degrees.

get rollnumber

Get current roll in degrees.

get i2cIdI2CPinIDfrom I2cComponentBase

The resolved I2C pin ID (e.g. "i2c-0"). Useful for logging or passing back to the Arduino sketch.

#Example

JavaScript
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)

ClassAccelerometerextends ComponentBase

3-axis accelerometer using Adafruit LIS3DH Emits ONE event when pitch/roll (or axes) change significantly (atomic snapshot)

#Required libraries

#Setup

C++
#include "blokdots.h"

Accelerometer myComponent(eventBus, MY_EVENT);

#Constructor

Accelerometer(bus, evtMove, i2cAddress, threshold, angleThresholdDeg, intervalMs, minEmitIntervalMs)
ParameterTypeDefault
busEventBusrequired
evtMoveuint8_trequired
i2cAddressuint8_tLIS3DH_DEFAULT_ADDRESS
thresholdint50
angleThresholdDegfloat1.0f
intervalMsuint16_t10
minEmitIntervalMsuint16_t30

#Methods

pitchDeg()float
rollDeg()float
rawPitchDeg()float

Optional: most recent sample (may be newer than last event)

rawRollDeg()float
x()int

Optional: keep axis access too (in case you need it later)

y()int
z()int
rawX()int
rawY()int
rawZ()int
address()uint8_t

Optional debug helper (non-breaking addition)

emittedValue()const char*

Format: "pitch:roll" (e.g., "45:-30")