Skip to content

Commit 45c2486

Browse files
committed
0.4.0 MS5611_SPI
1 parent e81d972 commit 45c2486

File tree

19 files changed

+220
-149
lines changed

19 files changed

+220
-149
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ jobs:
66
runs-on: ubuntu-latest
77
timeout-minutes: 5
88
steps:
9-
- uses: actions/checkout@v4
10-
- uses: arduino/arduino-lint-action@v1
9+
- uses: actions/checkout@v5
10+
- uses: arduino/arduino-lint-action@v2
1111
with:
1212
library-manager: update
1313
compliance: strict

libraries/MS5611_SPI/.github/workflows/arduino_test_runner.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
timeout-minutes: 20
99

1010
steps:
11-
- uses: actions/checkout@v4
11+
- uses: actions/checkout@v5
1212
- uses: ruby/setup-ruby@v1
1313
with:
1414
ruby-version: 2.6

libraries/MS5611_SPI/.github/workflows/jsoncheck.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ on:
55
paths:
66
- '**.json'
77
pull_request:
8+
paths:
9+
- '**.json'
810

911
jobs:
1012
test:
1113
runs-on: ubuntu-latest
1214
timeout-minutes: 5
1315
steps:
14-
- uses: actions/checkout@v4
16+
- uses: actions/checkout@v5
1517
- name: json-syntax-check
1618
uses: limitusus/json-syntax-check@v2
1719
with:

libraries/MS5611_SPI/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.4.0] - 2025-09-26
10+
- fix #14, sync MS5611
11+
- add **getAltitudeFeet(float airPressure)**
12+
- moved code to .cpp
13+
- update keywords.txt
14+
- minor edits
15+
16+
----
17+
918
## [0.3.1] - 2024-06-03
1019
- add support for ARDUINO_ARCH_MBED
1120

libraries/MS5611_SPI/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) 2014-2024 Rob Tillaart
3+
Copyright (c) 2014-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/MS5611_SPI/MS5611_SPI.cpp

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//
22
// FILE: MS5611_SPI.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.3.1
5-
// PURPOSE: MS5611 (SPI) Temperature & Pressure library for Arduino
4+
// VERSION: 0.4.0
5+
// PURPOSE: Arduino library for MS5611 (SPI) temperature and pressure sensor
66
// URL: https://github.com/RobTillaart/MS5611_SPI
7-
//
8-
// HISTORY: see changelog.md
97

108

119
#include "MS5611_SPI.h"
@@ -96,7 +94,7 @@ bool MS5611_SPI::begin()
9694
digitalWrite(_clock, LOW);
9795
}
9896

99-
return reset();
97+
return reset(0); // MS5611 has mathMode 0, see datasheet + initConstants.
10098
}
10199

102100

@@ -111,8 +109,9 @@ bool MS5611_SPI::reset(uint8_t mathMode)
111109
{
112110
command(MS5611_CMD_RESET);
113111
uint32_t start = micros();
112+
114113
// while loop prevents blocking RTOS
115-
while (micros() - start < 3000) // increased as first ROM values were missed.
114+
while (micros() - start < 3000) // increased as first ROM values were missed.
116115
{
117116
yield();
118117
delayMicroseconds(10);
@@ -130,7 +129,7 @@ bool MS5611_SPI::reset(uint8_t mathMode)
130129
// C[7] == CRC - skipped.
131130
uint16_t tmp = readProm(reg);
132131
C[reg] *= tmp;
133-
// _deviceID is a simple SHIFT XOR merge of PROM data
132+
// _deviceID is a SHIFT XOR merge of 7 PROM registers, reasonable unique
134133
_deviceID <<= 4;
135134
_deviceID ^= tmp;
136135
// Serial.println(readProm(reg));
@@ -147,16 +146,22 @@ int MS5611_SPI::read(uint8_t bits)
147146
{
148147
// VARIABLES NAMES BASED ON DATASHEET
149148
// ALL MAGIC NUMBERS ARE FROM DATASHEET
149+
150150
convert(MS5611_CMD_CONVERT_D1, bits);
151+
if (_result) return _result;
151152
// NOTE: D1 and D2 seem reserved in MBED (NANO BLE)
152153
uint32_t _D1 = readADC();
154+
if (_result) return _result;
155+
153156
convert(MS5611_CMD_CONVERT_D2, bits);
157+
if (_result) return _result;
154158
uint32_t _D2 = readADC();
159+
if (_result) return _result;
155160

156161
// Serial.println(_D1);
157162
// Serial.println(_D2);
158163

159-
// TEST VALUES - comment lines above
164+
// TEST VALUES - comment lines above
160165
// uint32_t _D1 = 9085466;
161166
// uint32_t _D2 = 8569150;
162167

@@ -218,13 +223,21 @@ float MS5611_SPI::getTemperature() const
218223
}
219224

220225

226+
// milliBar
221227
float MS5611_SPI::getPressure() const
222228
{
223229
if (_pressureOffset == 0) return _pressure * 0.01;
224230
return _pressure * 0.01 + _pressureOffset;
225231
}
226232

227233

234+
// Pascal SI-unit.
235+
float MS5611_SPI::getPressurePascal() const
236+
{
237+
if (_pressureOffset == 0) return _pressure;
238+
return _pressure + _pressureOffset * 100.0;
239+
}
240+
228241
void MS5611_SPI::setPressureOffset(float offset)
229242
{
230243
_pressureOffset = offset;
@@ -249,6 +262,25 @@ float MS5611_SPI::getTemperatureOffset()
249262
}
250263

251264

265+
// (from MS5837)
266+
// https://www.mide.com/air-pressure-at-altitude-calculator
267+
// https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702 (stale link).
268+
// https://en.wikipedia.org/wiki/Pressure_altitude
269+
float MS5611_SPI::getAltitude(float airPressure)
270+
{
271+
// _pressure is in Pascal (#44) and airPressure in mBar.
272+
float ratio = _pressure * 0.01 / airPressure;
273+
return 44307.694 * (1 - pow(ratio, 0.190284));
274+
}
275+
276+
277+
float MS5611_SPI::getAltitudeFeet(float airPressure)
278+
{
279+
float ratio = _pressure * 0.01 / airPressure;
280+
return 145366.45 * (1 - pow(ratio, 0.190284));
281+
}
282+
283+
252284
int MS5611_SPI::getLastResult() const
253285
{
254286
return _result;
@@ -291,6 +323,18 @@ uint16_t MS5611_SPI::getSerialCode()
291323
return readProm(7) >> 4;
292324
}
293325

326+
// DEVELOP
327+
uint16_t MS5611_SPI::getProm(uint8_t index)
328+
{
329+
return readProm(index);
330+
}
331+
332+
// DEVELOP
333+
uint16_t MS5611_SPI::getCRC()
334+
{
335+
return readProm(7) & 0x0F;
336+
}
337+
294338

295339
void MS5611_SPI::setSPIspeed(uint32_t speed)
296340
{
@@ -311,23 +355,22 @@ bool MS5611_SPI::usesHWSPI()
311355
}
312356

313357

314-
315358
/////////////////////////////////////////////////////
316359
//
317-
// PRIVATE
360+
// PROTECTED
318361
//
319362
void MS5611_SPI::convert(const uint8_t addr, uint8_t bits)
320363
{
321-
// values from page 3 datasheet - MAX column (rounded up)
322-
uint16_t del[5] = {600, 1200, 2300, 4600, 9100};
323-
324364
uint8_t index = bits;
325365
if (index < 8) index = 8;
326366
else if (index > 12) index = 12;
327367
index -= 8;
328368
uint8_t offset = index * 2;
329369
command(addr + offset);
330370

371+
// values from page 3 datasheet - MAX column (rounded up)
372+
uint16_t del[5] = {600, 1200, 2300, 4600, 9100};
373+
331374
uint16_t waitTime = del[index];
332375
uint32_t start = micros();
333376
// while loop prevents blocking RTOS
@@ -467,5 +510,12 @@ void MS5611_SPI::initConstants(uint8_t mathMode)
467510
}
468511

469512

513+
///////////////////////////////////////////////////////////////////
514+
//
515+
// DERIVED CLASSES
516+
//
517+
// TODO ?
518+
519+
470520
// -- END OF FILE --
471521

libraries/MS5611_SPI/MS5611_SPI.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//
33
// FILE: MS5611_SPI.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.3.1
6-
// PURPOSE: S5611 (SPI) Temperature & Pressure library for Arduino
5+
// VERSION: 0.4.0
6+
// PURPOSE: Arduino library for MS5611 (SPI) temperature and pressure sensor
77
// URL: https://github.com/RobTillaart/MS5611_SPI
88

99

@@ -30,7 +30,7 @@
3030
// CS to GND ==> 0x77
3131

3232

33-
#define MS5611_SPI_LIB_VERSION (F("0.3.1 EXPERIMENTAL"))
33+
#define MS5611_SPI_LIB_VERSION (F("0.4.0 EXPERIMENTAL"))
3434

3535
#ifndef __SPI_CLASS__
3636
// MBED must be tested before RP2040
@@ -72,7 +72,7 @@ class MS5611_SPI
7272

7373
// reset command + get constants
7474
// mathMode = 0 (default), 1 = factor 2 fix.
75-
// returns false if ROM constants == 0;
75+
// returns false if ROM constants are 0;
7676
bool reset(uint8_t mathMode = 0);
7777

7878
// the actual reading of the sensor;
@@ -87,40 +87,54 @@ class MS5611_SPI
8787
// oversampling rate is in osr_t
8888
osr_t getOversampling() const;
8989

90-
// temperature is in ²C
90+
// temperature is in degrees C
9191
float getTemperature() const;
92-
9392
// pressure is in mBar
9493
float getPressure() const;
94+
// pressure is in Pascal (SI-unit)
95+
float getPressurePascal() const;
9596

9697
// OFFSET
98+
// pressure offset is in mBar.
9799
void setPressureOffset(float offset = 0);
98100
float getPressureOffset();
101+
// temperature offset in degrees C.
99102
void setTemperatureOffset(float offset = 0);
100103
float getTemperatureOffset();
101104

105+
// ALTITUDE (from MS5837)
106+
// air pressure in mBar, returns meters
107+
float getAltitude(float airPressure = 1013.25);
108+
// idem, returns feet.
109+
float getAltitudeFeet(float airPressure = 1013.25);
110+
102111
// to check for failure
103112
int getLastResult() const;
104113

105114
// last time in millis() when the sensor has been read.
106115
uint32_t lastRead() const;
107116

117+
// _deviceID is a SHIFT XOR merge of 7 PROM registers, reasonable unique
108118
uint32_t getDeviceID() const;
109119

110120
void setCompensation(bool flag = true);
111121
bool getCompensation();
112122

123+
// EXPERIMENTAL
124+
uint16_t getManufacturer();
125+
uint16_t getSerialCode();
126+
127+
// DEVELOP
128+
uint16_t getProm(uint8_t index);
129+
uint16_t getCRC();
130+
113131
// develop functions.
114132
/*
115133
void setAddress(uint8_t address) { _address = address; }; // RANGE CHECK
116134
uint8_t getAddress() const { return _address; };
117135
uint8_t detectAddress() { todo }; // works with only one on the bus?
118136
*/
119137

120-
// EXPERIMENTAL
121-
uint16_t getManufacturer();
122-
uint16_t getSerialCode();
123-
124138

125139
// speed in Hz
126140
void setSPIspeed(uint32_t speed);
@@ -144,7 +158,7 @@ class MS5611_SPI
144158
float _pressureOffset;
145159
float _temperatureOffset;
146160
int _result;
147-
float C[7];
161+
float C[7]; // constants, name from datasheet
148162
uint32_t _lastRead;
149163
uint32_t _deviceID;
150164
bool _compensation;
@@ -162,5 +176,13 @@ class MS5611_SPI
162176
};
163177

164178

179+
180+
///////////////////////////////////////////////////////////////////
181+
//
182+
// DERIVED CLASSES
183+
//
184+
// class MS5607_SPI : public MS5611_SPI ??
185+
186+
165187
// -- END OF FILE --
166188

0 commit comments

Comments
 (0)