Erriez BMP280 / BME280 library for Arduino  1.0.1
This is a BMP280/BME280 temperature/pressure/humidity sensor library for Arduino.
ErriezBMX280.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2020 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
36 #ifndef ERRIEZ_BME280_H_
37 #define ERRIEZ_BME280_H_
38 
39 #include <Arduino.h>
40 #include <Wire.h>
41 
42 // I2C address
43 #define BMX280_I2C_ADDR 0x76
44 #define BMX280_I2C_ADDR_ALT 0x77
45 
46 // Register defines
47 #define BMX280_REG_DIG_T1 0x88
48 #define BMX280_REG_DIG_T2 0x8A
49 #define BMX280_REG_DIG_T3 0x8C
50 
51 #define BMX280_REG_DIG_P1 0x8E
52 #define BMX280_REG_DIG_P2 0x90
53 #define BMX280_REG_DIG_P3 0x92
54 #define BMX280_REG_DIG_P4 0x94
55 #define BMX280_REG_DIG_P5 0x96
56 #define BMX280_REG_DIG_P6 0x98
57 #define BMX280_REG_DIG_P7 0x9A
58 #define BMX280_REG_DIG_P8 0x9C
59 #define BMX280_REG_DIG_P9 0x9E
60 
61 #define BME280_REG_DIG_H1 0xA1
62 #define BME280_REG_DIG_H2 0xE1
63 #define BME280_REG_DIG_H3 0xE3
64 #define BME280_REG_DIG_H4 0xE4
65 #define BME280_REG_DIG_H5 0xE5
66 #define BME280_REG_DIG_H6 0xE7
67 
68 #define BME280_REG_CHIPID 0xD0
69 #define BME280_REG_RESET 0xE0
70 
71 #define BME280_REG_CTRL_HUM 0xF2
72 #define BMX280_REG_STATUS 0XF3
73 #define BMX280_REG_CTRL_MEAS 0xF4
74 #define BMX280_REG_CONFIG 0xF5
75 #define BMX280_REG_PRESS 0xF7
76 #define BMX280_REG_TEMP 0xFA
77 #define BME280_REG_HUM 0xFD
78 
79 // Bit defines
80 #define CHIP_ID_BMP280 0x58
81 #define CHIP_ID_BME280 0x60
82 #define RESET_KEY 0xB6
83 #define STATUS_IM_UPDATE 0
84 
85 
88 typedef enum {
93 
97 typedef enum {
105 
109 typedef enum {
116 
120 typedef enum {
130 
135 {
136 public:
137  // Constructor
138  ErriezBMX280(uint8_t i2cAddr);
139 
140  // Initialization
141  bool begin();
142  uint8_t getChipID();
143 
144  // BMP280/BME280
145  float readTemperature();
146  float readPressure();
147  float readAltitude(float seaLevel);
148 
149  // BME280 only
150  float readHumidity();
151 
152  // Configuration
154  BMX280_Sampling_e tempSampling = BMX280_SAMPLING_X16,
155  BMX280_Sampling_e pressSampling = BMX280_SAMPLING_X16,
158  BMX280_Standby_e standbyDuration = BMX280_STANDBY_MS_0_5);
159 
160  // Register access
161  uint8_t read8(uint8_t reg);
162  uint16_t read16(uint8_t reg);
163  uint16_t read16_LE(uint8_t reg); // little endian unsigned
164  int16_t readS16_LE(uint8_t reg); // little endian signed
165  uint32_t read24(uint8_t reg);
166  void write8(uint8_t reg, uint8_t value);
167 
168 private:
169  uint8_t _i2cAddr;
170  uint8_t _chipID;
171  int32_t _t_fine;
172 
173  uint16_t _dig_T1;
174  int16_t _dig_T2;
175  int16_t _dig_T3;
176 
177  uint16_t _dig_P1;
178  int16_t _dig_P2;
179  int16_t _dig_P3;
180  int16_t _dig_P4;
181  int16_t _dig_P5;
182  int16_t _dig_P6;
183  int16_t _dig_P7;
184  int16_t _dig_P8;
185  int16_t _dig_P9;
186 
187  uint8_t _dig_H1;
188  int16_t _dig_H2;
189  uint8_t _dig_H3;
190  int16_t _dig_H4;
191  int16_t _dig_H5;
192  int8_t _dig_H6;
193 
194  // Read coefficient registers
195  void readCoefficients(void);
196 };
197 
198 #endif // ERRIEZ_BMX280_H_
ErriezBMX280(uint8_t i2cAddr)
Constructor.
Sleep mode.
Definition: ErriezBMX280.h:89
uint8_t getChipID()
Get chip ID.
500ms standby
Definition: ErriezBMX280.h:127
x2 Sampling
Definition: ErriezBMX280.h:100
uint8_t read8(uint8_t reg)
Read from 8-bit register.
Normal mode.
Definition: ErriezBMX280.h:91
BMX280_Standby_e
Standby duration bits config register.
Definition: ErriezBMX280.h:120
int16_t readS16_LE(uint8_t reg)
Read from 16-bit signed register little endian.
void write8(uint8_t reg, uint8_t value)
Write to 8-bit register.
BMX280_Sampling_e
Sampling bits registers ctrl_hum, ctrl_meas.
Definition: ErriezBMX280.h:97
Forced mode.
Definition: ErriezBMX280.h:90
125ms standby
Definition: ErriezBMX280.h:125
uint16_t read16_LE(uint8_t reg)
Read from 16-bit unsigned register little endian.
x1 Sampling
Definition: ErriezBMX280.h:99
BMX280_Filter_e
Filter bits config register.
Definition: ErriezBMX280.h:109
float readPressure()
Read pressure.
float readTemperature()
Read temperature.
uint16_t read16(uint8_t reg)
Read from 16-bit register.
Filter off.
Definition: ErriezBMX280.h:110
uint32_t read24(uint8_t reg)
Read from 24-bit register.
x4 Sampling
Definition: ErriezBMX280.h:101
250ms standby
Definition: ErriezBMX280.h:126
float readHumidity()
Read humidity (BME280 only)
BMX280 class.
Definition: ErriezBMX280.h:134
float readAltitude(float seaLevel)
Read approximate altitude.
bool begin()
Sensor initialization.
void setSampling(BMX280_Mode_e mode=BMX280_MODE_NORMAL, BMX280_Sampling_e tempSampling=BMX280_SAMPLING_X16, BMX280_Sampling_e pressSampling=BMX280_SAMPLING_X16, BMX280_Sampling_e humSampling=BMX280_SAMPLING_X16, BMX280_Filter_e filter=BMX280_FILTER_OFF, BMX280_Standby_e standbyDuration=BMX280_STANDBY_MS_0_5)
Set sampling registers.
Sampling disabled.
Definition: ErriezBMX280.h:98
x16 Sampling
Definition: ErriezBMX280.h:103
x8 Sampling
Definition: ErriezBMX280.h:102
BMX280_Mode_e
Sleep mode bits ctrl_meas register.
Definition: ErriezBMX280.h:88