Skip to content

Commit e81d972

Browse files
committed
0.5.0 MC5611
1 parent f236339 commit e81d972

File tree

9 files changed

+162
-67
lines changed

9 files changed

+162
-67
lines changed

libraries/MS5611/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.5.0] - 2025-09-26
10+
- fix #44, getAltitude()
11+
- sync MS5611_SPI
12+
- add **getAltitudeFeet(float airPressure)**
13+
- moved code to .cpp
14+
- update keywords.txt
15+
- minor edits
16+
17+
----
18+
919
## [0.4.2] - 2025-08-31
1020
- fix #42, add reference to STM32 version of library
1121
- add **float getAltitude(float airPressure = 1013.25)**

libraries/MS5611/MS5611.cpp

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// FILE: MS5611.cpp
33
// AUTHOR: Rob Tillaart
44
// Erni - testing/fixes
5-
// VERSION: 0.4.2
6-
// PURPOSE: Arduino library for MS5611 temperature and pressure sensor
5+
// VERSION: 0.5.0
6+
// PURPOSE: Arduino library for MS5611 (I2C) temperature and pressure sensor
77
// URL: https://github.com/RobTillaart/MS5611
88

99

@@ -64,7 +64,7 @@ bool MS5611::reset(uint8_t mathMode)
6464
uint32_t start = micros();
6565

6666
// while loop prevents blocking RTOS
67-
while (micros() - start < 2800)
67+
while (micros() - start < 3000) // increased as first ROM values were missed.
6868
{
6969
yield();
7070
delayMicroseconds(10);
@@ -163,6 +163,12 @@ void MS5611::setOversampling(osr_t samplingRate)
163163
}
164164

165165

166+
osr_t MS5611::getOversampling() const
167+
{
168+
return (osr_t) _samplingRate;
169+
}
170+
171+
166172
float MS5611::getTemperature() const
167173
{
168174
if (_temperatureOffset == 0) return _temperature * 0.01;
@@ -185,18 +191,79 @@ float MS5611::getPressurePascal() const
185191
return _pressure + _pressureOffset * 100.0;
186192
}
187193

194+
void MS5611::setPressureOffset(float offset)
195+
{
196+
_pressureOffset = offset;
197+
}
198+
199+
200+
float MS5611::getPressureOffset()
201+
{
202+
return _pressureOffset;
203+
}
204+
205+
206+
void MS5611::setTemperatureOffset(float offset)
207+
{
208+
_temperatureOffset = offset;
209+
}
210+
211+
212+
float MS5611::getTemperatureOffset()
213+
{
214+
return _temperatureOffset;
215+
}
216+
188217

189218
// (from MS5837)
190219
// https://www.mide.com/air-pressure-at-altitude-calculator
191220
// https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702 (stale link).
192221
// https://en.wikipedia.org/wiki/Pressure_altitude
193222
float MS5611::getAltitude(float airPressure)
194223
{
195-
float ratio = _pressure / airPressure;
224+
// _pressure is in Pascal (#44) and airPressure in mBar.
225+
float ratio = _pressure * 0.01 / airPressure;
196226
return 44307.694 * (1 - pow(ratio, 0.190284));
197227
}
198228

199229

230+
float MS5611::getAltitudeFeet(float airPressure)
231+
{
232+
float ratio = _pressure * 0.01 / airPressure;
233+
return 145366.45 * (1 - pow(ratio, 0.190284));
234+
}
235+
236+
237+
int MS5611::getLastResult() const
238+
{
239+
return _result;
240+
}
241+
242+
243+
uint32_t MS5611::lastRead() const
244+
{
245+
return _lastRead;
246+
}
247+
248+
249+
uint32_t MS5611::getDeviceID() const
250+
{
251+
return _deviceID;
252+
}
253+
254+
255+
void MS5611::setCompensation(bool flag)
256+
{
257+
_compensation = flag;
258+
}
259+
260+
261+
bool MS5611::getCompensation()
262+
{
263+
return _compensation;
264+
}
265+
266+
200267
// EXPERIMENTAL
201268
uint16_t MS5611::getManufacturer()
202269
{
@@ -215,6 +282,7 @@ uint16_t MS5611::getProm(uint8_t index)
215282
return readProm(index);
216283
}
217284

285+
// DEVELOP
218286
uint16_t MS5611::getCRC()
219287
{
220288
return readProm(7) & 0x0F;
@@ -227,16 +295,16 @@ uint16_t MS5611::getCRC()
227295
//
228296
void MS5611::convert(const uint8_t addr, uint8_t bits)
229297
{
230-
// values from page 3 datasheet - MAX column (rounded up)
231-
uint16_t del[5] = {600, 1200, 2300, 4600, 9100};
232-
233298
uint8_t index = bits;
234299
if (index < 8) index = 8;
235300
else if (index > 12) index = 12;
236301
index -= 8;
237302
uint8_t offset = index * 2;
238303
command(addr + offset);
239304

305+
// values from page 3 datasheet - MAX column (rounded up)
306+
uint16_t del[5] = {600, 1200, 2300, 4600, 9100};
307+
240308
uint16_t waitTime = del[index];
241309
uint32_t start = micros();
242310
// while loop prevents blocking RTOS
@@ -317,12 +385,12 @@ void MS5611::initConstants(uint8_t mathMode)
317385
C[5] = 256; // Tref = C[5] * 2^8 | * 2^8
318386
C[6] = 1.1920928955E-7; // TEMPSENS = C[6] / 2^23 | / 2^23
319387

320-
if (mathMode == 1) // Appnote version for pressure.
388+
if (mathMode == 1) // Appnote version for pressure.
321389
{
322-
C[1] = 65536L; // SENSt1
323-
C[2] = 131072L; // OFFt1
324-
C[3] = 7.8125E-3; // TCS
325-
C[4] = 1.5625e-2; // TCO
390+
C[1] = 65536L; // SENSt1
391+
C[2] = 131072L; // OFFt1
392+
C[3] = 7.8125E-3; // TCS
393+
C[4] = 1.5625e-2; // TCO
326394
}
327395
}
328396

@@ -334,7 +402,7 @@ void MS5611::initConstants(uint8_t mathMode)
334402
MS5607::MS5607(uint8_t deviceAddress, TwoWire *wire)
335403
: MS5611(deviceAddress, wire)
336404
{
337-
};
405+
}
338406

339407

340408
bool MS5607::begin()
@@ -343,7 +411,8 @@ bool MS5607::begin()
343411
if (! isConnected()) return false;
344412

345413
return reset(1); // MS5607 has mathMode 1, see datasheet + initConstants.
346-
};
414+
}
415+
347416

348417
// -- END OF FILE --
349418

libraries/MS5611/MS5611.h

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// FILE: MS5611.h
44
// AUTHOR: Rob Tillaart
55
// Erni - testing/fixes
6-
// VERSION: 0.4.2
7-
// PURPOSE: Arduino library for MS5611 temperature and pressure sensor
6+
// VERSION: 0.5.0
7+
// PURPOSE: Arduino library for MS5611 (I2C) temperature and pressure sensor
88
// URL: https://github.com/RobTillaart/MS5611
99

1010

@@ -31,24 +31,25 @@
3131
// CS to GND ==> 0x77
3232

3333

34-
#define MS5611_LIB_VERSION (F("0.4.2"))
34+
#define MS5611_LIB_VERSION (F("0.5.0"))
3535

3636
#ifndef MS5611_DEFAULT_ADDRESS
3737
#define MS5611_DEFAULT_ADDRESS 0x77
3838
#endif
3939

40+
4041
#define MS5611_READ_OK 0
4142
#define MS5611_ERROR_2 2 // low level I2C error
4243
#define MS5611_NOT_READ -999
4344

4445

4546
enum osr_t
4647
{
47-
OSR_ULTRA_HIGH = 12, // 10 millis
48-
OSR_HIGH = 11, // 5 millis
49-
OSR_STANDARD = 10, // 3 millis
50-
OSR_LOW = 9, // 2 millis
51-
OSR_ULTRA_LOW = 8 // 1 millis Default = backwards compatible
48+
OSR_ULTRA_HIGH = 12, // 10 millis
49+
OSR_HIGH = 11, // 5 millis
50+
OSR_STANDARD = 10, // 3 millis
51+
OSR_LOW = 9, // 2 millis
52+
OSR_ULTRA_LOW = 8 // 1 millis Default = backwards compatible
5253
};
5354

5455

@@ -76,40 +77,40 @@ class MS5611
7677
void setOversampling(osr_t samplingRate);
7778

7879
// oversampling rate is in osr_t
79-
osr_t getOversampling() const { return (osr_t) _samplingRate; };
80+
osr_t getOversampling() const;
8081

81-
// temperature is in ²C
82+
// temperature is in degrees C
8283
float getTemperature() const;
83-
8484
// pressure is in mBar
8585
float getPressure() const;
86-
// pressure is in Pascal (SI-unit) - 0.4.1
86+
// pressure is in Pascal (SI-unit)
8787
float getPressurePascal() const;
8888

89-
// OFFSET - 0.3.6
89+
// OFFSET
9090
// pressure offset is in mBar.
91-
void setPressureOffset(float offset = 0) { _pressureOffset = offset; };
92-
float getPressureOffset() { return _pressureOffset; };
91+
void setPressureOffset(float offset = 0);
92+
float getPressureOffset();
9393
// temperature offset in degrees C.
94-
void setTemperatureOffset(float offset = 0) { _temperatureOffset = offset; };
95-
float getTemperatureOffset() { return _temperatureOffset; };
94+
void setTemperatureOffset(float offset = 0);
95+
float getTemperatureOffset();
9696

9797
// ALTITUDE (from MS5837)
98+
// air pressure in mBar, returns meters
9899
float getAltitude(float airPressure = 1013.25);
99-
100+
// idem, returns feet.
101+
float getAltitudeFeet(float airPressure = 1013.25);
100102

101103
// to check for failure
102-
int getLastResult() const { return _result; };
104+
int getLastResult() const;
103105

104106
// last time in millis() when the sensor has been read.
105-
uint32_t lastRead() const { return _lastRead; };
107+
uint32_t lastRead() const;
106108

107109
// _deviceID is a SHIFT XOR merge of 7 PROM registers, reasonable unique
108-
uint32_t getDeviceID() const { return _deviceID; };
109-
110-
void setCompensation(bool flag = true) { _compensation = flag; };
111-
bool getCompensation() { return _compensation; };
110+
uint32_t getDeviceID() const;
112111

112+
void setCompensation(bool flag = true);
113+
bool getCompensation();
113114

114115
// EXPERIMENTAL
115116
uint16_t getManufacturer();
@@ -118,8 +119,10 @@ class MS5611
118119
// DEVELOP
119120
uint16_t getProm(uint8_t index);
120121
uint16_t getCRC();
122+
123+
// develop functions.
121124
/*
122-
void setAddress(uint8_t address) { _address = address; }; // RANGE CHECK + isConnected() !
125+
void setAddress(uint8_t address) { _address = address; }; // RANGE CHECK
123126
uint8_t detectAddress() { todo }; // works with only one on the bus?
124127
*/
125128

0 commit comments

Comments
 (0)