Go Down

Topic: Strange readings from embedded barometer on Arduino Nano 33 BLE Sense (Read 2198 times) previous topic - next topic

ArtiSchmidt

I've been testing out the barometer (LPS22HB) on my new Arduino Nano, and I've run into two issues with it. The first is that after an inconsistent amount of time from starting to take readings (usually seems to be between 2 and 20 minutes), the reading stays at a constant value. For example:

Code: [Select]
100.542725
100.541061
100.540550
100.541649
100.541214
100.541214
100.541214
100.541214
100.541214...


And here is the program:

Code: [Select]
#include <Arduino_LPS22HB.h>

unsigned long lastRead = 0;

void setup() {
    Serial.begin(9600);
    BARO.begin();
}

void loop() {
    if (millis() - lastRead >= 10) {
        lastRead = millis();
        Serial.println(BARO.readPressure(), 6);
    }
}


I'm simply printing the sensor value every 10 ms using its official library. I can't simplify it much more than I have, so I'm pretty stuck on debugging this.

The second issue is that the first few readings from the barometer are always around 75.95, about 25 below subsequent values. I've been unable to pin down whether these occur within a certain amount of time or number of reads from startup, as it doesn't seem consistent. Is it normal to get strange sensor readings on startup?

I'm more concerned with the first issue as I can dispose initial readings to remedy the second. Any idea what might be going on?

Note: I originally asked this question on the Arduino Stack Exchange

TheMemberFormerlyKnownAsAWOL

Quote
100.541214
12 characters (including cr/lf) at 9600 bits per second takes more than 10 ms.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

ArtiSchmidt

Thanks for the reply; I'm running into the same issue at 115200 baud though. Also, shouldn't 12 characters  or 96 bits take exactly 10 ms at 9600 baud? I suppose even if that is the case there might be issues right on the edge like that.

TheMemberFormerlyKnownAsAWOL

#3
Jan 22, 2020, 09:38 pm Last Edit: Jan 22, 2020, 09:38 pm by TheMemberFormerlyKnownAsAWOL
12 characters, 120 bits.

How did you decide this is an installation issue?
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

ArtiSchmidt

Ah okay, didn't think about the necessity of other bits. I decided this was a troubleshooting issue; would it be better suited for a section like Nano 33 BLE Sense? I guess I could delete this thread and post somewhere else if you think I should.

ballscrewbob

@ArtiSchmidt

Moved your topic to it's current location as it is more suitable.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google (who would have thunk it ! ) or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Klaus_K

I did a test to replicate the issue and can confirm the behavior.

The issue is very likely not the connection to the PC but the LPS22HB. When you get to the state where you get the constant value, you can reset the Arduino by pressing the reset button on the board. After the board restarts and you reconnect the Serial Monitor you will still get the same value.

So, the sensor gets into a state where it no longer sends the correct value.

The datasheet states the device has a maximum pressure output data rate (ODRpres) of 75Hz. So, the minimum time between sensor reading should be ~14ms.

ArtiSchmidt

I think you're right. I haven't had any issues with 20 ms between reads. This behavior when the read frequency is exceeded certainly seems odd, but at least it seems likely to be the cause. Thank you!

Also, if you get a chance, could you please run this code to see what is printed? I've been getting values around 75 kPa on initial reads even after a delay (about 25 below the true value). I'm curious if this is normal behavior on startup.

Code: [Select]
#include <Arduino_LPS22HB.h>

float value;

void setup() {
    Serial.begin(115200);
    BARO.begin();
    delay(1000);
    value = BARO.readPressure();
}

void loop() {
    Serial.println(value, 6);
    delay(1000);
}

matt_lee_smith

Hello

Quote
I think you're right. I haven't had any issues with 20 ms between reads. This behavior when the read frequency is exceeded certainly seems odd, but at least it seems likely to be the cause. Thank you!
Nothing is truly instant, the sensor needs time to do what it needs, so if you're constantly barraging it with requests then strange things can happen. Furthermore, I can't imagine many situations where you need to read an atmospheric pressure sensor more than 75 times a second.

As for your code, as it currently is it would only print the first value because the sensor read is in the setup.

If you look at the example on the IDE: File->Examples->Arduino_LPS22HB->Read Pressure the read request is run every second.

I have attached the readout of my Nano 33 Sense using the IDE example code to 6 decimal places.

ArtiSchmidt

Yes, I was specifically referring to the fact that the sensor starts outputting a constant value. I'm sure this would make sense if I took the time to understand the inner workings of the the sensor, but without doing that, a few repeat values or nonsense values seem like more likely symptoms of exceeding the read frequency.

I'm going to use the pressure sensor as an altimeter which is why I need to read at a high frequency, and I plan to average multiple readings on top of that.

I'm intentionally only printing the first read in the most recent code, just to show that it is incorrect at around 75 (a separate issue). I was wondering what causes this, as it occurs even after waiting several seconds before the first read or taking and discarding a read beforehand. I see that it is the same case in your pressure data; thank you for sharing that.

matt_lee_smith

#10
Jan 28, 2020, 02:29 am Last Edit: Jan 28, 2020, 10:02 am by matt_lee_smith Reason: Found out that sensor pin isn't connected to Arduino
Right, with you now.

I just ran a test using the following code:

Code: [Select]
#include <Arduino_LPS22HB.h>
float F_array[1000];
unsigned long T_array[1000];

void setup() {
  Serial.begin(9600);
  while (!Serial);

  if (!BARO.begin()) {
    Serial.println("Failed to initialize pressure sensor!");
    while (1);
  }
  for (int i = 0; i < 1000; i++) {
    F_array[i] = BARO.readPressure();
    T_array[i] = millis();
  }
  for (int i = 0; i < 1000; i++) {
    Serial.print(T_array[i]);
    Serial.print(": ");
    Serial.println(F_array[i], 6);
  }
}

void loop() {
}


As expected, the 75 ish value lasts about 10-13ms, but I managed to read the actual value 5 times before it changed. I think this is because the actual library is using the One_Shot mode described in the datasheet which is effectively powering the sensor up and down per read.

So my hunch is that the initial issue where the data becomes stuck over time comes from this repeated One_Shot approach. For prolonged use, it would probably be sensible to customise the Arduino library or find a suitable alternative library that uses either the First In First Out (FIFO) "stream mode" that continuously sends data out without request/polling or (and perhaps better) using the builtin threshold interrupt mode that can compare the last read value and decides based on user-set thresholds if the pressure has changed enough to tell the Arduino (page 34-35 on the datasheet)

Not entirely sure how you do that though and it is probably fine using what you have now with the 20ms delay but I thought this might add a bit more detail to what could be done.

EDIT: Just checked the Nano 33 Sense schematic and the interrupt pin isn't actually connected to anything so ignore at least the interrupt idea!

Riccardo_Rizzo

Hi all, this will be fixed with the following PR: https://github.com/arduino-libraries/Arduino_LPS22HB/pull/2

The problem was don't wait for hardware cleaning of the one shot bit has reported in the issue.

the reading time is around 16 ms, in one shot mode.

it is possible change how the library is implemented and as you say the FIFO and/or the Continuous mode could reduce the readings time

Pay attention the interrupt are not available, the rails of the interrupts, as you can see on the schematics:

https://content.arduino.cc/assets/NANO33BLE_V2.0_sch.pdf

is not connected, then not available.

The sketch i used to make the test is the following:

Code: [Select]


#include <Arduino_LPS22HB.h>
long time0 = 0;
long time1 = 0;
long time2 = 0;
long time3 = 0;
long time4 = 0;
int a = 1;

void setup() {
  Serial.begin(9600);
  while (!Serial);
  time0 = millis();
  if (!BARO.begin()) {
    Serial.println("Failed to initialize pressure sensor!");
    while (1);
  }
  time1 = millis();
}

void loop() {
  // read the sensor value
  time3 = millis();
  float pressure = BARO.readPressure();
  time4 = millis();
  if (a == 1) {
    time2 = millis();
    a = 0;
  }
  Serial.print("Initializaion sernsor time: ");
  Serial.print(time1 - time0);
  Serial.println(" ms");
  Serial.print("from init to first reading: ");
  Serial.print(time2 - time1);
  Serial.println(" ms");
  Serial.print("total time: ");
  Serial.print(time2 - time0);
  Serial.println(" ms");
  Serial.print("readings time: ");
  Serial.print(time4 - time3);
  Serial.println(" ms");
  // print the sensor value
  Serial.print("Pressure = ");
  Serial.print(pressure);
  Serial.println(" kPa");

  // print an empty line
  Serial.println();

  // wait 1 second to print again
  delay(1000);
}


EDIT: i see after your comment about the int pin, then ok you already know

ArtiSchmidt

Thank you both for your help and for writing that pull request; I appreciate it. Everything is working as expected now.

Riccardo_Rizzo

welcome, than i'll merge on the official repository,

Riccardo

Go Up