SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bma456h_data_ready_interrupt.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
50{
51 Serial.print("Data ready: ");
52 Serial.print("Temperature: ");
53 Serial.print(data.temperature);
54 Serial.print(" °C, Acc_Raw: (");
55 Serial.print(data.raw.x);
56 Serial.print(", ");
57 Serial.print(data.raw.y);
58 Serial.print(", ");
59 Serial.print(data.raw.z);
60 Serial.print("), Acc_mps2: (");
61 Serial.print(data.mps2.x);
62 Serial.print(", ");
63 Serial.print(data.mps2.y);
64 Serial.print(", ");
65 Serial.print(data.mps2.z);
66 Serial.println(")");
67}
68
69void setup()
70{
71 bool rslt;
72
73 Serial.begin(115200);
74 while (!Serial);
75
76 pinMode(SENSOR_IRQ, INPUT);
77 attachInterrupt(SENSOR_IRQ, +[]() {
79 }, RISING);
80
81 // Sensor initialization
82 rslt = accelSensor.begin(Wire, BMA4XX_I2C_ADDR_SDO_HIGH, SENSOR_SDA, SENSOR_SCL);
83 if (!rslt) {
84 while (1) {
85 Serial.println("BMA4xx sensor initialization failed");
86 delay(1000);
87 }
88 }
89
90 Serial.print(accelSensor.getModelName());
91 Serial.println(" Sensor initialized successfully");
92
93 // Set sensor orientation, based on the hardware placement setting
95 if (!rslt) {
96 Serial.println("Failed to set remap axes");
97 }
98
99 // Accelerometer configuration
100 // The desired operation mode. Allowed values are SUSPEND, NORMAL.
101 OperationMode operationMode = OperationMode::NORMAL;
102 // The desired full-scale range. Allowed values are FS_2G, FS_4G, FS_8G, FS_16G.
104 // The desired bandwidth. Allowed values are OSR4_AVG1, OSR2_AVG2, NORMAL_AVG4, etc.
106 // 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.
107 float data_rate_hz = 100.0f;
108 // The desired performance mode. Allowed values are CIC_AVG_MODE, CONTINUOUS_MODE
110
111 if (!accelSensor.configAccelerometer(operationMode, fullScaleRange, data_rate_hz, bandwidth, perfMode)) {
112 Serial.println("Failed to configure accelerometer");
113 while (1);
114 }
115
116 bool feature_enable = true; // Enable feature
117 bool interrupt_enable = true; //True: hardware interrupt enabled
119
121 pin_map, // Which pin should the hardware interrupt be routed to?
122 false, // level trigger
123 false, // active high
124 true, // output enable
125 false); // input disable
126 if (!rslt) {
127 Serial.println("Failed to set interrupt pin configuration");
128 while (1);
129 }
130
131 // Enable data ready feature
132 rslt = accelSensor.enableDataReady(feature_enable, pin_map);
133 if (!rslt) {
134 Serial.println("Failed to enable data ready");
135 while (1);
136 }
137
138 // Set data ready callback
140
141 delay(3000);
142
143 Serial.println("Now you can move the sensor.");
144}
145
146void loop()
147{
149 isInterruptTriggered = false;
151 }
152 delay(30);
153}
@license MIT License
OperationMode
Enumeration of sensor operation modes.
@ TOP_LAYER_RIGHT_CORNER
AccelFullScaleRange
Enumeration of accelerometer full-scale range settings.
InterruptPinMap
Enumeration of interrupt pin mapping options.
@ PIN1
Interrupt pin 1.
SensorBMA456H accelSensor
volatile bool isInterruptTriggered
void onDataReady(AccelerometerData data)
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
void update() override
Update sensor state and process interrupts.
void setOnDataReadyCallback(std::function< void(AccelerometerData)> cb)
Set the callback for data ready events.
bool enableDataReady(bool enable=true, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable data ready interrupt.
virtual bool setRemapAxes(SensorRemap remap)
Set the remapping of axes for the BMA4XX sensor.
bool setInterruptPinConfig(InterruptPinMap pin_map, bool edge_trigger=false, bool active_low=true, bool output_en=true, bool input_en=false)
Configure the electrical properties of an interrupt pin.
const char * getModelName() const
Get the model name of the BMA4XX sensor.
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.