Skip to content

Commit

Permalink
fix isConnected() for NANO 33 BLE (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart authored Jan 14, 2022
1 parent b28135b commit 62b8543
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 36 deletions.
8 changes: 5 additions & 3 deletions MS5611.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// FILE: MS5611.cpp
// AUTHOR: Rob Tillaart
// Erni - testing/fixes
// VERSION: 0.3.4
// VERSION: 0.3.5
// PURPOSE: MS5611 Temperature & Humidity library for Arduino
// URL: https://github.com/RobTillaart/MS5611
//
// HISTORY:
// 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
// 0.3.2 2021-12-24 add get/set oversampling, read() (thanks to LyricPants66133)
Expand All @@ -18,7 +19,7 @@
//
// 0.2.2 2021-01-01 add Arduino-CI + unit tests + isConnected()
// 0.2.1 2020-06-28 fix #1 min macro compile error
// 0.2.0 2020-06-21 refactor; #pragma once;
// 0.2.0 2020-06-21 refactor; #pragma once;
//
// 0.1.8 fix #109 incorrect constants (thanks to flauth)
// 0.1.7 revert double to float (issue 33)
Expand Down Expand Up @@ -104,6 +105,7 @@ bool MS5611::begin(TwoWire * wire)
bool MS5611::isConnected()
{
_wire->beginTransmission(_address);
_wire->write(0); // needed for NANO 33 BLE
return (_wire->endTransmission() == 0);
}

Expand Down Expand Up @@ -147,7 +149,7 @@ int MS5611::read(uint8_t bits)
if (_result) return _result;
uint32_t _D2 = readADC();
if (_result) return _result;

// TEST VALUES - comment lines above
// uint32_t D1 = 9085466;
// uint32_t D2 = 8569150;
Expand Down
21 changes: 19 additions & 2 deletions MS5611.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,33 @@
// FILE: MS5611.h
// AUTHOR: Rob Tillaart
// Erni - testing/fixes
// VERSION: 0.3.4
// VERSION: 0.3.5
// PURPOSE: Arduino library for MS5611 temperature and pressure sensor
// URL: https://github.com/RobTillaart/MS5611


#include "Arduino.h"
#include "Wire.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

#define MS5611_LIB_VERSION (F("0.3.4"))
#define MS5611_LIB_VERSION (F("0.3.5"))


#define MS5611_READ_OK 0
Expand Down
53 changes: 44 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,47 @@ The high resolution is made possible by oversampling (many times).

The device address is 0x76 or 0x77 depending on the CSB pin.


#### 0.3.0 breaking changes
This library only implements the I2C interface.

```cpp
//
// 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
//
```

## important Changes

#### 0.3.0 breaking change

1. fixed math error so previous versions are **obsolete**.
2. temperature is a float expressed in degrees Celsius.
3. pressure is a float expressed in mBar.


#### 0.3.5 NANO 33 BLE

After lots of hours of testing it appeared 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.


## Interface

- **MS5611(uint8_t deviceAddress)** constructor.
Expand All @@ -34,18 +67,20 @@ The device address is 0x76 or 0x77 depending on the CSB pin.
- **bool isConnected()** checks availability of device address on the I2C bus.
- **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. 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 millis() since startup.
- **uint32_t lastRead()** last time when **read()** was called in milliseconds since startup.


#### Oversampling table

(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 |
Expand All @@ -57,8 +92,7 @@ See table below and test example how to use.

## Disclaimer

The library is experimental. As I have no such sensor the quality is hard to test.
So if you happen to have such a sensor, please give it a try and let me know.
The library is still experimental. So all feedback is welcome.


## Operation
Expand All @@ -68,11 +102,12 @@ See examples

## Future

- get such a sensor to test
- update documentation
- create a SPI based library (same base class if possible?)
- first get it working 100%
- proper error handling
- redo lower level functions?
- handle the read + math of temperature first? (need hardware to test)
-
- handle the read + math of temperature first?
- add get- and setPressureOffset()
- add get- and setTemperatureOffset()

44 changes: 44 additions & 0 deletions examples/MS5611_detector/MS5611_detector.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// FILE: MS5611_DETECTOR.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: detect an MS5611 on a NANO33 BLE
// DATE: 2022-01-13
// URL: https://github.com/RobTillaart/MS5611


#include "Arduino.h"
#include "Wire.h"


void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);

Wire.begin();
Wire.beginTransmission(0x76);
Wire.write(0);
int x = Wire.endTransmission();
Wire.beginTransmission(0x77);
Wire.write(0);
int y = Wire.endTransmission();

Serial.println(x);
Serial.println(y);
delay(1000);

if (x == 0) Serial.println("MS5611 found at 0x76");
else if (y == 0) Serial.println("MS5611 found at 0x77");
else Serial.println("no MS5611 found");

Serial.println("done...");
}


void loop()
{
}


// -- END OF FILE --
11 changes: 8 additions & 3 deletions examples/MS5611_minimal/MS5611_minimal.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ MS5611 MS5611(0x77); // 0x76 = CSB to VCC; 0x77 = CSB to GND
void setup()
{
Serial.begin(115200);
Serial.print(__FILE__);
while(!Serial);

Serial.println();
Serial.println(__FILE__);
Serial.print("MS5611_LIB_VERSION: ");
Serial.println(MS5611_LIB_VERSION);

Expand All @@ -25,18 +28,20 @@ void setup()
else
{
Serial.println("MS5611 not found. halt.");
while(1);
while (1);
}
Serial.println();
}


void loop()
{
MS5611.read(); // note no error checking => "optimistic".
Serial.print("\nT:\t");
Serial.print("T:\t");
Serial.print(MS5611.getTemperature(), 2);
Serial.print("\tP:\t");
Serial.print(MS5611.getPressure(), 2);
Serial.println();
delay(1000);
}

Expand Down
14 changes: 9 additions & 5 deletions examples/MS5611_performance/MS5611_performance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "MS5611.h"


MS5611 MS5611(0x77); // 0x76 = CSB to VCC; 0x77 = CSB to GND
MS5611 MS5611(0x76); // 0x76 = CSB to VCC; 0x77 = CSB to GND


uint32_t start, stop, count;
Expand All @@ -18,19 +18,23 @@ uint32_t start, stop, count;
void setup()
{
Serial.begin(115200);
Serial.print(__FILE__);
Serial.print("MS5611 lib version: ");
while(!Serial);

Serial.println();
Serial.println(__FILE__);
Serial.print("MS5611_LIB_VERSION: ");
Serial.println(MS5611_LIB_VERSION);

if (MS5611.begin())
if (MS5611.begin() == true)
{
Serial.println("MS5611 found.");
}
else
{
Serial.println("MS5611 not found. halt.");
while(1);
while (1);
}
Serial.println();

count = 0;
}
Expand Down
12 changes: 8 additions & 4 deletions examples/MS5611_performance_all/MS5611_performance_all.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ uint32_t start, stop;
void setup()
{
Serial.begin(115200);
Serial.print(__FILE__);
Serial.print("MS5611 lib version: ");
while(!Serial);

Serial.println();
Serial.println(__FILE__);
Serial.print("MS5611_LIB_VERSION: ");
Serial.println(MS5611_LIB_VERSION);

if (MS5611.begin())
if (MS5611.begin() == true)
{
Serial.println("MS5611 found.");
}
else
{
Serial.println("MS5611 not found. halt.");
while(1);
while (1);
}
Serial.println();

Serial.println("OSR \t TIME");

Expand Down
Loading

0 comments on commit 62b8543

Please sign in to comment.