SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bma456h_accelerometer.ino
Go to the documentation of this file.
1
30#include <AccelerometerDrv.hpp>
31
32#ifndef SENSOR_SDA
33#define SENSOR_SDA 21
34#endif
35
36#ifndef SENSOR_SCL
37#define SENSOR_SCL 22
38#endif
39
40#ifndef SENSOR_IRQ
41#define SENSOR_IRQ 39
42#endif
43
44// Configuring using BMA456H features
46
47volatile bool isInterruptTriggered = false;
48
49void setup()
50{
51 bool rslt;
52
53 Serial.begin(115200);
54 while (!Serial);
55
56 pinMode(SENSOR_IRQ, INPUT);
57 attachInterrupt(SENSOR_IRQ, +[]() {
59 }, RISING);
60
61 // Sensor initialization
62 rslt = accelSensor.begin(Wire, BMA4XX_I2C_ADDR_SDO_HIGH, SENSOR_SDA, SENSOR_SCL);
63 if (!rslt) {
64 while (1) {
65 Serial.println("BMA4xx sensor initialization failed");
66 delay(1000);
67 }
68 }
69
70 Serial.print(accelSensor.getModelName());
71 Serial.println(" Sensor initialized successfully");
72
73 // Set sensor orientation, based on the hardware placement setting
75 if (!rslt) {
76 Serial.println("Failed to set remap axes");
77 }
78
79 // Accelerometer configuration
80 // The desired operation mode. Allowed values are SUSPEND, NORMAL.
82 // The desired full-scale range. Allowed values are FS_2G, FS_4G, FS_8G, FS_16G.
84 // The desired bandwidth. Allowed values are OSR4_AVG1, OSR2_AVG2, NORMAL_AVG4, etc.
86 // 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.
87 float data_rate_hz = 100.0f;
88 // The desired performance mode. Allowed values are CIC_AVG_MODE, CONTINUOUS_MODE
90
91 if (!accelSensor.configAccelerometer(operationMode, fullScaleRange, data_rate_hz, bandwidth, perfMode)) {
92 Serial.println("Failed to configure accelerometer");
93 while (1);
94 }
95
96 // Enable data ready feature
97 rslt = accelSensor.enableDataReady(true);
98 if (!rslt) {
99 Serial.println("Failed to enable data ready");
100 while (1);
101 }
102
103 delay(3000);
104
105 Serial.println("Now read accelerometer data form sensor");
106}
107
108void loop()
109{
110 AccelerometerData accelData;
111 if (accelSensor.isDataReady()) {
112 // Read the accelerometer data
113 if (accelSensor.readData(accelData)) {
114 Serial.print("Temperature: ");
115 Serial.print(accelData.temperature);
116 Serial.print(" °C, Acc_Raw: (");
117 Serial.print(accelData.raw.x);
118 Serial.print(", ");
119 Serial.print(accelData.raw.y);
120 Serial.print(", ");
121 Serial.print(accelData.raw.z);
122 Serial.print("), Acc_ms2: (");
123 Serial.print(accelData.mps2.x, 2);
124 Serial.print(", ");
125 Serial.print(accelData.mps2.y, 2);
126 Serial.print(", ");
127 Serial.print(accelData.mps2.z, 2);
128 Serial.print(") ");
129 uint32_t timeSampleMs = accelSensor.getTimeSampleMs();
130 Serial.print(timeSampleMs);
131 Serial.print("(ms)/");
132 Serial.print(millis());
133 Serial.println(" ");
134 } else {
135 Serial.println("Failed to read accelerometer data");
136 }
137 }
138}
@license MIT License
OperationMode
Enumeration of sensor operation modes.
@ TOP_LAYER_RIGHT_CORNER
AccelFullScaleRange
Enumeration of accelerometer full-scale range settings.
SensorBMA456H 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.