Skip to content

Commit 11e5826

Browse files
committed
0.1.1 MS5837
1 parent c5a824c commit 11e5826

File tree

12 files changed

+565
-119
lines changed

12 files changed

+565
-119
lines changed

libraries/MS5837/.github/workflows/arduino-lint.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ jobs:
1010
- uses: arduino/arduino-lint-action@v1
1111
with:
1212
library-manager: update
13-
compliance: strict
13+
compliance: strict
14+

libraries/MS5837/.github/workflows/arduino_test_runner.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ jobs:
1313
with:
1414
ruby-version: 2.6
1515
- run: |
16+
sudo sysctl vm.mmap_rnd_bits=28
1617
gem install arduino_ci
1718
arduino_ci.rb

libraries/MS5837/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ 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.1.1] - 2025-04-07
10+
- update GitHub actions
11+
- update readme.md
12+
- add **int lastError()** to prepare error handling
13+
- add **uint32_t lastRead()**
14+
- add derived class **MS5803** to be able to set address.
15+
- minor edits
16+
17+
918
## [0.1.0] - 2023-12-24
1019
- initial release
1120

libraries/MS5837/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023-2024 Rob Tillaart
3+
Copyright (c) 2023-2025 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

libraries/MS5837/MS5837.cpp

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: MS5837.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// DATE: 2023-11-12
66
// PURPOSE: Arduino library for MS5837 temperature and pressure sensor.
77
// URL: https://github.com/RobTillaart/MS5837
@@ -50,10 +50,10 @@ bool MS5837::reset(uint8_t mathMode)
5050

5151
initConstants(mathMode);
5252

53-
// SKIP CRC check
53+
// SKIP CRC check (for now)
5454

5555
// derive the type from mathMode instead of the other way around.
56-
// user must
56+
// user must provide correct mathMode.
5757
_type = MS5837_TYPE_30;
5858
if (mathMode == 1) _type = MS5837_TYPE_02;
5959
if (mathMode == 2) _type = MS5803_TYPE_01;
@@ -75,19 +75,28 @@ uint8_t MS5837::getAddress()
7575
//
7676
// READ
7777
//
78+
// datasheet page 7
79+
// bits determines OSR => nr of samples => accuracy etc
7880
bool MS5837::read(uint8_t bits)
7981
{
8082
if (isConnected() == false) return false;
8183

82-
int index = constrain(bits, 8, 13);
83-
index -= 8;
84+
int OSR = constrain(bits, 8, 13);
85+
OSR -= 8;
86+
// datasheet page 2 OSR, ADC maximum conversion time.
8487
uint8_t waitMillis[6] = { 1, 2, 3, 5, 9, 18 };
85-
uint8_t wait = waitMillis[index];
86-
88+
uint8_t wait = waitMillis[OSR];
89+
8790
// D1 conversion
8891
_wire->beginTransmission(_address);
89-
_wire->write(MS5837_CMD_CONVERT_D1 + index * 2);
90-
_wire->endTransmission(); // TODO check all of these
92+
// datasheet page 7 adjust command byte based on OSR
93+
_wire->write(MS5837_CMD_CONVERT_D1 + OSR * 2);
94+
_error = _wire->endTransmission();
95+
if (_error != 0)
96+
{
97+
// _error = MS5837_I2C_ERROR ?
98+
return false;
99+
}
91100

92101
uint32_t start = millis();
93102

@@ -97,13 +106,19 @@ bool MS5837::read(uint8_t bits)
97106
yield();
98107
delay(1);
99108
}
100-
// NOTE: D1 and D2 are reserved in MBED (NANO BLE)
109+
// NOTE: names D1 and D2 are reserved in MBED (NANO BLE)
101110
uint32_t _D1 = readADC();
102111

103112
// D2 conversion
104113
_wire->beginTransmission(_address);
105-
_wire->write(MS5837_CMD_CONVERT_D2 + index * 2);
106-
_wire->endTransmission();
114+
// datasheet page 7 adjust command byte based on OSR
115+
_wire->write(MS5837_CMD_CONVERT_D2 + OSR * 2);
116+
_error = _wire->endTransmission();
117+
if (_error != 0)
118+
{
119+
// _error = MS5837_I2C_ERROR ?
120+
return false;
121+
}
107122

108123
start = millis();
109124
// while loop prevents blocking RTOS
@@ -113,7 +128,7 @@ bool MS5837::read(uint8_t bits)
113128
delay(1);
114129
}
115130

116-
// NOTE: D1 and D2 are reserved in MBED (NANO BLE)
131+
// NOTE: names D1 and D2 are reserved in MBED (NANO BLE)
117132
uint32_t _D2 = readADC();
118133

119134
float dT = _D2 - C[5];
@@ -140,32 +155,38 @@ bool MS5837::read(uint8_t bits)
140155
_pressure = (_D1 * sens * 4.76837158203E-7 - offset) * C[7] * 0.01;
141156
_temperature *= 0.01;
142157

158+
_lastRead = millis();
143159
return true;
144160
}
145161

162+
uint32_t MS5837::lastRead()
163+
{
164+
return _lastRead;
165+
}
166+
146167

147168
float MS5837::getPressure()
148169
{
149170
return _pressure;
150171
}
151172

173+
152174
float MS5837::getTemperature()
153175
{
154176
return _temperature;
155177
}
156178

157179

158180
// https://www.mide.com/air-pressure-at-altitude-calculator
159-
// https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702
160-
//
181+
// https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702 (stale link).
182+
// https://en.wikipedia.org/wiki/Pressure_altitude
161183
float MS5837::getAltitude(float airPressure)
162184
{
163185
float ratio = _pressure / airPressure;
164-
return 44330 * (1 - pow(ratio, 0.190294957));
186+
return 44330 * (1 - pow(ratio, 0.190294957));
165187
}
166188

167189

168-
169190
//////////////////////////////////////////////////////////////////////
170191
//
171192
// DENSITY for depth
@@ -186,11 +207,23 @@ float MS5837::getDepth(float airPressure)
186207
// 1 / (_density * 9.80665 * 10) can be pre-calculated and cached in setDensity.
187208
//
188209
// delta P = rho * g * h => h = delta P / rho * g
189-
// pressure = mbar, density grams/cm3 => correction factor 0.1 (=1/10)
210+
// pressure = mbar,
211+
// density grams/cm3 => correction factor 0.1 (= 1/10)
190212
return (_pressure - airPressure)/(_density * 9.80665 * 10);
191213
}
192214

193215

216+
//////////////////////////////////////////////////////////////////////
217+
//
218+
// ERROR
219+
//
220+
int MS5837::getLastError()
221+
{
222+
int e = _error;
223+
_error = 0;
224+
return e;
225+
}
226+
194227

195228
//////////////////////////////////////////////////////////////////////
196229
//
@@ -213,17 +246,17 @@ void MS5837::initConstants(uint8_t mathMode)
213246
//
214247
// datasheet MS5837_30 page 7
215248
//
216-
// mathMode = 0; | = 1
217-
C[0] = 1;
218-
C[1] = 32768L; // SENSt1 = C[1] * 2^15 | * 2^16
219-
C[2] = 65536L; // OFFt1 = C[2] * 2^16 | * 2^17
220-
C[3] = 3.90625E-3; // TCS = C[3] / 2^8 | / 2^7
221-
C[4] = 7.8125E-3; // TCO = C[4] / 2^7 | / 2^6
222-
C[5] = 256; // Tref = C[5] * 2^8 | * 2^8
223-
C[6] = 1.1920928955E-7; // TEMPSENS = C[6] / 2^23 | / 2^23
224-
C[7] = 1.220703125E-4; // compensate uses / 2^13 | / 2^15
225-
226-
// Appnote version for pressure.
249+
// mathMode = 0 | = 1 | = 2 |
250+
C[0] = 1; // manufacturer
251+
C[1] = 32768L; // SENSt1 = C[1] * 2^15 | * 2^16 | * 2^15 |
252+
C[2] = 65536L; // OFFt1 = C[2] * 2^16 | * 2^17 | * 2^16 |
253+
C[3] = 3.90625E-3; // TCS = C[3] / 2^8 | / 2^7 | / 2^8 |
254+
C[4] = 7.8125E-3; // TCO = C[4] / 2^7 | / 2^6 | / 2^7 |
255+
C[5] = 256; // Tref = C[5] * 2^8 | * 2^8 | * 2^8 |
256+
C[6] = 1.1920928955E-7; // TEMPSENS = C[6] / 2^23 | / 2^23 | / 2^23 |
257+
C[7] = 1.220703125E-4; // compensate uses / 2^13 | / 2^15 | / 2^15 |
258+
259+
// App note version for pressure.
227260
// adjustments for MS5837_02
228261
if (mathMode == 1)
229262
{
@@ -275,5 +308,25 @@ uint32_t MS5837::readADC()
275308
}
276309

277310

311+
//////////////////////////////////////////////////////////////////////
312+
//
313+
// DERIVED CLASSES
314+
//
315+
316+
//////////////////////////////////////////////////////////////////////
317+
//
318+
// MS5803
319+
//
320+
MS5803::MS5803(TwoWire *wire):MS5837(wire)
321+
{
322+
}
323+
324+
MS5803::MS5803(uint32_t address, TwoWire *wire)
325+
{
326+
_address = address;
327+
_wire = wire;
328+
}
329+
330+
278331
// -- END OF FILE --
279332

libraries/MS5837/MS5837.h

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: MS5837.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.0
5+
// VERSION: 0.1.1
66
// DATE: 2023-11-12
77
// PURPOSE: Arduino library for MS5837 temperature and pressure sensor.
88
// URL: https://github.com/RobTillaart/MS5837
@@ -12,12 +12,10 @@
1212
#include "Wire.h"
1313

1414

15-
// TODO OSR factor in code?
16-
17-
18-
#define MS5837_LIB_VERSION (F("0.1.0"))
15+
#define MS5837_LIB_VERSION (F("0.1.1"))
1916

2017

18+
// TYPES
2119
#define MS5803_TYPE_01 1
2220
#define MS5837_TYPE_02 2
2321
#define MS5837_TYPE_30 30
@@ -28,30 +26,34 @@ class MS5837
2826
public:
2927
MS5837(TwoWire *wire = &Wire);
3028

31-
// MS5837-30bar = 0
32-
// MS5837-02bar = 1
33-
bool begin(uint8_t mathMode);
34-
bool isConnected();
35-
bool reset(uint8_t mathMode);
36-
uint8_t getType();
37-
uint8_t getAddress();
29+
// MS5837_30 bar = 0
30+
// MS5837_02 bar = 1
31+
// MS5803_02 bar = 2
32+
bool begin(uint8_t mathMode);
33+
bool isConnected();
34+
bool reset(uint8_t mathMode);
35+
uint8_t getType();
36+
uint8_t getAddress();
3837

3938

4039
//////////////////////////////////////////////////////////////////////
4140
//
4241
// READ
4342
//
44-
// call will block 3-40 milliseconds, depends on # bits.
45-
// bits = 8-12 (8-13 for the MS5837_02)
46-
bool read(uint8_t bits = 8);
43+
// call will block 3-40 milliseconds, depends on # bits.
44+
// bits = 8-12 for the MS5803_02
45+
// bits = 8-13 for the MS5837_02 and MS5837_30)
46+
bool read(uint8_t bits = 8);
47+
uint32_t lastRead();
48+
4749
// see https://github.com/RobTillaart/pressure for conversions.
4850
// returns mBar
49-
float getPressure();
51+
float getPressure();
5052
// see https://github.com/RobTillaart/temperature for conversions.
5153
// returns Celsius
52-
float getTemperature();
53-
// compensate for actual air pressure if needed
54-
float getAltitude(float airPressure = 1013.25);
54+
float getTemperature();
55+
// compensate for actual air pressure if needed
56+
float getAltitude(float airPressure = 1013.25);
5557

5658

5759
//////////////////////////////////////////////////////////////////////
@@ -64,11 +66,18 @@ class MS5837
6466
// density water 20°C = 0.99802
6567
// density seawater is
6668
// density in grams / cm3 (so not in grams per liter
67-
void setDensity(float density = 0.99802);
68-
float getDensity();
69-
// returns meters (SI unit)
70-
// compensate for actual air pressure if needed
71-
float getDepth(float airPressure = 1013.25);
69+
void setDensity(float density = 0.99802);
70+
float getDensity();
71+
// returns meters (SI unit)
72+
// compensate for actual air pressure if needed
73+
float getDepth(float airPressure = 1013.25);
74+
75+
76+
//////////////////////////////////////////////////////////////////////
77+
//
78+
// ERROR (experimental)
79+
//
80+
int getLastError();
7281

7382

7483
protected:
@@ -77,7 +86,7 @@ class MS5837
7786
uint32_t readADC();
7887

7988

80-
uint8_t _address = 0x76; // fixed address
89+
uint8_t _address = 0x76;
8190
TwoWire * _wire = NULL;
8291

8392
float _pressure;
@@ -88,6 +97,9 @@ class MS5837
8897
int _result;
8998

9099
float _density = 0.99802; // water at 20 °C
100+
// prepare error handling.
101+
int _error = 0;
102+
uint32_t _lastRead;
91103
};
92104

93105

@@ -96,7 +108,23 @@ class MS5837
96108
// DERIVED CLASSES
97109
//
98110

99-
// MS5837_30 MS5837_02 etc ?
111+
//////////////////////////////////////////////////////////////////////
112+
//
113+
// MS5803
114+
//
115+
class MS5803 : public MS5837
116+
{
117+
public:
118+
MS5803(TwoWire *wire = &Wire);
119+
MS5803(uint32_t address, TwoWire *wire = &Wire);
120+
};
121+
122+
123+
//////////////////////////////////////////////////////////////////////
124+
//
125+
// MS5837_30
126+
// MS5837_02
127+
//
100128

101129

102130
// -- END OF FILE --

0 commit comments

Comments
 (0)