0

MPRLS0300YG00001B Pressure Sensor Code
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MPRLS0300YG00001B Pressure Sensor Code

by wcj1n18 on Sun Sep 08, 2019 12:36 pm

Hello,

I have purchased the Adafruit MPRLS0025PA00001A pressure sensor breakout board and have been able to get good results with the code from here:
https://github.com/adafruit/Adafruit_MPRLS

The final "A" in MPRLS0025PA00001A means a transfer function of 10% to 90% of 2^24 counts.

However, I would also like to use the same code but apply it to the Honeywell pressure sensor/ breakout board MPRLS0300YG00001B.

The final "B" in MPRLS0300YG00001B means a transfer function of 2.5% to 22.5% of 2^24 counts.

I thought I had successfully modified the code in the link above to suit this pressure sensor, however it doesn't seem quite right as (although it is able to be verified and uploaded in the Arduino), the serial monitor gives very low values of about 5.00 hPa . I've attached the .ino code (which is just the same as the "simple test" in the link above apart from changing the name of of the .h file (#include <0300mmHg.h>).

I've then attached the .h code (which is the same apart from changing the pressure range- converting 300mmHg to PSI).

Finally, I've attached the .cpp code, which I've changed to account for the different transfer function (2.5% to 22.5%, rather than 20% to 90%) and the (#include <0300mmHg.h>).

Code: Select all | TOGGLE FULL SIZE
#include <0300mmHg.h>
#include <Wire.h>

// You dont *need* a reset and EOC pin for most uses, so we set to -1 and don't connect
#define RESET_PIN  -1  // set to any GPIO pin # to hard-reset on begin()
#define EOC_PIN    -1  // set to any GPIO pin to read end-of-conversion by pin
Adafruit_MPRLS mpr = Adafruit_MPRLS(RESET_PIN, EOC_PIN);

void setup() {
  // put your setup code here, to run once:
Serial.begin(2000000);
  Serial.println("MPRLS Simple Test");
  if (! mpr.begin()) {
    Serial.println("Failed to communicate with MPRLS sensor, check wiring?");
    while (1) {
      delay(10);
    }
  }
  Serial.println("Found MPRLS sensor");
}

void loop() {
  // put your main code here, to run repeatedly:
 float pressure_hPa = mpr.readPressure();
 
  Serial.print("Pressure (hPa): "); Serial.println(pressure_hPa);
  /* Serial.print("Pressure (mm_Hg): "); Serial.println(pressure_hPa / 1.33322387415); */
  delay(100);
}






Code: Select all | TOGGLE FULL SIZE
#if (ARDUINO >= 100)
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif
#include "Wire.h"

#define MPRLS_DEFAULT_ADDR         (0x18) ///< Most common I2C address
#define MPRLS_STATUS_POWERED       (0x40) ///< Status SPI powered bit
#define MPRLS_STATUS_BUSY          (0x20) ///< Status busy bit
#define MPRLS_STATUS_FAILED        (0x04) ///< Status bit for integrity fail
#define MPRLS_STATUS_MATHSAT       (0x01) ///< Status bit for math saturation


/**************************************************************************/
/*!
    @brief  Class that stores state and functions for interacting with MPRLS sensor IC
*/
/**************************************************************************/
class Adafruit_MPRLS {
 public:
  Adafruit_MPRLS(int8_t reset_pin = -1, int8_t EOC_pin = -1,
       uint8_t PSI_min = 0, uint8_t PSI_max = 5.801032491);

  boolean begin(uint8_t i2c_addr = MPRLS_DEFAULT_ADDR,
        TwoWire *twoWire = &Wire);

  uint8_t readStatus(void);
  float   readPressure(void);

 private:
  uint32_t readData(void);

  uint8_t _i2c_addr;
  int8_t _reset, _eoc;
  uint8_t _PSI_min, _PSI_max;

  TwoWire *_i2c;
};


Code: Select all | TOGGLE FULL SIZE
#if (ARDUINO >= 100)
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif
#include "Wire.h"

#include "0300mmHg.h"

/**************************************************************************/
/*!
    @brief constructor initializes default configuration value
    @param reset_pin Optional hardware reset pin, default set to -1 to skip
    @param EOC_pin Optional End-of-Convert indication pin, default set to -1 to skip
    @param PSI_min The minimum PSI measurement range of the sensor, default 0
    @param PSI_max The maximum PSI measurement range of the sensor, default 25
*/
/**************************************************************************/
Adafruit_MPRLS::Adafruit_MPRLS(int8_t reset_pin, int8_t EOC_pin,
                uint8_t PSI_min, uint8_t PSI_max) {

    _reset = reset_pin;
    _eoc = EOC_pin;
    _PSI_min = PSI_min;
    _PSI_max = PSI_max;
}

/**************************************************************************/
/*!
    @brief  setup and initialize communication with the hardware
    @param i2c_addr The I2C address for the sensor (default is 0x18)
    @param twoWire Optional pointer to the desired TwoWire I2C object. Defaults to &Wire
    @returns True on success, False if sensor not found
*/
/**************************************************************************/
boolean Adafruit_MPRLS::begin(uint8_t i2c_addr, TwoWire *twoWire) {
  _i2c_addr = i2c_addr;
  _i2c = twoWire;

  _i2c->begin();

  if (_reset != -1) {
    pinMode(_reset, OUTPUT);
    digitalWrite(_reset, HIGH);
    digitalWrite(_reset, LOW);
    delay(10);
    digitalWrite(_reset, HIGH);
  }
  if (_eoc != -1) {
    pinMode(_eoc, INPUT);
  }

  delay(10); // startup timing

  //Serial.print("Status: ");
  uint8_t stat = readStatus();
  //Serial.println(stat);
  return !(stat & 0b10011110);
}

/**************************************************************************/
/*!
    @brief Read and calculate the pressure
    @returns The measured pressure, in hPa on success, NAN on failure
*/
/**************************************************************************/
float Adafruit_MPRLS::readPressure(void) {
  uint32_t raw_psi = readData();
  if (raw_psi == 0xFFFFFFFF) {
    return NAN;
  }

  // All is good, calculate the PSI and convert to hPA
  // use the 2.5-22.5 calibration curve
  float psi = (raw_psi - 419430.4) * (_PSI_max - _PSI_min);
  psi /= (float) (3774873.6 - 419430.4);
  psi += _PSI_min;
    // convert PSI to hPA
  return psi * 68.947572932;
}


/**************************************************************************/
/*!
    @brief Read 24 bits of measurement data from the device
    @returns -1 on failure (check status) or 24 bits of raw ADC reading
*/
/**************************************************************************/
uint32_t Adafruit_MPRLS::readData(void) {
  _i2c->beginTransmission(_i2c_addr);
  _i2c->write(0xAA);   // command to read pressure
  _i2c->write(0x0);
  _i2c->write(0x0);
  _i2c->endTransmission();
 
  // Use the gpio to tell end of conversion
  if (_eoc != -1) {
    while (!digitalRead(_eoc)) {
      delay(10);
    }
  } else {
    // check the status byte
    uint8_t stat;
    while ((stat = readStatus()) & MPRLS_STATUS_BUSY) {
      //Serial.print("Status: "); Serial.println(stat, HEX);
      delay(10);
    }
  }
  _i2c->requestFrom(_i2c_addr, (uint8_t)4);
 
  uint8_t status = _i2c->read();
  if (status & MPRLS_STATUS_MATHSAT) {
    return 0xFFFFFFFF;
  }
  if (status & MPRLS_STATUS_FAILED) {
    return 0xFFFFFFFF;
  }

  uint32_t ret;
  ret = _i2c->read(); ret <<= 8;
  ret |= _i2c->read(); ret <<= 8;
  ret |= _i2c->read();
 
  return ret;
}

/**************************************************************************/
/*!
    @brief Read just the status byte, see datasheet for bit definitions
    @returns 8 bits of status data
*/
/**************************************************************************/
uint8_t Adafruit_MPRLS::readStatus(void) {
  _i2c->requestFrom(_i2c_addr, (uint8_t)1);

  return _i2c->read();
}


I'd like to add that my Arduino experience is limited, so there may be a lot more code that I need to change. If there is a lot more that needs to be changed, i'm not expecting you to do that, i'm just hoping that there is something minor I'm missing out on. The fact that i'm at least getting some results (that are clearly wrong), may suggest that i'm not too far off? :)
Thank you

wcj1n18
 
Posts: 1
Joined: Sun Sep 08, 2019 11:34 am

Please be positive and constructive with your questions and comments.