From 985713a0289d2fd1bc0b29f0304feea9bee4bbe7 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Fri, 14 Jan 2022 20:27:13 +0100 Subject: [PATCH 1/6] add offset functions, refactor --- MS5611.cpp | 53 +++++-- MS5611.h | 19 ++- .../MS5611_test_offset/MS5611_test_offset.ino | 134 ++++++++++++++++++ keywords.txt | 5 + library.json | 2 +- library.properties | 2 +- 6 files changed, 198 insertions(+), 17 deletions(-) create mode 100644 examples/MS5611_test_offset/MS5611_test_offset.ino diff --git a/MS5611.cpp b/MS5611.cpp index 26d3032..5c3c611 100644 --- a/MS5611.cpp +++ b/MS5611.cpp @@ -2,11 +2,12 @@ // FILE: MS5611.cpp // AUTHOR: Rob Tillaart // Erni - testing/fixes -// VERSION: 0.3.5 +// VERSION: 0.3.6 // PURPOSE: MS5611 Temperature & Humidity library for Arduino // URL: https://github.com/RobTillaart/MS5611 // // HISTORY: +// 0.3.6 2022-01-15 add setOffset functions; minor refactor; // 0.3.5 2022-01-13 fix isConnected() for NANO 33 BLE // 0.3.4 2021-12-29 fix #16 compilation for MBED // 0.3.3 2021-12-25 Update oversampling timings to reduce time spent waiting @@ -61,12 +62,14 @@ // MS5611::MS5611(uint8_t deviceAddress) { - _address = deviceAddress; - _samplingRate = OSR_ULTRA_LOW; - _temperature = MS5611_NOT_READ; - _pressure = MS5611_NOT_READ; - _result = MS5611_NOT_READ; - _lastRead = 0; + _address = deviceAddress; + _samplingRate = OSR_ULTRA_LOW; + _temperature = MS5611_NOT_READ; + _pressure = MS5611_NOT_READ; + _result = MS5611_NOT_READ; + _lastRead = 0; + _pressureOffset = 0; + _temperatureOffset = 0; } @@ -105,7 +108,10 @@ bool MS5611::begin(TwoWire * wire) bool MS5611::isConnected() { _wire->beginTransmission(_address); - _wire->write(0); // needed for NANO 33 BLE + #ifdef ARDUINO_ARCH_NRF52840 + // needed for NANO 33 BLE + _wire->write(0); + #endif return (_wire->endTransmission() == 0); } @@ -113,7 +119,12 @@ bool MS5611::isConnected() void MS5611::reset() { command(MS5611_CMD_RESET); - delayMicroseconds(2800); + uint32_t start = micros(); + while (micros() - start < 2800) // prevent blocking RTOS + { + yield(); + delayMicroseconds(10); + } // constants that were multiplied in read() // do this once and you save CPU cycles C[0] = 1; @@ -195,6 +206,21 @@ void MS5611::setOversampling(osr_t samplingRate) _samplingRate = (uint8_t) samplingRate; } + +float MS5611::getTemperature() const +{ + if (_temperatureOffset == 0) return _temperature * 0.01; + return _temperature * 0.01 + _temperatureOffset; +}; + + +float MS5611::getPressure() const +{ + if (_pressureOffset == 0) return _pressure * 0.01; + return _pressure * 0.01 + _pressureOffset; +}; + + ///////////////////////////////////////////////////// // // PRIVATE @@ -207,7 +233,14 @@ void MS5611::convert(const uint8_t addr, uint8_t bits) bits = constrain(bits, 8, 12); uint8_t offset = (bits - 8) * 2; command(addr + offset); - delayMicroseconds(del[offset/2]); + + uint32_t start = micros(); + uint16_t waitTime = del[offset/2]; + while (micros() - start < waitTime) // prevent blocking RTOS + { + yield(); + delayMicroseconds(10); + } } diff --git a/MS5611.h b/MS5611.h index 012652c..342036e 100644 --- a/MS5611.h +++ b/MS5611.h @@ -3,7 +3,7 @@ // FILE: MS5611.h // AUTHOR: Rob Tillaart // Erni - testing/fixes -// VERSION: 0.3.5 +// VERSION: 0.3.6 // PURPOSE: Arduino library for MS5611 temperature and pressure sensor // URL: https://github.com/RobTillaart/MS5611 @@ -29,7 +29,8 @@ // CS to VCC ==> 0x76 // CS to GND ==> 0x77 -#define MS5611_LIB_VERSION (F("0.3.5")) + +#define MS5611_LIB_VERSION (F("0.3.6")) #define MS5611_READ_OK 0 @@ -73,15 +74,21 @@ class MS5611 osr_t getOversampling() const { return (osr_t) _samplingRate; }; // temperature is in ²C - float getTemperature() const { return _temperature * 0.01; }; + float getTemperature() const; // pressure is in mBar - float getPressure() const { return _pressure * 0.01; }; + float getPressure() const; + + // OFFSET - 0.3.6 + void setPressureOffset(float offset = 0) { _pressureOffset = offset; }; + float getPressureOffset() { return _pressureOffset; }; + void setTemperatureOffset(float offset = 0) { _temperatureOffset = offset; }; + float getTemperatureOffset() { return _temperatureOffset; }; // to check for failure int getLastResult() const { return _result; }; - // last time in millis() that the sensor has been read. + // last time in millis() when the sensor has been read. uint32_t lastRead() { return _lastRead; }; @@ -95,6 +102,8 @@ class MS5611 uint8_t _samplingRate; int32_t _temperature; int32_t _pressure; + float _pressureOffset; + float _temperatureOffset; int _result; float C[7]; uint32_t _lastRead; diff --git a/examples/MS5611_test_offset/MS5611_test_offset.ino b/examples/MS5611_test_offset/MS5611_test_offset.ino new file mode 100644 index 0000000..e880853 --- /dev/null +++ b/examples/MS5611_test_offset/MS5611_test_offset.ino @@ -0,0 +1,134 @@ +// +// FILE: MS5611_test_offset.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo application +// DATE: 2022-01-15 +// URL: https://github.com/RobTillaart/MS5611 + + +#include "MS5611.h" + +// BREAKOUT MS5611 aka GY63 - see datasheet +// +// SPI I2C +// +--------+ +// VCC VCC | o | +// GND GND | o | +// SCL | o | +// SDI SDA | o | +// CSO | o | +// SDO | o L | L = led +// PS | o O | O = opening PS +// +--------+ +// +// PS to VCC ==> I2C +// PS to GND ==> SPI +// CS to VCC ==> 0x76 +// CS to GND ==> 0x77 + +MS5611 MS5611(0x76); // 0x76 = CSB to VCC; 0x77 = CSB to GND + + +uint32_t start, stop; + + +void setup() +{ + Serial.begin(115200); + while (!Serial); + + Serial.println(); + Serial.println(__FILE__); + Serial.print("MS5611_LIB_VERSION: "); + Serial.println(MS5611_LIB_VERSION); + + if (MS5611.begin() == true) + { + Serial.println("MS5611 found."); + } + else + { + Serial.println("MS5611 not found. halt."); + while (1) + { + digitalWrite(LED_BUILTIN, HIGH); + delay(1000); + digitalWrite(LED_BUILTIN, LOW); + delay(1000); + } + } + Serial.println(); + + MS5611.setTemperatureOffset(273.15); // set temp in Kelvin + MS5611.setPressureOffset(-1013); // set pressure relative to 1 ATM +} + + +/* + There are 5 oversampling settings, each corresponding to a different amount of milliseconds + The higher the oversampling, the more accurate the reading will be, however the longer it will take. + OSR_ULTRA_HIGH -> 8.22 millis + OSR_HIGH -> 4.11 millis + OSR_STANDARD -> 2.1 millis + OSR_LOW -> 1.1 millis + OSR_ULTRA_LOW -> 0.5 millis Default = backwards compatible +*/ +void loop() +{ + digitalWrite(LED_BUILTIN, HIGH); + MS5611.setOversampling(OSR_ULTRA_LOW); + test(); + digitalWrite(LED_BUILTIN, LOW); + delay(1000); + + digitalWrite(LED_BUILTIN, HIGH); + MS5611.setOversampling(OSR_LOW); + test(); + digitalWrite(LED_BUILTIN, LOW); + delay(1000); + + digitalWrite(LED_BUILTIN, HIGH); + MS5611.setOversampling(OSR_STANDARD); + test(); + digitalWrite(LED_BUILTIN, LOW); + delay(1000); + + digitalWrite(LED_BUILTIN, HIGH); + MS5611.setOversampling(OSR_HIGH); + test(); + digitalWrite(LED_BUILTIN, LOW); + delay(1000); + + digitalWrite(LED_BUILTIN, HIGH); + MS5611.setOversampling(OSR_ULTRA_HIGH); + test(); + digitalWrite(LED_BUILTIN, LOW); + delay(1000); + Serial.println(); +} + + +void test() +{ + start = micros(); + int result = MS5611.read(); + stop = micros(); + if (result != MS5611_READ_OK) + { + Serial.print("Error in read: "); + Serial.println(result); + } + else + { + Serial.print("T: "); + Serial.print(MS5611.getTemperature(), 2); + Serial.print("\tP: "); + Serial.print(MS5611.getPressure(), 2); + Serial.print("\tt: "); + Serial.print(stop - start); + Serial.println(); + } +} + + +// -- END OF FILE -- diff --git a/keywords.txt b/keywords.txt index b56c0a7..afc3609 100644 --- a/keywords.txt +++ b/keywords.txt @@ -17,6 +17,11 @@ getPressure KEYWORD2 getLastResult KEYWORD2 lastRead KEYWORD2 +setTemperatureOffset KEYWORD2 +getTemperatureOffset KEYWORD2 +setPressureOffset KEYWORD2 +getPressureOffset KEYWORD2 + # Instances (KEYWORD2) diff --git a/library.json b/library.json index f4dcd41..8bd23a6 100644 --- a/library.json +++ b/library.json @@ -18,7 +18,7 @@ "type": "git", "url": "https://github.com/RobTillaart/MS5611.git" }, - "version": "0.3.5", + "version": "0.3.6", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/library.properties b/library.properties index e87f01b..caab8e3 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=MS5611 -version=0.3.5 +version=0.3.6 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for MS5611 temperature and pressure sensor From 91663b1cfc833c4fa7733e71fd8c62e69dd1731a Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Fri, 14 Jan 2022 20:44:03 +0100 Subject: [PATCH 2/6] add offset functions, refactor --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ab69b43..6490d38 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,21 @@ See table below and test example how to use. - **uint32_t lastRead()** last time when **read()** was called in milliseconds since startup. -#### Oversampling table +### Offset + +The offset functions are added to calibrate the sensor against e.g. local weather station. This calibration can only be done runtime. + +- **void setPressureOffset(float offset = 0)** Set an offset to calibrate the pressure. +Can also be used to get the pressure relative to e.g. 1 Atm. (set offset to -1013 HPa). +Default the value is set to 0. +- **float getPressureOffset()** returns the current pressure offset. +- **void setTemperatureOffset(float offset = 0)** Set an offset to calibrate the temperature. +Can also be used to get the temperature in degrees Kelvin. (set offset to +273.15) +Default the value is set to 0. +- **float getTemperatureOffset()** returns the current temperature offset. + + +### Oversampling table (numbers from datasheet, actual time differs - todo) From 20b1b61da78bf8685fe9e35323ae951e39b50307 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Sat, 15 Jan 2022 12:32:46 +0100 Subject: [PATCH 3/6] refactor --- MS5611.cpp | 43 ++++++++++++--------- MS5611.h | 15 ++++---- README.md | 93 ++++++++++++++++++++++++++++------------------ library.json | 2 +- library.properties | 2 +- 5 files changed, 92 insertions(+), 63 deletions(-) diff --git a/MS5611.cpp b/MS5611.cpp index 5c3c611..6a232a8 100644 --- a/MS5611.cpp +++ b/MS5611.cpp @@ -120,7 +120,8 @@ void MS5611::reset() { command(MS5611_CMD_RESET); uint32_t start = micros(); - while (micros() - start < 2800) // prevent blocking RTOS + // while loop prevents blocking RTOS + while (micros() - start < 2800) { yield(); delayMicroseconds(10); @@ -227,16 +228,20 @@ float MS5611::getPressure() const // void MS5611::convert(const uint8_t addr, uint8_t bits) { - //Values from page 2 datasheet + // values from page 2 datasheet uint16_t del[5] = {500, 1100, 2100, 4100, 8220}; - bits = constrain(bits, 8, 12); - uint8_t offset = (bits - 8) * 2; + uint8_t index = bits; + if (index < 8) index = 8; + else if (index > 12) index = 12; + index -= 8; + uint8_t offset = index * 2; command(addr + offset); + uint16_t waitTime = del[index]; uint32_t start = micros(); - uint16_t waitTime = del[offset/2]; - while (micros() - start < waitTime) // prevent blocking RTOS + // while loop prevents blocking RTOS + while (micros() - start < waitTime) { yield(); delayMicroseconds(10); @@ -246,7 +251,7 @@ void MS5611::convert(const uint8_t addr, uint8_t bits) uint16_t MS5611::readProm(uint8_t reg) { - // last EEPROM register is CRC - Page13 datasheet. + // last EEPROM register is CRC - Page 13 datasheet. uint8_t promCRCRegister = 7; if (reg > promCRCRegister) return 0; @@ -254,12 +259,13 @@ uint16_t MS5611::readProm(uint8_t reg) command(MS5611_CMD_READ_PROM + offset); if (_result == 0) { - int nr = _wire->requestFrom(_address, (uint8_t)2); - if (nr >= 2) + uint8_t length = 2; + int bytes = _wire->requestFrom(_address, length); + if (bytes >= length) { - uint16_t val = _wire->read() * 256; - val += _wire->read(); - return val; + uint16_t value = _wire->read() * 256; + value += _wire->read(); + return value; } return 0; } @@ -272,13 +278,14 @@ uint32_t MS5611::readADC() command(MS5611_CMD_READ_ADC); if (_result == 0) { - int nr = _wire->requestFrom(_address, (uint8_t)3); - if (nr >= 3) + uint8_t length = 3; + int bytes = _wire->requestFrom(_address, length); + if (bytes >= length) { - uint32_t val = _wire->read() * 65536UL; - val += _wire->read() * 256UL; - val += _wire->read(); - return val; + uint32_t value = _wire->read() * 65536UL; + value += _wire->read() * 256UL; + value += _wire->read(); + return value; } return 0UL; } diff --git a/MS5611.h b/MS5611.h index 342036e..7964edf 100644 --- a/MS5611.h +++ b/MS5611.h @@ -21,7 +21,7 @@ // SDI SDA | o | // CSO | o | // SDO | o L | L = led -// PS | o O | O = opening PS +// PS | o O | O = opening PS = protocol select // +--------+ // // PS to VCC ==> I2C @@ -40,11 +40,11 @@ enum osr_t { - OSR_ULTRA_HIGH = 12, // 10 millis - OSR_HIGH = 11, // 5 millis - OSR_STANDARD = 10, // 3 millis - OSR_LOW = 9, // 2 millis - OSR_ULTRA_LOW = 8 // 1 millis Default = backwards compatible + OSR_ULTRA_HIGH = 12, // 10 millis + OSR_HIGH = 11, // 5 millis + OSR_STANDARD = 10, // 3 millis + OSR_LOW = 9, // 2 millis + OSR_ULTRA_LOW = 8 // 1 millis Default = backwards compatible }; @@ -65,7 +65,8 @@ class MS5611 // the actual reading of the sensor; // returns MS5611_READ_OK upon success int read(uint8_t bits); - inline int read() { return read( (uint8_t) _samplingRate); }; // uses the preset oversampling + // wrapper, uses the preset oversampling rate. + inline int read() { return read( (uint8_t) _samplingRate); }; // sets oversampling to a value between 8 and 12 void setOversampling(osr_t samplingRate); diff --git a/README.md b/README.md index 6490d38..08accdb 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,16 @@ Arduino library for MS5611 temperature and pressure sensor. ## Description -The MS5611 is a high resolution temperature and pressure sensor. -The high resolution is made possible by oversampling (many times). +The MS5611 is a high resolution temperature and pressure sensor a.k.a GY-63. +The high resolution is made possible by oversampling many times. -The device address is 0x76 or 0x77 depending on the CSB pin. +The device address is 0x76 or 0x77 depending on the CSB/CSO pin. This library only implements the I2C interface. + +#### breakout + ```cpp // // BREAKOUT MS5611 aka GY63 - see datasheet @@ -32,7 +35,7 @@ This library only implements the I2C interface. // SDI SDA | o | // CSO | o | // SDO | o L | L = led -// PS | o O | O = opening PS +// PS | o O | O = opening PS = protocol select // +--------+ // // PS to VCC ==> I2C @@ -42,7 +45,14 @@ This library only implements the I2C interface. // ``` -## important Changes +#### related libraries + +For pressure conversions see - https://github.com/RobTillaart/pressure + +For temperature conversions see - https://github.com/RobTillaart/Temperature + + +## Release Notes #### 0.3.0 breaking change @@ -53,45 +63,40 @@ This library only implements the I2C interface. #### 0.3.5 NANO 33 BLE -After lots of hours of testing it appeared that the I2C/Wire library of the NANO 33 BLE +The I2C/Wire library of the NANO 33 BLE does not see the device on the I2C bus. +After hours of testing it looks like that the I2C/Wire library of the NANO 33 BLE does not handle **isConnected()** like other platforms do. -It looks like an uninitialized length of the I2C buffer, causing failure when calling **begin()**. -Adding a **wire->write(0x00)** seems to fix the issue. +Adding a **wire->write(0x00)** in **isConnected()** fixes the problem, +however more investigation is needed to understand the root cause. +In 0.3.6 this **write(0)** is made conditional explicit for the NANO 33 BLE. ## Interface +#### Base + - **MS5611(uint8_t deviceAddress)** constructor. - **bool begin(uint8_t sda, uint8_t scl, TwoWire \*wire = &Wire)** for ESP and alike, optionally set Wire interface. initializes internals, - **bool begin(TwoWire \*wire = &Wire)** for UNO and alike, optionally set Wire interface. Initializes internals. - **bool isConnected()** checks availability of device address on the I2C bus. +(see note above NANO 33 BLE). - **reset()** resets the chip and loads constants from its ROM. -- **int read(uint8_t bits)** the actual reading of the sensor. Returns MS5611_READ_OK upon success. -- **int read()** the actual reading of the sensor, uses the preset oversampling (see below). Returns MS5611_READ_OK upon success. -- **void setOversampling(osr_t samplingRate)** sets the amount of oversampling. -See table below and test example how to use. -- **osr_t getOversampling()** returns amount of oversampling. -- **float getTemperature()** returns temperature in °C. Subsequent calls will return same value until a new **read()** is called. -- **float getPressure()** pressure is in mBar. Subsequent calls will return same value until a new **read()** is called. -- **int getLastResult()** checks last I2C communication (replace with more informative error handling?) -- **uint32_t lastRead()** last time when **read()** was called in milliseconds since startup. - +- **int read(uint8_t bits)** the actual reading of the sensor. +Number of bits determines the oversampling factor. Returns MS5611_READ_OK upon success. +- **int read()** wraps the **read()** above, uses the preset oversampling (see below). +Returns MS5611_READ_OK upon success. +- **float getTemperature()** returns temperature in °C. +Subsequent calls will return the same value until a new **read()** is called. +- **float getPressure()** pressure is in mBar. +Subsequent calls will return the same value until a new **read()** is called. -### Offset -The offset functions are added to calibrate the sensor against e.g. local weather station. This calibration can only be done runtime. - -- **void setPressureOffset(float offset = 0)** Set an offset to calibrate the pressure. -Can also be used to get the pressure relative to e.g. 1 Atm. (set offset to -1013 HPa). -Default the value is set to 0. -- **float getPressureOffset()** returns the current pressure offset. -- **void setTemperatureOffset(float offset = 0)** Set an offset to calibrate the temperature. -Can also be used to get the temperature in degrees Kelvin. (set offset to +273.15) -Default the value is set to 0. -- **float getTemperatureOffset()** returns the current temperature offset. +#### Oversampling +- **void setOversampling(osr_t samplingRate)** sets the amount of oversampling. +See table below and test example how to use. +- **osr_t getOversampling()** returns amount of oversampling. -### Oversampling table (numbers from datasheet, actual time differs - todo) @@ -104,9 +109,26 @@ Default the value is set to 0. | OSR_ULTRA_LOW | 8 | 256 | 0.065 | 0.5 | Default = backwards compatible -## Disclaimer +#### Offset -The library is still experimental. So all feedback is welcome. +The offset functions are added (0.3.6) to calibrate the sensor against e.g. a local weather station. +This calibration can only be done runtime. + +- **void setPressureOffset(float offset = 0)** Set an offset to calibrate the pressure. +Can be used to get the pressure relative to e.g. 1 Atm. +Set the offset to -1013 HPa/mBar and you get a sort of relative pressure. +Default the offset is set to 0. +- **float getPressureOffset()** returns the current pressure offset. +- **void setTemperatureOffset(float offset = 0)** Set an offset to calibrate the temperature. +Can be used to get the temperature in degrees Kelvin, just set the offset to +273.15. +Default the offset is set to 0. +- **float getTemperatureOffset()** returns the current temperature offset. + + +#### Misc + +- **int getLastResult()** checks last I2C communication. Replace with more informative error handling? +- **uint32_t lastRead()** last time when **read()** was called in milliseconds since startup. ## Operation @@ -117,11 +139,10 @@ See examples ## Future - update documentation + - separate release notes? - create a SPI based library (same base class if possible?) - - first get it working 100% + - first get this lib working 100% - proper error handling - redo lower level functions? - handle the read + math of temperature first? -- add get- and setPressureOffset() -- add get- and setTemperatureOffset() - +- diff --git a/library.json b/library.json index 8bd23a6..8f42149 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "MS5611", - "keywords": "MS5611,temperature,pressure", + "keywords": "MS5611,GY-63,GY63,temperature,pressure", "description": "Arduino library for MS5611 temperature and pressure sensor", "authors": [ diff --git a/library.properties b/library.properties index caab8e3..723be85 100644 --- a/library.properties +++ b/library.properties @@ -3,7 +3,7 @@ version=0.3.6 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for MS5611 temperature and pressure sensor -paragraph=Experimental +paragraph=Experimental, GY-63, GY63. category=Sensors url=https://github.com/RobTillaart/MS5611 architectures=* From e3d1b442592213687da8a41fbf6c6ccabdfd93cc Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Sat, 15 Jan 2022 16:08:15 +0100 Subject: [PATCH 4/6] update convert timing - use MAX - issue 23 --- MS5611.cpp | 5 +++-- README.md | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/MS5611.cpp b/MS5611.cpp index 6a232a8..c1bb7bc 100644 --- a/MS5611.cpp +++ b/MS5611.cpp @@ -8,6 +8,7 @@ // // HISTORY: // 0.3.6 2022-01-15 add setOffset functions; minor refactor; +// adjust convert timing to max - see issue #23 // 0.3.5 2022-01-13 fix isConnected() for NANO 33 BLE // 0.3.4 2021-12-29 fix #16 compilation for MBED // 0.3.3 2021-12-25 Update oversampling timings to reduce time spent waiting @@ -228,8 +229,8 @@ float MS5611::getPressure() const // void MS5611::convert(const uint8_t addr, uint8_t bits) { - // values from page 2 datasheet - uint16_t del[5] = {500, 1100, 2100, 4100, 8220}; + // values from page 3 datasheet - MAX column (rounded up) + uint16_t del[5] = {600, 1200, 2300, 4600, 9100}; uint8_t index = bits; if (index < 8) index = 8; diff --git a/README.md b/README.md index 08accdb..215adb9 100644 --- a/README.md +++ b/README.md @@ -98,15 +98,17 @@ See table below and test example how to use. - **osr_t getOversampling()** returns amount of oversampling. -(numbers from datasheet, actual time differs - todo) - -| definition | value | oversampling ratio | resolution (mbar) | time (ms) | notes | -|:--------------:|:-----:|:------------------:|:----------------:|:---------:|:------:| -| OSR_ULTRA_HIGH | 12 | 4096 | 0.012 | 8.22 | -| OSR_HIGH | 11 | 2048 | 0.018 | 4.1 | -| OSR_STANDARD | 10 | 1024 | 0.027 | 2.1 | -| OSR_LOW | 9 | 512 | 0.042 | 1.1 | -| OSR_ULTRA_LOW | 8 | 256 | 0.065 | 0.5 | Default = backwards compatible +Some numbers from datasheet, page 3 MAX column rounded up. (see #23) +(actual read time differs - see performance sketch) + +| definition | value | oversampling ratio | resolution (mbar) | time (us) | notes | +|:--------------:|:-----:|:------------------:|:-----------------:|:---------:|:------:| +| OSR_ULTRA_HIGH | 12 | 4096 | 0.012 | 9100 | +| OSR_HIGH | 11 | 2048 | 0.018 | 4600 | +| OSR_STANDARD | 10 | 1024 | 0.027 | 2300 | +| OSR_LOW | 9 | 512 | 0.042 | 1200 | +| OSR_ULTRA_LOW | 8 | 256 | 0.065 | 600 | Default = backwards compatible + #### Offset From a083e9236e14e92ac44fbde50347b0d63984402d Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Sat, 15 Jan 2022 16:34:09 +0100 Subject: [PATCH 5/6] update convert timing - use MAX - issue 23 --- MS5611.cpp | 8 +- MS5611.h | 12 ++- README.md | 8 +- examples/MS5611_test_two/MS5611_test_two.ino | 106 +++++++++++++++++++ 4 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 examples/MS5611_test_two/MS5611_test_two.ino diff --git a/MS5611.cpp b/MS5611.cpp index c1bb7bc..f66543c 100644 --- a/MS5611.cpp +++ b/MS5611.cpp @@ -209,17 +209,17 @@ void MS5611::setOversampling(osr_t samplingRate) } -float MS5611::getTemperature() const +float MS5611::getTemperature() const { - if (_temperatureOffset == 0) return _temperature * 0.01; - return _temperature * 0.01 + _temperatureOffset; + if (_temperatureOffset == 0) return _temperature * 0.01; + return _temperature * 0.01 + _temperatureOffset; }; float MS5611::getPressure() const { if (_pressureOffset == 0) return _pressure * 0.01; - return _pressure * 0.01 + _pressureOffset; + return _pressure * 0.01 + _pressureOffset; }; diff --git a/MS5611.h b/MS5611.h index 7964edf..abed9b1 100644 --- a/MS5611.h +++ b/MS5611.h @@ -81,8 +81,8 @@ class MS5611 float getPressure() const; // OFFSET - 0.3.6 - void setPressureOffset(float offset = 0) { _pressureOffset = offset; }; - float getPressureOffset() { return _pressureOffset; }; + void setPressureOffset(float offset = 0) { _pressureOffset = offset; }; + float getPressureOffset() { return _pressureOffset; }; void setTemperatureOffset(float offset = 0) { _temperatureOffset = offset; }; float getTemperatureOffset() { return _temperatureOffset; }; @@ -90,7 +90,13 @@ class MS5611 int getLastResult() const { return _result; }; // last time in millis() when the sensor has been read. - uint32_t lastRead() { return _lastRead; }; + uint32_t lastRead() const { return _lastRead; }; + + // develop functions. + /* + void setAddress(uint8_t address) { _address = address; }; // RANGE CHECK !!! + uint8_t getAddress() const { return _address; }; + */ private: diff --git a/README.md b/README.md index 215adb9..c6a59e8 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,13 @@ After hours of testing it looks like that the I2C/Wire library of the NANO 33 BL does not handle **isConnected()** like other platforms do. Adding a **wire->write(0x00)** in **isConnected()** fixes the problem, however more investigation is needed to understand the root cause. -In 0.3.6 this **write(0)** is made conditional explicit for the NANO 33 BLE. + + +#### 0.3.6 + +The **write(0)** in **isConnected()** is made conditional explicit for the NANO 33 BLE. + +The timing for convert is adjusted from TYPICAL to MAX - datasheet page 3. ## Interface diff --git a/examples/MS5611_test_two/MS5611_test_two.ino b/examples/MS5611_test_two/MS5611_test_two.ino new file mode 100644 index 0000000..9eac6fc --- /dev/null +++ b/examples/MS5611_test_two/MS5611_test_two.ino @@ -0,0 +1,106 @@ +// +// FILE: MS5611_test_two.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo application for two sensors +// DATE: 2022-01-15 +// URL: https://github.com/RobTillaart/MS5611 + + +#include "MS5611.h" + +// BREAKOUT MS5611 aka GY63 - see datasheet +// +// SPI I2C +// +--------+ +// VCC VCC | o | +// GND GND | o | +// SCL | o | +// SDI SDA | o | +// CSO | o | +// SDO | o L | L = led +// PS | o O | O = opening PS +// +--------+ +// +// PS to VCC ==> I2C +// PS to GND ==> SPI +// CS to VCC ==> 0x76 +// CS to GND ==> 0x77 + + +MS5611 ONE(0x76); // 0x76 = CSB to VCC +MS5611 TWO(0x77); // 0x77 = CSB to GND + + +uint32_t start, stop; + + +void setup() +{ + Serial.begin(115200); + while (!Serial); + + Serial.println(); + Serial.println(__FILE__); + Serial.print("MS5611_LIB_VERSION: "); + Serial.println(MS5611_LIB_VERSION); + + if (ONE.begin() == true) + { + Serial.println("MS5611 0x76 found."); + } + else + { + Serial.println("MS5611 0x76 not found."); + } + + if (TWO.begin() == true) + { + Serial.println("MS5611 0x77 found."); + } + else + { + Serial.println("MS5611 0x77 not found."); + } + +} + + +void loop() +{ + digitalWrite(LED_BUILTIN, HIGH); + int result = ONE.read(); + stop = micros(); + if (result != MS5611_READ_OK) + { + Serial.print("0x76 Error in read: "); + Serial.println(result); + } + + result = TWO.read(); + stop = micros(); + if (result != MS5611_READ_OK) + { + Serial.print("0x77 Error in read: "); + Serial.println(result); + } + digitalWrite(LED_BUILTIN, LOW); + + Serial.print("T: "); + Serial.print(ONE.getTemperature(), 2); + Serial.print("\tP: "); + Serial.print(ONE.getPressure(), 2); + Serial.print("\tT: "); + Serial.print(TWO.getTemperature(), 2); + Serial.print("\tP: "); + Serial.print(TWO.getPressure(), 2); + Serial.print("\tT: "); + Serial.print(ONE.getTemperature() - TWO.getTemperature(), 2); + Serial.print("\tP: "); + Serial.print(ONE.getPressure() - TWO.getPressure(), 2); + Serial.println(); + + delay(1000); +} + + +// -- END OF FILE -- From ea068c7d56354cba0de9f9d0442f8b646a0cec07 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Sat, 15 Jan 2022 20:31:53 +0100 Subject: [PATCH 6/6] fixes --- examples/MS5611_test/MS5611_test.ino | 2 + .../MS5611_test_offset/MS5611_test_offset.ino | 2 + .../MS5611_test_plotter.ino | 72 +++++++++++++++++++ examples/MS5611_test_two/MS5611_test_two.ino | 2 + 4 files changed, 78 insertions(+) create mode 100644 examples/MS5611_test_plotter/MS5611_test_plotter.ino diff --git a/examples/MS5611_test/MS5611_test.ino b/examples/MS5611_test/MS5611_test.ino index c877a13..4697662 100644 --- a/examples/MS5611_test/MS5611_test.ino +++ b/examples/MS5611_test/MS5611_test.ino @@ -37,6 +37,8 @@ void setup() Serial.begin(115200); while (!Serial); + pinMode(LED_BUILTIN, OUTPUT); + Serial.println(); Serial.println(__FILE__); Serial.print("MS5611_LIB_VERSION: "); diff --git a/examples/MS5611_test_offset/MS5611_test_offset.ino b/examples/MS5611_test_offset/MS5611_test_offset.ino index e880853..70da060 100644 --- a/examples/MS5611_test_offset/MS5611_test_offset.ino +++ b/examples/MS5611_test_offset/MS5611_test_offset.ino @@ -37,6 +37,8 @@ void setup() Serial.begin(115200); while (!Serial); + pinMode(LED_BUILTIN, OUTPUT); + Serial.println(); Serial.println(__FILE__); Serial.print("MS5611_LIB_VERSION: "); diff --git a/examples/MS5611_test_plotter/MS5611_test_plotter.ino b/examples/MS5611_test_plotter/MS5611_test_plotter.ino new file mode 100644 index 0000000..3742acb --- /dev/null +++ b/examples/MS5611_test_plotter/MS5611_test_plotter.ino @@ -0,0 +1,72 @@ +// +// FILE: MS5611_test_plotter.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo application +// DATE: 2022-01-15 +// URL: https://github.com/RobTillaart/MS5611 + + +#include "MS5611.h" + +// BREAKOUT MS5611 aka GY63 - see datasheet +// +// SPI I2C +// +--------+ +// VCC VCC | o | +// GND GND | o | +// SCL | o | +// SDI SDA | o | +// CSO | o | +// SDO | o L | L = led +// PS | o O | O = opening PS +// +--------+ +// +// PS to VCC ==> I2C +// PS to GND ==> SPI +// CS to VCC ==> 0x76 +// CS to GND ==> 0x77 + +MS5611 MS5611(0x76); // 0x76 = CSB to VCC; 0x77 = CSB to GND + +void setup() +{ + Serial.begin(115200); + while (!Serial); + + pinMode(LED_BUILTIN, OUTPUT); + + Serial.println(); + if (MS5611.begin() == true) + { + Serial.println("MS5611 found."); + } + else + { + Serial.println("MS5611 not found. halt."); + while (1) + { + digitalWrite(LED_BUILTIN, HIGH); + delay(1000); + digitalWrite(LED_BUILTIN, LOW); + delay(1000); + } + } + Serial.println("TEMP\tPRESSURE"); + // scale T & P to same range :) + MS5611.setPressureOffset(-1000); +} + +void loop() +{ + digitalWrite(LED_BUILTIN, HIGH); + MS5611.read(); + Serial.print(MS5611.getTemperature(), 2); + Serial.print("\t"); + Serial.print(MS5611.getPressure(), 2); + Serial.println(); + digitalWrite(LED_BUILTIN, LOW); + delay(1000); +} + + +// -- END OF FILE -- diff --git a/examples/MS5611_test_two/MS5611_test_two.ino b/examples/MS5611_test_two/MS5611_test_two.ino index 9eac6fc..abd4a64 100644 --- a/examples/MS5611_test_two/MS5611_test_two.ino +++ b/examples/MS5611_test_two/MS5611_test_two.ino @@ -39,6 +39,8 @@ void setup() Serial.begin(115200); while (!Serial); + pinMode(LED_BUILTIN, OUTPUT); + Serial.println(); Serial.println(__FILE__); Serial.print("MS5611_LIB_VERSION: ");