Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
Added 2's complement for temperature readings and minor bug fixes
  • Loading branch information
reefwing committed Feb 20, 2023
1 parent bb39e21 commit 84470c5
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 33 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Reefwing LPS22HB

This is a Library for the LPS22HB Pressure Sensor, found in the Arduino Nano 33 BLE Sense Revisions 1 and 2. This library differs from the ArduinoLPS22HB library by providing altitude calculations for QNE, QNH and QFE pressure references. It also enables Block Data Update (`BDU`) which ensures that the content of the output registers is not updated until the last register is read, avoiding the reading of values related to different samples. This is important if you set the sampling rate (ODR) to anything other than one-shot.

For additional details, please refer to our Medium article: [Reefwing LPS22HB Library for the Nano 33 BLE Sense](https://reefwing.medium.com/reefwing-lps22hb-library-for-the-nano-33-ble-sense-44839caa34e4).

The LPS22HB is a compact piezoresistive absolute pressure sensor which functions as a digital barometer. The device comprises a sensing element and an interface which communicates using I2C or SPI. The Nano 33 BLE Sense is connected via I2C on Wire 1, and is factory calibrated.

Expand Down Expand Up @@ -253,7 +255,7 @@ void loop() {

## Library Public Methods

The ReefwingLPS22HB Library contains the following public methods to control you sensor.
The ReefwingLPS22HB Library contains the following public methods to control your sensor.

```c++
ReefwingLPS22HB();
Expand Down
Binary file added data sheet/an4833-measuring-pressure-lps22hb.pdf
Binary file not shown.
Binary file not shown.
5 changes: 3 additions & 2 deletions examples/readAltitude/readAltitude.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
@copyright Please see the accompanying LICENSE file.
Code: David Such
Version: 1.0.1
Date: 11/02/23
Version: 1.0.2
Date: 20/02/23
1.0.1 Original Release. 11/02/23
1.0.2 Added 2's comp for temperature 20/02/23
Acquires temperature and pressure readings from the LPS22HB
Pressure Sensor mounted on the Arduino Nano 33 BLE Sense
Expand Down
5 changes: 3 additions & 2 deletions examples/readPressureUnits/readPressureUnits.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
@copyright Please see the accompanying LICENSE file.
Code: David Such
Version: 1.0.1
Date: 11/02/23
Version: 1.0.2
Date: 20/02/23
1.0.1 Original Release. 11/02/23
1.0.2 Added 2's comp for temperature 20/02/23
Acquires temperature and pressure readings from the LPS22HB
Pressure Sensor mounted on the Arduino Nano 33 BLE Sense
Expand Down
5 changes: 3 additions & 2 deletions examples/readTempAndPressure/readTempAndPressure.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
@copyright Please see the accompanying LICENSE file.
Code: David Such
Version: 1.0.1
Date: 11/02/23
Version: 1.0.2
Date: 20/02/23
1.0.1 Original Release. 11/02/23
1.0.2 Added 2's comp for temperature 20/02/23
Acquires temperature and pressure readings from the LPS22HB
Pressure Sensor mounted on the Arduino Nano 33 BLE Sense
Expand Down
5 changes: 3 additions & 2 deletions examples/setODR/setODR.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
@copyright Please see the accompanying LICENSE file.
Code: David Such
Version: 1.0.1
Date: 11/02/23
Version: 1.0.2
Date: 20/02/23
1.0.1 Original Release. 11/02/23
1.0.2 Added 2's comp for temperature 20/02/23
Acquires temperature and pressure readings from the LPS22HB
Pressure Sensor mounted on the Arduino Nano 33 BLE Sense
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ReefwingLPS22HB
version=1.0.1
version=1.0.2
author=David Such <[email protected]>
maintainer=David Such <[email protected]>
sentence=Arduino Library for the LPS22HB Pressure Sensor.
Expand Down
56 changes: 38 additions & 18 deletions src/ReefwingLPS22HB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
@copyright Please see the accompanying LICENSE file.
Code: David Such
Version: 1.0.1
Date: 11/02/23
Version: 1.0.2
Date: 20/02/23
1.0.1 Original Release. 11/02/23
1.0.2 Added 2's comp for temperature 20/02/23
Credit - LPS22HB Absolute Digital Barometer class
based on work by Adrien Chapelet for IoThings.
Credit - Some code used from the LPS22HB Absolute Digital Barometer
class by Adrien Chapelet for IoThings.
ref: https://github.com/adrien3d/IO_LPS22HB
******************************************************************/
Expand Down Expand Up @@ -79,7 +80,7 @@ void ReefwingLPS22HB::begin() {
yield();
}

// One-shot mode, LPF off, and BDU on
// power-down mode, LPF off, and BDU on
write(LPS22HB_CTRL_REG1, 0x02);

// Save First Reading for QFE References
Expand All @@ -88,9 +89,8 @@ void ReefwingLPS22HB::begin() {
}

void ReefwingLPS22HB::reset() {
// 0x14 = 10100b, bit 4 = IF_ADD_INC, default = 1
// bit 2 = SWRESET, set to reset chip
write(LPS22HB_CTRL_REG2, 0x14);
// 0x04 = 0100b, bit 2 = SWRESET, set to reset chip
write(LPS22HB_CTRL_REG2, 0x04);

// software reset. Bit self clears when reset complete.
while ((read(LPS22HB_CTRL_REG2) & 0x04) != 0) {
Expand All @@ -111,10 +111,10 @@ bool ReefwingLPS22HB::connected() {
}

void ReefwingLPS22HB::setODR(Rate rate) {
_rate = (int)rate;
_rate = (uint8_t)rate;

// Set ODR bits 4, 5 & 6
write(LPS22HB_CTRL_REG1, (_rate & 0x07) << 4);
// Set ODR bits 4, 5 & 6 (_rate & 0x07) << 4 and BDU 0x02
write(LPS22HB_CTRL_REG1, ((_rate & 0x07) << 4) & 0x02);
}

void ReefwingLPS22HB::setQNH(float q) {
Expand All @@ -141,7 +141,7 @@ void ReefwingLPS22HB::triggerOneShot() {
}

float ReefwingLPS22HB::readPressure(Units units) {
if (_rate == (int)Rate::RATE_ONE_SHOT) { triggerOneShot(); }
if (_rate == (uint8_t)Rate::RATE_ONE_SHOT) { triggerOneShot(); }

// To guarantee the correct behavior of the BDU feature,
// PRESS_OUT_H (0x2A) must be the last address read.
Expand Down Expand Up @@ -176,25 +176,43 @@ float ReefwingLPS22HB::readPressure(Units units) {
return result;
}

uint32_t ReefwingLPS22HB::readPressureRAW() {
if (_rate == (int)Rate::RATE_ONE_SHOT) { triggerOneShot(); }
int16_t ReefwingLPS22HB::twosCompToInteger(uint16_t two_compliment_val) {
// [0x0000; 0x7FFF] corresponds to [0; 32,767]
// [0x8000; 0xFFFF] corresponds to [-32,768; -1]
// int16_t has the range [-32,768; 32,767]

uint16_t sign_mask = 0x8000;

if ( (two_compliment_val & sign_mask) == 0 ) {
// if positive
return two_compliment_val;
}
else {
// if negative invert all bits, add one, and add sign
return -(~two_compliment_val + 1);
}
}

uint32_t ReefwingLPS22HB::readPressureCount() {
if (_rate == (uint8_t)Rate::RATE_ONE_SHOT) { triggerOneShot(); }

uint8_t pressOutXL = read(LPS22HB_PRES_OUT_XL);
uint8_t pressOutL = read(LPS22HB_PRES_OUT_L);
uint8_t pressOutH = read(LPS22HB_PRES_OUT_H);

int32_t val = ( (pressOutH << 16) | (pressOutL << 8) | pressOutXL );
val = val + 0x400000;
long val = ( ((long)pressOutH << 16) | ((long)pressOutL << 8) | (long)pressOutXL );

return (uint32_t)val;
}

float ReefwingLPS22HB::readTemperature(Scales scales) {
if (_rate == (int)Rate::RATE_ONE_SHOT) { triggerOneShot(); }
if (_rate == (uint8_t)Rate::RATE_ONE_SHOT) { triggerOneShot(); }

uint8_t tempOutL = read(LPS22HB_TEMP_OUT_L);
uint8_t tempOutH = read(LPS22HB_TEMP_OUT_H);
int16_t val = (tempOutH << 8) | (tempOutL & 0xff);

uint16_t count = (tempOutH << 8) | (tempOutL & 0xff);
int16_t val = twosCompToInteger(count);

float result = ((float)val)/100.0f; // In Celsius

Expand Down Expand Up @@ -244,6 +262,8 @@ float ReefwingLPS22HB::readAltitude(PressureReference Pr) {
return result;
}

// I2C read and write byte

uint8_t ReefwingLPS22HB::read(uint8_t reg) {
Wire1.beginTransmission(_address);
Wire1.write(reg);
Expand Down
12 changes: 7 additions & 5 deletions src/ReefwingLPS22HB.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
@copyright Please see the accompanying LICENSE file.
Code: David Such
Version: 1.0.1
Date: 11/02/23
Version: 1.0.2
Date: 20/02/23
1.0.1 Original Release. 11/02/23
1.0.2 Added 2's comp for temperature 20/02/23
Credit - LPS22HB Absolute Digital Barometer class
based on work by Adrien Chapelet for IoThings.
Credit - Some code used from the LPS22HB Absolute Digital Barometer
class by Adrien Chapelet for IoThings.
ref: https://github.com/adrien3d/IO_LPS22HB
******************************************************************/
Expand Down Expand Up @@ -82,10 +83,11 @@ class ReefwingLPS22HB {
void setQNH(float q);
float getQNH();
void clearQNH();
int16_t twosCompToInteger(uint16_t two_compliment_val);
float readTemperature(Scales scales = Scales::CELSIUS);
float readPressure(Units units = Units::HECTOPASCAL);
float readAltitude(PressureReference Pr = PressureReference::QNE);
uint32_t readPressureRAW();
uint32_t readPressureCount();

BaroReading firstReading;
BaroReading lastReading;
Expand Down

0 comments on commit 84470c5

Please sign in to comment.