SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bma422_accelerometer.ino
Go to the documentation of this file.
1
29#include <AccelerometerDrv.hpp>
30
31#ifndef SENSOR_SDA
32#define SENSOR_SDA 21
33#endif
34
35#ifndef SENSOR_SCL
36#define SENSOR_SCL 22
37#endif
38
39#ifndef SENSOR_IRQ
40#define SENSOR_IRQ 39
41#endif
42
44
45volatile bool isInterruptTriggered = false;
46
47void setup()
48{
49 bool rslt;
50
51 Serial.begin(115200);
52 while (!Serial);
53
54 pinMode(SENSOR_IRQ, INPUT);
55 attachInterrupt(SENSOR_IRQ, +[]() {
57 }, RISING);
58
59 // Sensor initialization
60 rslt = accelSensor.begin(Wire, BMA4XX_I2C_ADDR_SDO_HIGH, SENSOR_SDA, SENSOR_SCL);
61 if (!rslt) {
62 while (1) {
63 Serial.println("BMA4xx sensor initialization failed");
64 delay(1000);
65 }
66 }
67
68 Serial.print(accelSensor.getModelName());
69 Serial.println(" Sensor initialized successfully");
70
71 // Set sensor orientation, based on the hardware placement setting
73 if (!rslt) {
74 Serial.println("Failed to set remap axes");
75 }
76
77 // Accelerometer configuration
78 // The desired operation mode. Allowed values are SUSPEND, NORMAL.
80 // The desired full-scale range. Allowed values are FS_2G, FS_4G, FS_8G, FS_16G.
82 // The desired bandwidth. Allowed values are OSR4_AVG1, OSR2_AVG2, NORMAL_AVG4, etc.
84 // The desired data rate in Hz. Allowed values are 0.78, 1.56, 3.12, 6.25, 12.5, 25, 50, 100, 200, 400, 800, 1600.
85 float data_rate_hz = 100.0f;
86 // The desired performance mode. Allowed values are CIC_AVG_MODE, CONTINUOUS_MODE
88
89 if (!accelSensor.configAccelerometer(operationMode, fullScaleRange, data_rate_hz, bandwidth, perfMode)) {
90 Serial.println("Failed to configure accelerometer");
91 while (1);
92 }
93
94 // Enable data ready feature
95 rslt = accelSensor.enableDataReady(true);
96 if (!rslt) {
97 Serial.println("Failed to enable data ready");
98 while (1);
99 }
100
101 delay(3000);
102
103 Serial.println("Now read accelerometer data form sensor");
104}
105
106void loop()
107{
108 AccelerometerData accelData;
109 if (accelSensor.isDataReady()) {
110 // Read the accelerometer data
111 if (accelSensor.readData(accelData)) {
112 Serial.print("Temperature: ");
113 Serial.print(accelData.temperature);
114 Serial.print(" °C, Acc_Raw: (");
115 Serial.print(accelData.raw.x);
116 Serial.print(", ");
117 Serial.print(accelData.raw.y);
118 Serial.print(", ");
119 Serial.print(accelData.raw.z);
120 Serial.print("), Acc_ms2: (");
121 Serial.print(accelData.mps2.x, 2);
122 Serial.print(", ");
123 Serial.print(accelData.mps2.y, 2);
124 Serial.print(", ");
125 Serial.print(accelData.mps2.z, 2);
126 Serial.print(") ");
127 uint32_t timeSampleMs = accelSensor.getTimeSampleMs();
128 Serial.print(timeSampleMs);
129 Serial.print("(ms)/");
130 Serial.print(millis());
131 Serial.println(" ");
132 } else {
133 Serial.println("Failed to read accelerometer data");
134 }
135 }
136}
@license MIT License
OperationMode
Enumeration of sensor operation modes.
@ TOP_LAYER_RIGHT_CORNER
AccelFullScaleRange
Enumeration of accelerometer full-scale range settings.
SensorBMA422 accelSensor
void setup()
#define SENSOR_SCL
volatile bool isInterruptTriggered
#define SENSOR_SDA
#define SENSOR_IRQ
void loop()
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
bool enableDataReady(bool enable=true, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable data ready interrupt.
bool isDataReady() override
Check if new accelerometer data is available.
virtual bool setRemapAxes(SensorRemap remap)
Set the remapping of axes for the BMA4XX sensor.
uint32_t getTimeSampleMs() const
Get the current sensor time in milliseconds.
const char * getModelName() const
Get the model name of the BMA4XX sensor.
bool readData(AccelerometerData &data) override
Get the accelerometer data.
bool configAccelerometer(OperationMode mode, AccelFullScaleRange range, float data_rate_hz, AccelBandwidth bandwidth, AccelPerfMode perf_mode)
Configure the accelerometer.
AccelBandwidth
BMA4 Accelerometer averaging filter configuration These values configure the internal averaging filte...
@ OSR2_AVG2
OSR2 mode with 2 samples averaged.
AccelPerfMode
BMA4 Accelerometer performance mode configuration Configures the internal filter behavior for sample ...
@ CIC_AVG_MODE
Averaging filter mode: samples are averaged based on configured bandwidth and ODR.
Structure representing accelerometer data.
SensorVector mps2
Acceleration in meters per second squared (m/s²)
float temperature
Temperature in degrees Celsius (if available)
RawVector raw
Raw sensor values in LSB (least significant bits)
int16_t x
X-axis raw value.
int16_t y
Y-axis raw value.
int16_t z
Z-axis raw value.
float x
X-axis component.
float y
Y-axis component.
float z
Z-axis component.