Skip to content

Commit 7dddcc9

Browse files
committed
Add support for the pressure sensor on GIGA R1
1 parent 0e9a98e commit 7dddcc9

File tree

10 files changed

+374
-8
lines changed

10 files changed

+374
-8
lines changed

src/Arduino_SensorKit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ U8X8_SSD1306_128X64_NONAME_HW_I2C Oled_HW(U8X8_PIN_NONE, _PIN_SCL, _PIN_SDA);
1818
SensorKit_DHT Environment(3,DHT11);
1919
DHT Environment_I2C(DHT20);
2020
SensorKit_LIS3DHTR Accelerometer(_WIRE);
21-
SensorKit_BMP280 Pressure;
21+
SensorKit_BMP280 Pressure(_WIRE);

src/Arduino_SensorKit_BMP280.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#include "Arduino_SensorKit_BMP280.h"
22

3-
SensorKit_BMP280::SensorKit_BMP280(){
4-
5-
}
6-
73
bool SensorKit_BMP280::begin(){
8-
return BMP.init();
4+
return BMP.init(*_wire);
95
}
106

117
float SensorKit_BMP280::readTemperature(){

src/Arduino_SensorKit_BMP280.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
#ifndef Arduino_SensorKit_BMP280_h
22
#define Arduino_SensorKit_BMP280_h
33

4-
#include "Seeed_BMP280.h"
4+
#include "Grove_-_Barometer_Sensor_BMP280/Seeed_BMP280.h"
55

66
class SensorKit_BMP280 {
77

88
BMP280 BMP;
99

1010
public:
11-
SensorKit_BMP280();
11+
SensorKit_BMP280(TwoWire& w) : _wire(&w) {};
1212
bool begin();
1313
void end();
1414

1515
float readTemperature();
1616
uint32_t readPressure();
1717
float readAltitude();
18+
private:
19+
TwoWire* _wire;
1820
};
1921

2022
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Seeed Technology Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Grove_BMP280
2+
------------
3+
4+
<img src=https://raw.githubusercontent.com/SeeedDocument/Grove-Barometer_Sensor-BMP280/master/img/Grove-Barometer_Sensor-BMP280-Components_1200_s.jpg width=auto>
5+
6+
[Grove - Barometer Sensor (BMP280)](https://www.seeedstudio.com/s/Grove-Temp%26Humi%26Barometer-Sensor-(BME280)-p-2653.html)
7+
8+
Thanks to Bosch, now we’ve got a high-precision environmental sensor at even lower cost, the Grove – Barometer Sensor(BMP280). It is based on their new BMP280 sensor, which is an upgraded version of the old BMP180 used in Grove – Barometer Sensor (BMP180). The module provides precise measurement of barometric pressure and temperature in the environment. The air pressure can be measured in a range from 300 hPa to 1100hPa with ±1.0 hPa absolute accuracy. As for the temperature the sensor works perfectly for temperatures between - 40 and 85 with an accuracy of ±1.
9+
10+
Owing to its high accuracy on measuring the pressure, and the pressure changes with altitude, we can calculate the altitude with ±1 meter accuracy, which makes it a precise altimeter as well. Another great thing about this module is that, you don’t even need to worry about the I2C collisions as it provides both I2C and SPI interfaces. To use SPI, simply de-solder the bonding pads on the back. If you go with I2C, the board also provides 2 I2C addresses for you to select as you want.
11+
12+
More information please visit [wiki page](http://wiki.seeedstudio.com/Grove-Barometer_Sensor-BMP280/).
13+
14+
----
15+
16+
This software is written by Lambor Fang (shijian.fanG@seeed.cc) for seeed studio<br>
17+
and is licensed under [The MIT License](http://opensource.org/licenses/mit-license.php). Check License.txt for more information.<br>
18+
19+
20+
Seeed Studio is an open hardware facilitation company based in Shenzhen, China. <br>
21+
Benefiting from local manufacture power and convenient global logistic system, <br>
22+
we integrate resources to serve new era of innovation. Seeed also works with <br>
23+
global distributors and partners to push open hardware movement.<br>
24+
25+
26+
[![Analytics](https://ga-beacon.appspot.com/UA-46589105-3/Grove_BMP280)](https://github.com/igrigorik/ga-beacon)
27+
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include "Seeed_BMP280.h"
2+
3+
bool BMP280::init(TwoWire& w)
4+
{
5+
_wire = &w;
6+
_wire->begin();
7+
8+
if(bmp280Read8(BMP280_REG_CHIPID) != 0x58)
9+
return false;
10+
11+
dig_T1 = bmp280Read16LE(BMP280_REG_DIG_T1);
12+
dig_T2 = bmp280ReadS16LE(BMP280_REG_DIG_T2);
13+
dig_T3 = bmp280ReadS16LE(BMP280_REG_DIG_T3);
14+
15+
dig_P1 = bmp280Read16LE(BMP280_REG_DIG_P1);
16+
dig_P2 = bmp280ReadS16LE(BMP280_REG_DIG_P2);
17+
dig_P3 = bmp280ReadS16LE(BMP280_REG_DIG_P3);
18+
dig_P4 = bmp280ReadS16LE(BMP280_REG_DIG_P4);
19+
dig_P5 = bmp280ReadS16LE(BMP280_REG_DIG_P5);
20+
dig_P6 = bmp280ReadS16LE(BMP280_REG_DIG_P6);
21+
dig_P7 = bmp280ReadS16LE(BMP280_REG_DIG_P7);
22+
dig_P8 = bmp280ReadS16LE(BMP280_REG_DIG_P8);
23+
dig_P9 = bmp280ReadS16LE(BMP280_REG_DIG_P9);
24+
25+
writeRegister(BMP280_REG_CONTROL, 0x3F);
26+
return true;
27+
}
28+
29+
float BMP280::getTemperature(void)
30+
{
31+
int32_t var1, var2;
32+
33+
int32_t adc_T = bmp280Read24(BMP280_REG_TEMPDATA);
34+
adc_T >>= 4;
35+
var1 = (((adc_T >> 3) - ((int32_t)(dig_T1 << 1))) *
36+
((int32_t)dig_T2)) >> 11;
37+
38+
var2 = (((((adc_T >> 4) - ((int32_t)dig_T1)) *
39+
((adc_T >> 4) - ((int32_t)dig_T1))) >> 12) *
40+
((int32_t)dig_T3)) >> 14;
41+
42+
t_fine = var1 + var2;
43+
float T = (t_fine * 5 + 128) >> 8;
44+
return T/100;
45+
}
46+
47+
uint32_t BMP280::getPressure(void)
48+
{
49+
int64_t var1, var2, p;
50+
51+
// Call getTemperature to get t_fine
52+
getTemperature();
53+
54+
int32_t adc_P = bmp280Read24(BMP280_REG_PRESSUREDATA);
55+
adc_P >>= 4;
56+
57+
var1 = ((int64_t)t_fine) - 128000;
58+
var2 = var1 * var1 * (int64_t)dig_P6;
59+
var2 = var2 + ((var1*(int64_t)dig_P5)<<17);
60+
var2 = var2 + (((int64_t)dig_P4)<<35);
61+
var1 = ((var1 * var1 * (int64_t)dig_P3)>>8) + ((var1 * (int64_t)dig_P2)<<12);
62+
var1 = (((((int64_t)1)<<47)+var1))*((int64_t)dig_P1)>>33;
63+
if (var1 == 0)
64+
{
65+
return 0; // avoid exception caused by division by zero
66+
}
67+
p = 1048576-adc_P;
68+
p = (((p<<31)-var2)*3125)/var1;
69+
var1 = (((int64_t)dig_P9) * (p>>13) * (p>>13)) >> 25;
70+
var2 = (((int64_t)dig_P8) * p) >> 19;
71+
p = ((p + var1 + var2) >> 8) + (((int64_t)dig_P7)<<4);
72+
return (uint32_t)p/256;
73+
}
74+
75+
float BMP280::calcAltitude(float pressure)
76+
{
77+
float A = pressure/101325;
78+
float B = 1/5.25588;
79+
float C = pow(A,B);
80+
C = 1.0 - C;
81+
C = C /0.0000225577;
82+
return C;
83+
}
84+
85+
uint8_t BMP280::bmp280Read8(uint8_t reg)
86+
{
87+
_wire->beginTransmission(BMP280_ADDRESS);
88+
_wire->write(reg);
89+
_wire->endTransmission();
90+
91+
_wire->requestFrom(BMP280_ADDRESS, 1);
92+
while(!_wire->available());
93+
return _wire->read();
94+
}
95+
96+
uint16_t BMP280::bmp280Read16(uint8_t reg)
97+
{
98+
uint8_t msb, lsb;
99+
100+
_wire->beginTransmission(BMP280_ADDRESS);
101+
_wire->write(reg);
102+
_wire->endTransmission();
103+
104+
_wire->requestFrom(BMP280_ADDRESS, 2);
105+
while(_wire->available()<2);
106+
msb = _wire->read();
107+
lsb = _wire->read();
108+
109+
return (uint16_t) msb<<8 | lsb;
110+
}
111+
112+
uint16_t BMP280::bmp280Read16LE(uint8_t reg)
113+
{
114+
uint16_t data = bmp280Read16(reg);
115+
return (data >> 8) | (data << 8);
116+
}
117+
118+
int16_t BMP280::bmp280ReadS16(uint8_t reg)
119+
{
120+
return (int16_t)bmp280Read16(reg);
121+
}
122+
123+
int16_t BMP280::bmp280ReadS16LE(uint8_t reg)
124+
{
125+
return (int16_t)bmp280Read16LE(reg);
126+
}
127+
128+
uint32_t BMP280::bmp280Read24(uint8_t reg)
129+
{
130+
uint32_t data;
131+
132+
_wire->beginTransmission(BMP280_ADDRESS);
133+
_wire->write(reg);
134+
_wire->endTransmission();
135+
136+
_wire->requestFrom(BMP280_ADDRESS, 3);
137+
while(_wire->available()<3);
138+
data = _wire->read();
139+
data <<= 8;
140+
data |= _wire->read();
141+
data <<= 8;
142+
data |= _wire->read();
143+
144+
return data;
145+
}
146+
147+
void BMP280::writeRegister(uint8_t reg, uint8_t val)
148+
{
149+
_wire->beginTransmission(BMP280_ADDRESS); // start transmission to device
150+
_wire->write(reg); // send register address
151+
_wire->write(val); // send value to write
152+
_wire->endTransmission(); // end transmission
153+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#ifndef _SEEED_BMP280_H_
2+
#define _SEEED_BMP280_H_
3+
4+
#include <Arduino.h>
5+
#include <Wire.h>
6+
7+
#define BMP280_ADDRESS 0x77
8+
9+
#define BMP280_REG_DIG_T1 0x88
10+
#define BMP280_REG_DIG_T2 0x8A
11+
#define BMP280_REG_DIG_T3 0x8C
12+
13+
#define BMP280_REG_DIG_P1 0x8E
14+
#define BMP280_REG_DIG_P2 0x90
15+
#define BMP280_REG_DIG_P3 0x92
16+
#define BMP280_REG_DIG_P4 0x94
17+
#define BMP280_REG_DIG_P5 0x96
18+
#define BMP280_REG_DIG_P6 0x98
19+
#define BMP280_REG_DIG_P7 0x9A
20+
#define BMP280_REG_DIG_P8 0x9C
21+
#define BMP280_REG_DIG_P9 0x9E
22+
23+
#define BMP280_REG_CHIPID 0xD0
24+
#define BMP280_REG_VERSION 0xD1
25+
#define BMP280_REG_SOFTRESET 0xE0
26+
27+
#define BMP280_REG_CONTROL 0xF4
28+
#define BMP280_REG_CONFIG 0xF5
29+
#define BMP280_REG_PRESSUREDATA 0xF7
30+
#define BMP280_REG_TEMPDATA 0xFA
31+
32+
33+
class BMP280
34+
{
35+
public:
36+
bool init(TwoWire& w = Wire);
37+
float getTemperature(void);
38+
uint32_t getPressure(void);
39+
float calcAltitude(float pressure);
40+
private:
41+
TwoWire* _wire;
42+
43+
// Calibratino data
44+
uint16_t dig_T1;
45+
int16_t dig_T2;
46+
int16_t dig_T3;
47+
48+
uint16_t dig_P1;
49+
int16_t dig_P2;
50+
int16_t dig_P3;
51+
int16_t dig_P4;
52+
int16_t dig_P5;
53+
int16_t dig_P6;
54+
int16_t dig_P7;
55+
int16_t dig_P8;
56+
int16_t dig_P9;
57+
58+
int32_t t_fine;
59+
60+
uint8_t bmp280Read8(uint8_t reg);
61+
uint16_t bmp280Read16(uint8_t reg);
62+
uint16_t bmp280Read16LE(uint8_t reg);
63+
int16_t bmp280ReadS16(uint8_t reg);
64+
int16_t bmp280ReadS16LE(uint8_t reg);
65+
uint32_t bmp280Read24(uint8_t reg);
66+
void writeRegister(uint8_t reg, uint8_t val);
67+
};
68+
69+
#endif
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* bmp280_example.ino
3+
* Example sketch for BMP280
4+
*
5+
* Copyright (c) 2016 seeed technology inc.
6+
* Website : www.seeedstudio.com
7+
* Author : Lambor, CHN
8+
* Create Time:
9+
* Change Log :
10+
*
11+
* The MIT License (MIT)
12+
*
13+
* Permission is hereby granted, free of charge, to any person obtaining a copy
14+
* of this software and associated documentation files (the "Software"), to deal
15+
* in the Software without restriction, including without limitation the rights
16+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
* copies of the Software, and to permit persons to whom the Software is
18+
* furnished to do so, subject to the following conditions:
19+
*
20+
* The above copyright notice and this permission notice shall be included in
21+
* all copies or substantial portions of the Software.
22+
*
23+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29+
* THE SOFTWARE.
30+
*/
31+
#include "Seeed_BMP280.h"
32+
#include <Wire.h>
33+
34+
BMP280 bmp280;
35+
36+
void setup()
37+
{
38+
Serial.begin(9600);
39+
if(!bmp280.init()){
40+
Serial.println("Device error!");
41+
}
42+
}
43+
44+
void loop()
45+
{
46+
float pressure;
47+
48+
//get and print temperatures
49+
Serial.print("Temp: ");
50+
Serial.print(bmp280.getTemperature());
51+
Serial.println("C"); // The unit for Celsius because original arduino don't support speical symbols
52+
53+
//get and print atmospheric pressure data
54+
Serial.print("Pressure: ");
55+
Serial.print(pressure = bmp280.getPressure());
56+
Serial.println("Pa");
57+
58+
//get and print altitude data
59+
Serial.print("Altitude: ");
60+
Serial.print(bmp280.calcAltitude(pressure));
61+
Serial.println("m");
62+
63+
Serial.println("\n");//add a line between output of different times.
64+
65+
delay(1000);
66+
}

0 commit comments

Comments
 (0)