Change the sample rate for Arduino NIcla sense me

I am so confused about the tutorial:


Can anyone help me to change the sample rate? since the data is expected to be read out every 10ms, therefore the sample rate should not be lower than 200Hz
And Can someone tell me the default value of these parameters of Nicla sense me?

For the benefit of other readers, I'll share a link to the tutorial in question:

https://docs.arduino.cc/tutorials/nicla-sense-me/cheat-sheet#standalone-mode

Here it is:
The SensorXYZ class inherits from SensorClass:
https://github.com/arduino-libraries/Arduino_BHY2/blob/1.0.3/src/sensors/SensorXYZ.h#L6

The default parameter values of SensorClass::begin:
https://github.com/arduino-libraries/Arduino_BHY2/blob/1.0.3/src/sensors/SensorClass.h#L15

bool begin(float rate = 1, uint32_t latency = 0);

As indicated by the tutorial, SensorClass::begin passes those parameters to SensorClass::configure:
https://github.com/arduino-libraries/Arduino_BHY2/blob/1.0.3/src/sensors/SensorClass.cpp#L29

configure(rate, latency);

How can I change the values in this case?


Unless I needed to adjust the configuration on the fly, I would just pass the rate and latency values as arguments in the accel.begin call.

For example, if you wanted a rate of 4 Hz and a latency of 2 ms, then you would change this line:

accel.begin();

to this:

accel.begin(4, 2);

If you do need to later change the settings, then you can use accel.configure:

accel.configure(4, 2);

Hii, I have changed my code into accel.begin(20, 1)


But the serial monitor shows:
image
Do I have to change some parameters in App as I am choosing Nicla acting as a slave and H7 lite Host:

1 Like

Please do not insert images of your code; paste it using the "Preformatted text" button (It looks like this: </>). This makes it easier & faster for us to help you :slight_smile:

Try making the following 3 changes (assuming you are using the ESLOV I2C connector):

  • On your Portenta sketch, change BHY2Host.begin(); to BHY2Host.begin(false, NICLA_VIA_ESLOV).
  • On your Nicla sketch, change BHY2.begin(); to BHY2.begin(NICLA_I2C, NICLA_VIA_ESLOV).
  • Also your data update rate on the Nicla should be faster than (or equal to) the data requesting rate on the Portenta. So try changing your BHY2.update(100) to BHY2.update(10) since you are requesting data every 50 ms.

Working Code

Here, I just tested this code and it works (4 files below):

Nicla.ino:

#include "Nicla_System.h"
#include "Arduino.h"
#include "Arduino_BHY2.h"

void colorCycle(int loops) {
  for(int i = 0; i < loops; i++) {
    nicla::leds.setColor(red);
    delay(1000);
    nicla::leds.setColor(green);
    delay(1000);
    nicla::leds.setColor(blue);
    delay(1000);
  }
  nicla::leds.setColor(off);
}

void setup(){
  BHY2.begin(NICLA_I2C, NICLA_VIA_ESLOV);
  nicla::leds.begin();
  colorCycle(5);
}

void loop(){
  // Update and then sleep
  nicla::leds.setColor(blue);
  BHY2.update(100);
  nicla::leds.setColor(off);
}

Portenta.ino:

#include "Arduino.h"
#include "Arduino_BHY2Host.h"
#include <LandoRGBLedPortenta.h>

SensorXYZ accel(SENSOR_ID_ACC);
LandoRGBLedPortenta portentaLeds;

const int MAX_DUPLICATE_READINGS = 10;
String colorToToggle = "white";
int duplicateReadings = 0;

static unsigned long printTime = 0;
static unsigned long startTime = 0;
static String timeToCrash = "";
static String lastAccelReading = "";

void setup()
{
  Serial.begin(115200);
  while(!Serial);
  Serial.println("Serial started!");
  portentaLeds.setColor("red");
  BHY2Host.begin(false, NICLA_VIA_ESLOV);
  Serial.println("BHY2HostSuccess!");
  portentaLeds.setColor("green");
  accel.begin(10, 1);
  Serial.println("Leaving setup!");
  portentaLeds.setColor("blue");
  
  printTime = millis();
  startTime = printTime;
}

void loop()
{
  BHY2Host.update();

  if (millis() - printTime >= 100) {
    printTime = millis();
    String accelReading = accel.toString();
    if(accelReading == lastAccelReading) {
      duplicateReadings++;
      if(duplicateReadings == MAX_DUPLICATE_READINGS) {
        timeToCrash = " " + String(printTime - startTime) + " ms";
        colorToToggle = "red";
      }
    }
    else {
      duplicateReadings = 0;
    }
    Serial.println(String("Acceleration values: ") + String(accelReading) + String(timeToCrash));
    portentaLeds.toggleColor(colorToToggle);
    lastAccelReading = accelReading;
  }
}

Add this folder and the following 2 files to your Arduino libraries (C:\Users\lando\Documents\Arduino\libraries\LandoRGBLedPortenta on my PC):

LandoRGBLedPortenta.h:

#ifndef LandoRGBLedPortenta_h
#define LandoRGBLedPortenta_h

#include "Arduino.h"

class LandoRGBLedPortenta
{
public:
    LandoRGBLedPortenta();
    void setColor(String targetColor);
    void toggleColor(String targetColor);

private:
    String _ledColor;
};

#endif

LandoRGBLedPortenta.cpp:

#include "Arduino.h"
#include "LandoRGBLedPortenta.h"

//Constructors:
LandoRGBLedPortenta::LandoRGBLedPortenta()
{
    this->_ledColor = "off";
}

//Publics:
void LandoRGBLedPortenta::setColor(String targetColor)
{
    this->_ledColor = targetColor;
    if (targetColor == "off")
    {
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, HIGH);
        digitalWrite(LEDB, HIGH);
    }
    else if (targetColor == "red")
    {
        digitalWrite(LEDR, LOW);
        digitalWrite(LEDG, HIGH);
        digitalWrite(LEDB, HIGH);
    }
    else if (targetColor == "green")
    {
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, LOW);
        digitalWrite(LEDB, HIGH);
    }
    else if (targetColor == "blue")
    {
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, HIGH);
        digitalWrite(LEDB, LOW);
    }
    else if (targetColor == "yellow")
    {
        digitalWrite(LEDR, LOW);
        digitalWrite(LEDG, LOW);
        digitalWrite(LEDB, HIGH);
    }
    else if (targetColor == "magenta")
    {
        digitalWrite(LEDR, LOW);
        digitalWrite(LEDG, HIGH);
        digitalWrite(LEDB, LOW);
    }
    else if (targetColor == "cyan")
    {
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, LOW);
        digitalWrite(LEDB, LOW);
    }
    else if (targetColor == "white")
    {
        digitalWrite(LEDR, LOW);
        digitalWrite(LEDG, LOW);
        digitalWrite(LEDB, LOW);
    }
}

void LandoRGBLedPortenta::toggleColor(String targetColor)
{
    if (this->_ledColor == "off")
    {
        this->setColor(targetColor);
    }
    else
    {
        this->setColor("off");
    }
}

Setup and Output

This is what my setup looks like:

And some serial data:

06:35:52.000 -> Acceleration values: XYZ values - X: 194   Y: 1129   Z: 3969
06:35:52.000 -> 
06:35:52.094 -> Acceleration values: XYZ values - X: 194   Y: 1129   Z: 3970
06:35:52.094 -> 
06:35:52.236 -> Acceleration values: XYZ values - X: 196   Y: 1130   Z: 3970
06:35:52.236 -> 
06:35:52.327 -> Acceleration values: XYZ values - X: 193   Y: 1129   Z: 3970
06:35:52.327 -> 
06:35:52.468 -> Acceleration values: XYZ values - X: 195   Y: 1126   Z: 3970
06:35:52.468 -> 
06:35:52.562 -> Acceleration values: XYZ values - X: 196   Y: 1128   Z: 3970
06:35:52.562 -> 
06:35:52.655 -> Acceleration values: XYZ values - X: 194   Y: 1130   Z: 3969
06:35:52.655 -> 
06:35:52.795 -> Acceleration values: XYZ values - X: 195   Y: 1130   Z: 3970
06:35:52.795 -> 
06:35:52.888 -> Acceleration values: XYZ values - X: 196   Y: 1128   Z: 3973
06:35:52.888 -> 
06:35:53.028 -> Acceleration values: XYZ values - X: 196   Y: 1129   Z: 3969
06:35:53.028 -> 
06:35:53.213 -> Acceleration values: XYZ values - X: 194   Y: 1128   Z: 3968
06:35:53.213 -> 
06:35:53.352 -> Acceleration values: XYZ values - X: 195   Y: 1127   Z: 3970
06:35:53.352 -> 

You can see the timestamp differences range from 90–150 ms -> 7–11 Hz.

Bugs

Do note however that there is a potential bug with the Nicla (only verified on my board right now) where the included code only works for half an hour before the connection screws up. Here are some links to my bug reports if you want to stay updated:

1 Like

Btw, I wasn't able to double the sampling freq to 20 Hz... I just got all zeros for 1st 3 readings and nothing after that.

If 10 Hz really is the limit then this board is a joke.

Same results... no wonder this board really is a joke.., lmao

1 Like

I am now so confused about what the unit of the data gotten by the accelerator is (i.e., m/s^2 or something else). If I do not know what I am getting from the board, the project becomes pointless then. :smiling_face_with_tear:

1 Like

There is a comment from the developer here that might help:

https://github.com/arduino/nicla-sense-me-fw/issues/46#issuecomment-1012338693

the unit of measure of the acceleration data is 1g = 9.80665 m/s^2.
With the current sensor configuration settings, the sensor has a range of +/-8G within 16bits of data. Then 1g = 4096.
You should then scale down the output data by 4096.

I just got a new task from my academic advisor which is visualising the motion from the data gathered by the accelerometer of Nicla sense me. Anyone got any ideas?

Visualising: the trajectory of the Arduino board.

Hii, Can anyone tell me the range of the accelerometer and the gyroscope? I am trying to normalize the gathered data.

1 Like

I would advise you to head over to the Bosch forums as they made all the sensor ICs on the Nicla and would be able to answer these types of questions. I don't think the guys at Arduino have any plans on improving their terribly lacking Portenta and Nicla documentation anytime soon and it doesn't seem like many people on the Arduino forums bought into the Portenta hype (and, given my experience, they probably have better judgement than I :joy:). Anyways, Bosch is probably your best bet.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.