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