• Hi,

    can someone help me to get the sensor data of the MPRL sensor?

    I wrote some code by using the documentation of the sensor manufacturer:

    Find datasheet here:

    var i2c = new I2C();
    i2c.setup({ scl : D29, sda: D28 });     
    i2c.writeTo(0xAA, 0x00, 0x00); 
    setInterval(function () {
    var x = i2c.readFrom(0x18, 5);  
    }, 200);

    What I get in the console :
    new Uint8Array([64, 139, 169, 98, 128])

    When I reupload the code and give a puff signal to the connected tube on the sensor the value 139 increase. When I do a sip in goes down. The other values 3 & 4 change randomly. the first and the last value is steady.

    The value 139 changes just when I reupload to the MDBT42Q module. What I need to do that the value is changing in realtime? Do I need to convert the value as well?


  • Hi @psc1988,

    There is a sample code for Arduino given by Adafruit: https://github.com/adafruit/Adafruit_MPR­LS/blob/master/Adafruit_MPRLS.cpp

    Basically the first byte you will receive is the status byte, then the 3 next bytes are meant to be concatenated to form a 24-bit value (see step 3 of table 17, page 16 of the corresponding datasheet). The sample code for this first step is shown here in the code

    Once you get your 24-bit value, you need to convert it to a meaningful value, as shown on page 19 of the datasheet or here in the code

    I don't think you need to read 5 bytes out of the sensor (var x = i2c.readFrom(0x18, 5);), IMO you can just try reading 4 bytes, and use the 3 last bytes to calculate the pressure value.

  • Hi Jean-Philippe,

    thanks for the fast feedback, that helps!

    But why the data in the console just change when I upload the code again? When i make a sip or puff activity, nothing will change.

  • because you need to call i2c.writeTo within the setInterval to send the read command (see step 3 of table 17, page 16 of the datasheet)

    The data retrieval is made of 1 Write, followed by 4 Read

  • Then i get the values ([96, 0, 0, 0, 0]) in the console.

  • Sat 2021.03.27

    'But why the data in the console just change when I upload the code again?'

    It's likely the chip select line is now in a correct state before the snippet in post #1 executes, and/or sufficient settling time occurred and the sensor is now happy and ready to respond.

    'Then i get the values ([96, 0, 0, 0, 0]) in the console.'

    While I have never used this sensor, I took a quick peek at the device specification data sheet. Has any of the following been tried or considered?

    Although I'm not able to find the fastest write/read interval, hammering on the device five times a second may be a bit too fast for a settling time. Changing L4 to a setTimeout() so that better control (number of times) over what data is retrieved might improve.

    From p.15
    Has the status byte been viewed to see if the sensor is 'actually' ready to send back it's data?

    Has an attempt to place pull-ups on the I2C control signal lines been attempted?

    Has the bitrate freq value been modified? (rounded pulse edges) See table 18 p.16

    opt as data was detected post #1: Has an attempt to use SPI over I2C been tried? p.17

    Is there a solid ground between the sensor, uP and power supply?

    See note on p.14 about capacitance on SDA and SCL relating to clock speed.

    Is there the availability of a logic analyzer to speed the debug process?

    For what it's worth, after hours of struggle with SPI, I2C and UARTS, I eventually buckled and picked up a logic analyzer ~$20 that provides immediate feedback on what is on the data lines. I had a floating CS chip Select that rounded out the leading edge, so the receiving device never started the data receive, and thus nothing during the expected read was seen. Here are some sample images. but with a UART:


    Note the protocol analyzer output that allows the conversion of Hi-Lo to actual ASCII we may read

  • Hi Robin,

    also thanks for your answer.

    Now with this code works to get a realtime 24-bit value without re-upload:

        var i2c = new I2C();
        i2c.setup({ scl : D29, sda: D28 });
        setInterval(() => {
          i2c.writeTo(0x18, 0xAA, 0x00, 0x00);
          setTimeout(() => {
            let a = i2c.readFrom(MPRL, 5);
          }, 100);
        }, 300);

    Now I try to calculate the pressure like in the description of the datasheet and the arduino library.
    Its strange I just played with the new code and the times of the timeout and interval function.

  • And finally the code with the psi-output value:

    var i2c = new I2C();
    i2c.setup({ scl : D29, sda: D28 });
    var output_bit;
    var output_max;
    var output_min;
    var P_max = 1;
    var P_min = -1;
    var pressure1;
    var pressure2;
    var pressure_final;
    setInterval(() => {
    i2c.writeTo(0x18, 0xAA, 0x00, 0x00);
      setTimeout(() => {
      let output = i2c.readFrom(0x18, 5);
        var output_bit = output[1] *100 *100 + output[2]*100 + output[3];
        var pressure1 = (output_bit-1200000)*(1-(-1));
        var pressure2 = pressure1/(1500000-1200000);
        var pressure_final = pressure2 + (-1) - 0.4;   
      }, 100);
    }, 300);
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

MPRL Pressure Sensor (Adafruit Breakout Board) - Reading sensor data via I2C

Posted by Avatar for psc1988 @psc1988