This is an Arduino library for the ST LPS22HB pressure sensor like it is used in the Arduino Nano 33 BLE Sense. The library supports several ways of reading the sensor data, including the provided mechanisms of the sensor like One-Shot mode, Continuous mode and also Interrupt generation. A main goal of the library is to provide a simple and also efficent way of reading the sensor data.
#include "st_lps22hb.h"
andrgrue::sensor::st_lps22hb Pressure(Wire1);
float pressure;
float temperature;
andrgrue::sensor::st_lps22hb::Data pressure_data;
andrgrue::sensor::st_lps22hb::Data temperature_data;
volatile bool interruptFlag = false;
void pressure_interrupt_handler() {
interruptFlag = true;
}
void setup() {
// wire must be initialized first
Wire1.begin();
Wire1.setClock(400000);
// Option 1. One Shot mode - poll sensor measurements
if (Pressure.initialize(andrgrue::sensor::st_lps22hb::Rate::RATE_ONE_SHOOT)) {
// Option 2. Continuous mode - polling of data ready info in status register to trigger sensor measurements
if (Pressure.initialize(andrgrue::sensor::st_lps22hb::Rate::RATE_50HZ)) {
// Option 3. Continuous mode - polling of interrupt pin to trigger sensor measurements
// need to specify interrupt pin
if (Pressure.initialize(andrgrue::sensor::st_lps22hb::Rate::RATE_50HZ, andrgrue::sensor::st_lps22hb::LowPassFilter::LPF_9, p12)) {
// Option 4. Continuous mode - use interrupt handler to trigger sensor measurements
// need to specify interrupt pin and interrupt handler
if (Pressure.initialize(andrgrue::sensor::st_lps22hb::Rate::RATE_50HZ, andrgrue::sensor::st_lps22hb::LowPassFilter::LPF_20, p12, pressure_interrupt_handler)) {
Serial.println("LPS22HB Pressure Sensor found.");
}
else {
Serial.println("LPS22HB Pressure Sensor not found.");
while (true) { } // loop forever
}
}
void loop() {
// Option 1.
pressure = Pressure.pressure();
temperature = Pressure.temperature();
// Option 2/3.
if (Pressure.dataAvailable()) {
pressure = Pressure.pressure();
temperature = Pressure.temperature();
}
// Option 4.
if (interruptFlag) {
pressure = Pressure.pressure();
temperature = Pressure.temperature();
interruptFlag = false;
}
}
This project was inspired and includes several elements from the following projects. Special thanks to the authors.
Copyright © 2024, André Grüttner. All rights reserved.
This project is licensed under the GNU Lesser General Public License v2.1 (LGPL-2.1). You may use, modify, and distribute this software under the terms of the LGPL-2.1 license. See the LICENSE file for details, or visit GNU’s official LGPL-2.1 page for the full license text.