Adafruit BMP280 Library
Adafruit_BMP280.h
Go to the documentation of this file.
1 
21 #ifndef __BMP280_H__
22 #define __BMP280_H__
23 
24 // clang-format off
25 #include <Arduino.h>
26 #include <Adafruit_Sensor.h>
27 #include <Adafruit_I2CDevice.h>
28 #include <Adafruit_SPIDevice.h>
29 // clang-format on
30 
34 #define BMP280_ADDRESS (0x77)
35 #define BMP280_ADDRESS_ALT \
36  (0x76)
37 #define BMP280_CHIPID (0x58)
42 enum {
43  BMP280_REGISTER_DIG_T1 = 0x88,
44  BMP280_REGISTER_DIG_T2 = 0x8A,
45  BMP280_REGISTER_DIG_T3 = 0x8C,
46  BMP280_REGISTER_DIG_P1 = 0x8E,
47  BMP280_REGISTER_DIG_P2 = 0x90,
48  BMP280_REGISTER_DIG_P3 = 0x92,
49  BMP280_REGISTER_DIG_P4 = 0x94,
50  BMP280_REGISTER_DIG_P5 = 0x96,
51  BMP280_REGISTER_DIG_P6 = 0x98,
52  BMP280_REGISTER_DIG_P7 = 0x9A,
53  BMP280_REGISTER_DIG_P8 = 0x9C,
54  BMP280_REGISTER_DIG_P9 = 0x9E,
55  BMP280_REGISTER_CHIPID = 0xD0,
56  BMP280_REGISTER_VERSION = 0xD1,
57  BMP280_REGISTER_SOFTRESET = 0xE0,
59  BMP280_REGISTER_STATUS = 0xF3,
60  BMP280_REGISTER_CONTROL = 0xF4,
61  BMP280_REGISTER_CONFIG = 0xF5,
62  BMP280_REGISTER_PRESSUREDATA = 0xF7,
63  BMP280_REGISTER_TEMPDATA = 0xFA,
64 };
65 
69 typedef struct {
70  uint16_t dig_T1;
71  int16_t dig_T2;
72  int16_t dig_T3;
74  uint16_t dig_P1;
75  int16_t dig_P2;
76  int16_t dig_P3;
77  int16_t dig_P4;
78  int16_t dig_P5;
79  int16_t dig_P6;
80  int16_t dig_P7;
81  int16_t dig_P8;
82  int16_t dig_P9;
84 
85 class Adafruit_BMP280;
86 
88 class Adafruit_BMP280_Temp : public Adafruit_Sensor {
89 public:
92  Adafruit_BMP280_Temp(Adafruit_BMP280 *parent) { _theBMP280 = parent; }
93  bool getEvent(sensors_event_t *);
94  void getSensor(sensor_t *);
95 
96 private:
97  int _sensorID = 280;
98  Adafruit_BMP280 *_theBMP280 = NULL;
99 };
100 
102 class Adafruit_BMP280_Pressure : public Adafruit_Sensor {
103 public:
106  Adafruit_BMP280_Pressure(Adafruit_BMP280 *parent) { _theBMP280 = parent; }
107  bool getEvent(sensors_event_t *);
108  void getSensor(sensor_t *);
109 
110 private:
111  int _sensorID = 0;
112  Adafruit_BMP280 *_theBMP280 = NULL;
113 };
114 
119 public:
123  SAMPLING_NONE = 0x00,
125  SAMPLING_X1 = 0x01,
127  SAMPLING_X2 = 0x02,
129  SAMPLING_X4 = 0x03,
131  SAMPLING_X8 = 0x04,
133  SAMPLING_X16 = 0x05
134  };
135 
137  enum sensor_mode {
139  MODE_SLEEP = 0x00,
141  MODE_FORCED = 0x01,
143  MODE_NORMAL = 0x03,
145  MODE_SOFT_RESET_CODE = 0xB6
146  };
147 
151  FILTER_OFF = 0x00,
153  FILTER_X2 = 0x01,
155  FILTER_X4 = 0x02,
157  FILTER_X8 = 0x03,
159  FILTER_X16 = 0x04
160  };
161 
165  STANDBY_MS_1 = 0x00,
167  STANDBY_MS_63 = 0x01,
169  STANDBY_MS_125 = 0x02,
171  STANDBY_MS_250 = 0x03,
173  STANDBY_MS_500 = 0x04,
175  STANDBY_MS_1000 = 0x05,
177  STANDBY_MS_2000 = 0x06,
179  STANDBY_MS_4000 = 0x07
180  };
181 
182  Adafruit_BMP280(TwoWire *theWire = &Wire);
183  Adafruit_BMP280(int8_t cspin, SPIClass *theSPI = &SPI);
184  Adafruit_BMP280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin);
185  ~Adafruit_BMP280(void);
186 
187  bool begin(uint8_t addr = BMP280_ADDRESS, uint8_t chipid = BMP280_CHIPID);
188  void reset(void);
189  uint8_t getStatus(void);
190  uint8_t sensorID(void);
191 
192  float readTemperature();
193  float readPressure(void);
194  float readAltitude(float seaLevelhPa = 1013.25);
195  float seaLevelForAltitude(float altitude, float atmospheric);
196  float waterBoilingPoint(float pressure);
197  bool takeForcedMeasurement();
198 
199  Adafruit_Sensor *getTemperatureSensor(void);
200  Adafruit_Sensor *getPressureSensor(void);
201 
202  void setSampling(sensor_mode mode = MODE_NORMAL,
203  sensor_sampling tempSampling = SAMPLING_X16,
204  sensor_sampling pressSampling = SAMPLING_X16,
205  sensor_filter filter = FILTER_OFF,
206  standby_duration duration = STANDBY_MS_1);
207 
208 private:
209  TwoWire *_wire;
210  Adafruit_I2CDevice *i2c_dev = NULL;
211  Adafruit_SPIDevice *spi_dev = NULL;
212 
213  Adafruit_BMP280_Temp *temp_sensor = NULL;
214  Adafruit_BMP280_Pressure *pressure_sensor = NULL;
215 
217  struct config {
219  config() : t_sb(STANDBY_MS_1), filter(FILTER_OFF), none(0), spi3w_en(0) {}
221  unsigned int t_sb : 3;
223  unsigned int filter : 3;
225  unsigned int none : 1;
227  unsigned int spi3w_en : 1;
229  unsigned int get() { return (t_sb << 5) | (filter << 2) | spi3w_en; }
230  };
231 
233  struct ctrl_meas {
235  ctrl_meas()
236  : osrs_t(SAMPLING_NONE), osrs_p(SAMPLING_NONE), mode(MODE_SLEEP) {}
238  unsigned int osrs_t : 3;
240  unsigned int osrs_p : 3;
242  unsigned int mode : 2;
244  unsigned int get() { return (osrs_t << 5) | (osrs_p << 2) | mode; }
245  };
246 
247  void readCoefficients(void);
248  uint8_t spixfer(uint8_t x);
249  void write8(byte reg, byte value);
250  uint8_t read8(byte reg);
251  uint16_t read16(byte reg);
252  uint32_t read24(byte reg);
253  int16_t readS16(byte reg);
254  uint16_t read16_LE(byte reg);
255  int16_t readS16_LE(byte reg);
256 
257  uint8_t _i2caddr;
258 
259  int32_t _sensorID = 0;
260  int32_t t_fine;
261  // int8_t _cs, _mosi, _miso, _sck;
262  bmp280_calib_data _bmp280_calib;
263  config _configReg;
264  ctrl_meas _measReg;
265 };
266 
267 #endif
Adafruit_BMP280_Pressure(Adafruit_BMP280 *parent)
Create an Adafruit_Sensor compatible object for the pressure sensor.
Definition: Adafruit_BMP280.h:106
int16_t dig_P3
Definition: Adafruit_BMP280.h:76
Definition: Adafruit_BMP280.h:69
sensor_filter
Definition: Adafruit_BMP280.h:149
int16_t dig_P6
Definition: Adafruit_BMP280.h:79
bool getEvent(sensors_event_t *)
Gets the temperature as a standard sensor event.
Definition: Adafruit_BMP280.cpp:443
int16_t dig_P2
Definition: Adafruit_BMP280.h:75
int16_t dig_T3
Definition: Adafruit_BMP280.h:72
uint16_t dig_T1
Definition: Adafruit_BMP280.h:70
int16_t dig_T2
Definition: Adafruit_BMP280.h:71
Definition: Adafruit_BMP280.h:88
int16_t dig_P7
Definition: Adafruit_BMP280.h:80
standby_duration
Definition: Adafruit_BMP280.h:163
int16_t dig_P8
Definition: Adafruit_BMP280.h:81
sensor_sampling
Definition: Adafruit_BMP280.h:121
int16_t dig_P5
Definition: Adafruit_BMP280.h:78
uint16_t dig_P1
Definition: Adafruit_BMP280.h:74
Definition: Adafruit_BMP280.h:102
sensor_mode
Definition: Adafruit_BMP280.h:137
void getSensor(sensor_t *)
Gets the sensor_t data for the BMP280&#39;s temperature sensor.
Definition: Adafruit_BMP280.cpp:420
int16_t dig_P4
Definition: Adafruit_BMP280.h:77
Definition: Adafruit_BMP280.h:58
int16_t dig_P9
Definition: Adafruit_BMP280.h:82
Adafruit_BMP280_Temp(Adafruit_BMP280 *parent)
Create an Adafruit_Sensor compatible object for the temp sensor.
Definition: Adafruit_BMP280.h:92
#define BMP280_ADDRESS
Definition: Adafruit_BMP280.h:34
#define BMP280_CHIPID
Definition: Adafruit_BMP280.h:37
Definition: Adafruit_BMP280.h:118