Skip to content

Commit 553c635

Browse files
committed
fixed and split example
1 parent 54b1c51 commit 553c635

File tree

4 files changed

+195
-142
lines changed

4 files changed

+195
-142
lines changed

Adafruit_BME280.cpp

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,7 @@ Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, i
4040
@brief Initialise sensor with given parameters / settings
4141
*/
4242
/**************************************************************************/
43-
bool Adafruit_BME280::begin(uint8_t addr,
44-
sensor_mode mode,
45-
sensor_sampling tempSampling,
46-
sensor_sampling pressSampling,
47-
sensor_sampling humSampling,
48-
sensor_filter filter,
49-
standby_duration duration
50-
)
43+
bool Adafruit_BME280::begin(uint8_t addr)
5144
{
5245
_i2caddr = addr;
5346

@@ -75,6 +68,27 @@ bool Adafruit_BME280::begin(uint8_t addr,
7568

7669
readCoefficients(); // read trimming parameters, see DS 4.2.2
7770

71+
setSampling(); // use defaults
72+
73+
return true;
74+
}
75+
76+
/**************************************************************************/
77+
/*!
78+
@brief setup sensor with given parameters / settings
79+
80+
This is simply a overload to the normal begin()-function, so SPI users
81+
don't get confused about the library requiring an address.
82+
*/
83+
/**************************************************************************/
84+
85+
86+
void Adafruit_BME280::setSampling(sensor_mode mode,
87+
sensor_sampling tempSampling,
88+
sensor_sampling pressSampling,
89+
sensor_sampling humSampling,
90+
sensor_filter filter,
91+
standby_duration duration) {
7892
_measReg.mode = mode;
7993
_measReg.osrs_t = tempSampling;
8094
_measReg.osrs_p = pressSampling;
@@ -90,28 +104,6 @@ bool Adafruit_BME280::begin(uint8_t addr,
90104
write8(BME280_REGISTER_CONTROLHUMID, _humReg.get());
91105
write8(BME280_REGISTER_CONFIG, _configReg.get());
92106
write8(BME280_REGISTER_CONTROL, _measReg.get());
93-
94-
return true;
95-
}
96-
97-
98-
/**************************************************************************/
99-
/*!
100-
@brief Initialise sensor with given parameters / settings
101-
102-
This is simply a overload to the normal begin()-function, so SPI users
103-
don't get confused about the library requiring an address.
104-
*/
105-
/**************************************************************************/
106-
bool Adafruit_BME280::begin(sensor_mode mode,
107-
sensor_sampling tempSampling,
108-
sensor_sampling pressSampling,
109-
sensor_sampling humSampling,
110-
sensor_filter filter,
111-
standby_duration duration
112-
)
113-
{
114-
return begin(BME280_ADDRESS, mode, tempSampling, pressSampling, humSampling, filter, duration);
115107
}
116108

117109

Adafruit_BME280.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,15 @@ class Adafruit_BME280 {
166166
Adafruit_BME280(int8_t cspin);
167167
Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin);
168168

169-
bool begin(uint8_t addr = BME280_ADDRESS,
170-
sensor_mode mode = MODE_NORMAL,
171-
sensor_sampling tempSampling = SAMPLING_X16,
172-
sensor_sampling pressSampling = SAMPLING_X16,
173-
sensor_sampling humSampling = SAMPLING_X16,
174-
sensor_filter filter = FILTER_OFF,
175-
standby_duration duration = STANDBY_MS_0_5
176-
);
177-
178-
// overload function without address (-> for SPI users)
179-
bool begin(sensor_mode mode = MODE_NORMAL,
180-
sensor_sampling tempSampling = SAMPLING_X16,
181-
sensor_sampling pressSampling = SAMPLING_X16,
182-
sensor_sampling humSampling = SAMPLING_X16,
183-
sensor_filter filter = FILTER_OFF,
184-
standby_duration duration = STANDBY_MS_0_5
185-
);
169+
bool begin(uint8_t addr = BME280_ADDRESS);
170+
171+
void setSampling(sensor_mode mode = MODE_NORMAL,
172+
sensor_sampling tempSampling = SAMPLING_X16,
173+
sensor_sampling pressSampling = SAMPLING_X16,
174+
sensor_sampling humSampling = SAMPLING_X16,
175+
sensor_filter filter = FILTER_OFF,
176+
standby_duration duration = STANDBY_MS_0_5
177+
);
186178

187179
void takeForcedMeasurement();
188180
float readTemperature(void);
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/***************************************************************************
2+
This is a library for the BME280 humidity, temperature & pressure sensor
3+
4+
Designed specifically to work with the Adafruit BME280 Breakout
5+
----> http://www.adafruit.com/products/2650
6+
7+
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
8+
to interface. The device's I2C address is either 0x76 or 0x77.
9+
10+
Adafruit invests time and resources providing this open source code,
11+
please support Adafruit andopen-source hardware by purchasing products
12+
from Adafruit!
13+
14+
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
15+
BSD license, all text above must be included in any redistribution
16+
***************************************************************************/
17+
18+
#include <Wire.h>
19+
#include <SPI.h>
20+
#include <Adafruit_Sensor.h>
21+
#include <Adafruit_BME280.h>
22+
23+
#define BME_SCK 13
24+
#define BME_MISO 12
25+
#define BME_MOSI 11
26+
#define BME_CS 10
27+
28+
#define SEALEVELPRESSURE_HPA (1013.25)
29+
30+
Adafruit_BME280 bme; // I2C
31+
//Adafruit_BME280 bme(BME_CS); // hardware SPI
32+
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
33+
34+
unsigned long delayTime;
35+
36+
void setup() {
37+
Serial.begin(9600);
38+
Serial.println(F("BME280 test"));
39+
40+
if (! bme.begin()) {
41+
Serial.println("Could not find a valid BME280 sensor, check wiring!");
42+
while (1);
43+
}
44+
45+
Serial.println("-- Default Test --");
46+
Serial.println("normal mode, 16x oversampling for all, filter off,");
47+
Serial.println("0.5ms standby period");
48+
delayTime = 5000;
49+
50+
51+
// For more details on the following scenarious, see chapter
52+
// 3.5 "Recommended modes of operation" in the datasheet
53+
54+
/*
55+
// weather monitoring
56+
Serial.println("-- Weather Station Scenario --");
57+
Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,");
58+
Serial.println("filter off");
59+
bme.setSampling(Adafruit_BME280::MODE_FORCED,
60+
Adafruit_BME280::SAMPLING_X1, // temperature
61+
Adafruit_BME280::SAMPLING_X1, // pressure
62+
Adafruit_BME280::SAMPLING_X1, // humidity
63+
Adafruit_BME280::FILTER_OFF );
64+
65+
// suggested rate is 1/60Hz (1m)
66+
delayTime = 60000; // in milliseconds
67+
*/
68+
69+
/*
70+
// humidity sensing
71+
Serial.println("-- Humidity Sensing Scenario --");
72+
Serial.println("forced mode, 1x temperature / 1x humidity / 0x pressure oversampling");
73+
Serial.println("= pressure off, filter off");
74+
bme.setSampling(Adafruit_BME280::MODE_FORCED,
75+
Adafruit_BME280::SAMPLING_X1, // temperature
76+
Adafruit_BME280::SAMPLING_NONE, // pressure
77+
Adafruit_BME280::SAMPLING_X1, // humidity
78+
Adafruit_BME280::FILTER_OFF );
79+
80+
// suggested rate is 1Hz (1s)
81+
delayTime = 1000; // in milliseconds
82+
*/
83+
84+
/*
85+
// indoor navigation
86+
Serial.println("-- Indoor Navigation Scenario --");
87+
Serial.println("normal mode, 16x pressure / 2x temperature / 1x humidity oversampling,");
88+
Serial.println("0.5ms standby period, filter 16x");
89+
bme.setSampling(Adafruit_BME280::MODE_NORMAL,
90+
Adafruit_BME280::SAMPLING_X2, // temperature
91+
Adafruit_BME280::SAMPLING_X16, // pressure
92+
Adafruit_BME280::SAMPLING_X1, // humidity
93+
Adafruit_BME280::FILTER_X16,
94+
Adafruit_BME280::STANDBY_MS_0_5 );
95+
96+
// suggested rate is 25Hz
97+
// 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + (2 * H_ovs + 0.5)
98+
// T_ovs = 2
99+
// P_ovs = 16
100+
// H_ovs = 1
101+
// = 40ms (25Hz)
102+
// with standby time that should really be 24.16913... Hz
103+
delayTime = 41;
104+
105+
/*
106+
// gaming
107+
Serial.println("-- Gaming Scenario --");
108+
Serial.println("normal mode, 4x pressure / 1x temperature / 0x humidity oversampling,");
109+
Serial.println("= humidity off, 0.5ms standby period, filter 16x");
110+
bme.setSampling(Adafruit_BME280::MODE_NORMAL,
111+
Adafruit_BME280::SAMPLING_X1, // temperature
112+
Adafruit_BME280::SAMPLING_X4, // pressure
113+
Adafruit_BME280::SAMPLING_NONE, // humidity
114+
Adafruit_BME280::FILTER_X16,
115+
Adafruit_BME280::STANDBY_MS_0_5 );
116+
117+
// Suggested rate is 83Hz
118+
// 1 + (2 * T_ovs) + (2 * P_ovs + 0.5)
119+
// T_ovs = 1
120+
// P_ovs = 4
121+
// = 11.5ms + 0.5ms standby
122+
delayTime = 12;
123+
*/
124+
125+
Serial.println();
126+
}
127+
128+
129+
void loop() {
130+
// Only needed in forced mode! In normal mode, you can remove the next line.
131+
bme.takeForcedMeasurement(); // has no effect in normal mode
132+
133+
printValues();
134+
delay(delayTime);
135+
}
136+
137+
138+
void printValues() {
139+
Serial.print("Temperature = ");
140+
Serial.print(bme.readTemperature());
141+
Serial.println(" *C");
142+
143+
Serial.print("Pressure = ");
144+
145+
Serial.print(bme.readPressure() / 100.0F);
146+
Serial.println(" hPa");
147+
148+
Serial.print("Approx. Altitude = ");
149+
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
150+
Serial.println(" m");
151+
152+
Serial.print("Humidity = ");
153+
Serial.print(bme.readHumidity());
154+
Serial.println(" %");
155+
156+
Serial.println();
157+
}

examples/bme280test/bme280test.ino

Lines changed: 7 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -41,107 +41,19 @@ void setup() {
4141

4242
// default settings
4343
status = bme.begin();
44-
Serial.println("-- Default Test --");
45-
Serial.println("normal mode, 16x oversampling for all, filter off,");
46-
Serial.println("0.5ms standby period");
47-
delayTime = 5000;
48-
49-
50-
// For more details on the following scenarious, see chapter
51-
// 3.5 "Recommended modes of operation" in the datasheet
52-
53-
/*
54-
// weather monitoring
55-
Serial.println("-- Weather Station Scenario --");
56-
Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,");
57-
Serial.println("filter off");
58-
status = bme.begin(Adafruit_BME280::MODE_FORCED,
59-
Adafruit_BME280::SAMPLING_X1, // temperature
60-
Adafruit_BME280::SAMPLING_X1, // pressure
61-
Adafruit_BME280::SAMPLING_X1, // humidity
62-
Adafruit_BME280::FILTER_OFF
63-
);
64-
65-
// suggested rate is 1/60Hz (1m)
66-
delayTime = 60e3;
67-
*/
68-
69-
70-
/*
71-
// humidity sensing
72-
Serial.println("-- Humidity Sensing Scenario --");
73-
Serial.println("forced mode, 1x temperature / 1x humidity / 0x pressure oversampling");
74-
Serial.println("= pressure off, filter off");
75-
status = bme.begin(Adafruit_BME280::MODE_FORCED,
76-
Adafruit_BME280::SAMPLING_X1, // temperature
77-
Adafruit_BME280::SAMPLING_NONE, // pressure
78-
Adafruit_BME280::SAMPLING_X1, // humidity
79-
Adafruit_BME280::FILTER_OFF
80-
);
81-
82-
// suggested rate is 1Hz (1s)
83-
delayTime = 1e3;
84-
*/
85-
86-
87-
/*
88-
// indoor navigation
89-
Serial.println("-- Indoor Navigation Scenario --");
90-
Serial.println("normal mode, 16x pressure / 2x temperature / 1x humidity oversampling,");
91-
Serial.println("0.5ms standby period, filter 16x");
92-
status = bme.begin(Adafruit_BME280::MODE_NORMAL,
93-
Adafruit_BME280::SAMPLING_X2, // temperature
94-
Adafruit_BME280::SAMPLING_X16, // pressure
95-
Adafruit_BME280::SAMPLING_X1, // humidity
96-
Adafruit_BME280::FILTER_X16,
97-
Adafruit_BME280::STANDBY_MS_0_5
98-
);
99-
100-
// suggested rate is 25Hz
101-
// 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + (2 * H_ovs + 0.5)
102-
// T_ovs = 2
103-
// P_ovs = 16
104-
// H_ovs = 1
105-
// = 40ms (25Hz)
106-
// with standby time that should really be 24.16913... Hz
107-
delayTime = 41;
108-
*/
109-
110-
111-
/*
112-
// gaming
113-
Serial.println("-- Gaming Scenario --");
114-
Serial.println("normal mode, 4x pressure / 1x temperature / 0x humidity oversampling,");
115-
Serial.println("= humidity off, 0.5ms standby period, filter 16x");
116-
status = bme.begin(Adafruit_BME280::MODE_NORMAL,
117-
Adafruit_BME280::SAMPLING_X1, // temperature
118-
Adafruit_BME280::SAMPLING_X4, // pressure
119-
Adafruit_BME280::SAMPLING_NONE, // humidity
120-
Adafruit_BME280::FILTER_X16,
121-
Adafruit_BME280::STANDBY_MS_0_5
122-
);
123-
124-
// Suggested rate is 83Hz
125-
// 1 + (2 * T_ovs) + (2 * P_ovs + 0.5)
126-
// T_ovs = 1
127-
// P_ovs = 4
128-
// = 11.5ms + 0.5ms standby
129-
delayTime = 12;
130-
*/
131-
132-
Serial.println();
133-
13444
if (!status) {
13545
Serial.println("Could not find a valid BME280 sensor, check wiring!");
13646
while (1);
13747
}
48+
49+
Serial.println("-- Default Test --");
50+
delayTime = 1000;
51+
52+
Serial.println();
13853
}
13954

14055

141-
void loop() {
142-
// Only needed in forced mode! In normal mode, you can remove the next line.
143-
bme.takeForcedMeasurement(); // has no effect in normal mode
144-
56+
void loop() {
14557
printValues();
14658
delay(delayTime);
14759
}
@@ -166,4 +78,4 @@ void printValues() {
16678
Serial.println(" %");
16779

16880
Serial.println();
169-
}
81+
}

0 commit comments

Comments
 (0)