Skip to content

Commit 0d0c4ad

Browse files
committed
Initial import (based on MKRENV lib)
0 parents  commit 0d0c4ad

File tree

8 files changed

+320
-0
lines changed

8 files changed

+320
-0
lines changed

README.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
= LPS22HB Library for Arduino =
2+
3+
Allows you to read the pressure sensor of your Nano 33 BLE Sense.
4+
5+
6+
== License ==
7+
8+
Copyright (c) 2019 Arduino SA. All rights reserved.
9+
10+
This library is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU Lesser General Public
12+
License as published by the Free Software Foundation; either
13+
version 2.1 of the License, or (at your option) any later version.
14+
15+
This library is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
Lesser General Public License for more details.
19+
20+
You should have received a copy of the GNU Lesser General Public
21+
License along with this library; if not, write to the Free Software
22+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
LPS22HB - Read Pressure
3+
4+
This example reads data from the on-board LPS22HB sensor of the
5+
Nano 33 BLE Sense and prints the pressure sensor value to the
6+
Serial Monitor once a second.
7+
8+
The circuit:
9+
- Arduino Nano 33 BLE Sense
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <Arduino_LPS22HB.h>
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
while (!Serial);
19+
20+
if (!BARO.begin()) {
21+
Serial.println("Failed to initialize pressure sensor!");
22+
while (1);
23+
}
24+
}
25+
26+
void loop() {
27+
// read the sensor value
28+
float pressure = BARO.readPressure();
29+
30+
// print the sensor value
31+
Serial.print("Pressure = ");
32+
Serial.print(pressure);
33+
Serial.println(" kPa");
34+
35+
// print an empty line
36+
Serial.println();
37+
38+
// wait 1 second to print again
39+
delay(1000);
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
LPS22HB - Read Pressure Imperial
3+
4+
This example reads data from the on-board LPS22HB sensor of the
5+
Nano 33 BLE Sense and prints the pressure sensor value in imperial
6+
units to the Serial Monitor once a second.
7+
8+
The circuit:
9+
- Arduino Nano 33 BLE Sense
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <Arduino_LPS22HB.h>
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
while (!Serial);
19+
20+
if (!BARO.begin()) {
21+
Serial.println("Failed to initialize pressure sensor!");
22+
while (1);
23+
}
24+
}
25+
26+
void loop() {
27+
// Passing PSI to readPressure(...)
28+
// allows you to read the sensor values in imperial units
29+
float pressure = BARO.readPressure(PSI);
30+
31+
// print the sensor value
32+
Serial.print("Pressure = ");
33+
Serial.print(pressure);
34+
Serial.println(" psi");
35+
36+
// print an empty line
37+
Serial.println();
38+
39+
// wait 1 second to print again
40+
delay(1000);
41+
}

keywords.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
##########################################
2+
# Syntax Coloring Map For Arduino_LPS22HB
3+
##########################################
4+
# Class
5+
#######################################
6+
7+
Arduino_LPS22HB KEYWORD1
8+
LPS22HB KEYWORD1
9+
BARO KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions
13+
#######################################
14+
15+
begin KEYWORD2
16+
end KEYWORD2
17+
18+
readPressure KEYWORD2
19+
20+
#######################################
21+
# Constants
22+
#######################################
23+
24+
PSI LITERAL1
25+
MILLIBAR LITERAL1
26+
KILOPASCAL LITERAL1

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Arduino_LPS22HB
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Allows you to read the pressure sensor of your Nano 33 BLE Sense.
6+
paragraph=
7+
category=Sensors
8+
url=http://github.com/arduino-libraries/Arduino_LPS22HB
9+
architectures=*
10+
includes=Arduino_LPS22HB.h

src/Arduino_LPS22HB.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
This file is part of the Arduino_LPS22HB library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _ARUDINO_LPS22HB_H_
21+
#define _ARUDINO_LPS22HB_H_
22+
23+
#include "BARO.h"
24+
25+
#endif

src/BARO.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
This file is part of the Arduino_LPS22HB library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <Wire.h>
21+
22+
#include "BARO.h"
23+
24+
#define LPS22HB_ADDRESS 0x5C
25+
26+
#define LPS22HB_WHO_AM_I_REG 0x0f
27+
#define LPS22HB_CTRL2_REG 0x11
28+
#define LPS22HB_STATUS_REG 0x27
29+
#define LPS22HB_PRESS_OUT_XL_REG 0x28
30+
#define LPS22HB_PRESS_OUT_L_REG 0x29
31+
#define LPS22HB_PRESS_OUT_H_REG 0x2a
32+
33+
LPS22HBClass::LPS22HBClass(TwoWire& wire) :
34+
_wire(&wire)
35+
{
36+
}
37+
38+
int LPS22HBClass::begin()
39+
{
40+
_wire->begin();
41+
42+
if (i2cRead(LPS22HB_WHO_AM_I_REG) != 0xb1) {
43+
end();
44+
return 0;
45+
}
46+
47+
return 1;
48+
}
49+
50+
void LPS22HBClass::end()
51+
{
52+
_wire->end();
53+
}
54+
55+
float LPS22HBClass::readPressure(int units)
56+
{
57+
// trigger one shot
58+
i2cWrite(LPS22HB_CTRL2_REG, 0x01);
59+
60+
// wait for completion
61+
while ((i2cRead(LPS22HB_STATUS_REG) & 0x02) == 0) {
62+
yield();
63+
}
64+
65+
float reading = (i2cRead(LPS22HB_PRESS_OUT_XL_REG) |
66+
(i2cRead(LPS22HB_PRESS_OUT_L_REG) << 8) |
67+
(i2cRead(LPS22HB_PRESS_OUT_H_REG) << 16)) / 40960.0;
68+
69+
if (units == MILLIBAR) { // 1 kPa = 10 MILLIBAR
70+
return reading * 10;
71+
} else if (units == PSI) { // 1 kPa = 0.145038 PSI
72+
return reading * 0.145038;
73+
} else {
74+
return reading;
75+
}
76+
}
77+
78+
int LPS22HBClass::i2cRead(uint8_t reg)
79+
{
80+
_wire->beginTransmission(LPS22HB_ADDRESS);
81+
_wire->write(reg);
82+
if (_wire->endTransmission(false) != 0) {
83+
return -1;
84+
}
85+
86+
if (_wire->requestFrom(LPS22HB_ADDRESS, 1) != 1) {
87+
return -1;
88+
}
89+
90+
return _wire->read();
91+
}
92+
93+
int LPS22HBClass::i2cWrite(uint8_t reg, uint8_t val)
94+
{
95+
_wire->beginTransmission(LPS22HB_ADDRESS);
96+
_wire->write(reg);
97+
_wire->write(val);
98+
if (_wire->endTransmission() != 0) {
99+
return 0;
100+
}
101+
102+
return 1;
103+
}
104+
105+
LPS22HBClass BARO(Wire1);

src/BARO.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
This file is part of the Arduino_LPS22HB library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _BARO_H_
21+
#define _BARO_H_
22+
23+
#include <Arduino.h>
24+
#include <Wire.h>
25+
26+
enum {
27+
PSI,
28+
MILLIBAR,
29+
KILOPASCAL
30+
};
31+
32+
class LPS22HBClass {
33+
public:
34+
LPS22HBClass(TwoWire& wire);
35+
36+
int begin();
37+
void end();
38+
39+
float readPressure(int units = KILOPASCAL);
40+
41+
private:
42+
int i2cRead(uint8_t reg);
43+
int i2cWrite(uint8_t reg, uint8_t val);
44+
45+
private:
46+
TwoWire* _wire;
47+
};
48+
49+
extern LPS22HBClass BARO;
50+
51+
#endif

0 commit comments

Comments
 (0)