Getting Started with Nicla Sense ME

Tutorial for getting started with the Nicla Sense ME, getting readings from the sensor on the board

Introduction

This tutorial will show you how to get started with your Nicla Sense ME board and how it can be utilized by itself. It will showcase the boards IMU and other sensors to get readings which we can easily display. We will go through a basic sketch for the Nicla Sense ME and see the potential of the board.

Goals

The goals of this project are:

  • Learn about the Nicla Sense ME
  • Learn how to use the IMU on the Nicla Sense ME
  • Learn how to set up the Nicla Sense ME with Arduino IDE

Hardware & Software Needed

  • Nicla sense ME
  • Arduino IDE
  • Micro USB cable

Sensors on the Nicla Sense ME

The Nicla Sense ME's BHI sensor includes a 6-axis IMU, we will use this IMU to give us accelerometer and gyroscope readings. An accelerometer is an electromechanical device used to measure acceleration forces. Such forces may be static, like the continuous force of gravity or, as is the case with many mobile devices, dynamic to sense movement or vibrations. A gyroscope sensor can measure and maintain the orientation and angular velocity of an object. Gyroscopes are more advanced than accelerometers, as they can measure the tilt and lateral orientation of an object, whereas an accelerometer can only measure its linear motion.

You can see the full list with all the needed information about the sensors here.

Sensor Types

To access the Arduino Nicla Sense ME sensor's data you will need to declare the type of the sensor you want to use (they are called constructors), which have different data functions that return data (e.g Accelerometer will have X, Y and Z values).

The Arduino_BHY2 library contains these sensors:

Sensor

value()
- Returns the data, for example if you use the Temperature sensor, it will give you the Temperature reading.
toString()
- Prints the value(s) in a String, you can use it to print all the data into the serial monitor.

Syntax example

1#include "Arduino_BHY2.h"
2
3 Sensor temperature(SENSOR_ID_TEMP);
4 float temperatureValue = 0;
5
6 void setup(){
7 Serial.begin(115200);
8 BHY2.begin();
9 temperature.begin();
10 }
11
12 void loop(){
13 BHY2.update();
14 temperatureValue = temperature.value();
15
16 Serial.print("Temperature :");
17 Serial.println(temperatureValue);
18 }

All of the following Sensor types also have the Sensor functions

value()
and
toString()

SensorXYZ

x()
- gives X axis
y()
- gives Y axis
z()
- gives Z axis

Syntax example

1#include "Arduino_BHY2.h"
2
3 SensorXYZ gyroscope(SENSOR_ID_GYRO);
4 int16_t valueX;
5 int16_t valueY;
6 int16_t valueZ;
7
8 void setup(){
9 Serial.begin(115200);
10 BHY2.begin();
11 gyroscope.begin();
12 }
13
14 void loop(){
15 BHY2.update();
16 valueX = gyroscope.x();
17 valueY = gyroscope.y();
18 valueZ = gyroscope.z();
19
20 Serial.println(gyroscope.toString); //Prints all the data "automatically"
21
22 //Print the individual values
23 Serial.println(valueX);
24 }

SensorOrientation

heading()
- returns Z axis rotation
pitch()
- returns X axis rotation
roll()
- returns Y axies rotation

Syntax example

1#include "Arduino_BHY2.h"
2
3 SensorOrientation orientation(SENSOR_ID_DEVICE_ORI);
4
5 void setup(){
6 Serial.begin(115200);
7 BHY2.begin();
8 orientation.begin();
9 }
10
11 void loop(){
12 BHY2.update();
13
14 Serial.print("orientation pitch :");
15 Serial.println(orientation.pitch());
16 delay(500);
17 }

SensorQuaternion

x()
- gives X quaternion (0-1 signed float)
y()
- gives Y quaternion (0-1 signed float)
z()
- gives Z quaternion (0-1 signed float)
w()
- gives W quaternion (0-1 signed float)

Syntax example

1#include "Arduino_BHY2.h"
2
3 SensorQuaternion quaternion(SENSOR_ID_RV);
4
5 void setup(){
6 Serial.begin(115200);
7 BHY2.begin();
8 quaternion.begin();
9 }
10
11 void loop(){
12 BHY2.update();
13
14 Serial.print("quaternion w :");
15 Serial.println(orientation.w());
16 delay(500);
17 }

SensorActivity

getActivity()
- gives you a string with the current activity.

This are the activities:

  • 0: "Still activity ended"
  • 1: "Walking activity ended"
  • 2: "Running activity ended"
  • 3: "On bicycle activity ended"
  • 4: "In vehicle activity ended"
  • 5: "Tilting activity ended"
  • 6: "In vehicle still ended"
  • 7: blank
  • 8: "Still activity started"
  • 9: "Walking activity started"
  • 10: "Running activity started"
  • 11: "On bicycle activity started"
  • 12: "IN vehicle activity started"
  • 13: "Tilting activity started"
  • 14: "In vehicle still started"
  • 15: blank

Syntax example

1#include "Arduino_BHY2.h"
2
3 SensorActivity activity(SENSOR_ID_AR);
4
5 void setup(){
6 Serial.begin(115200);
7 BHY2.begin();
8 activity.begin();
9 }
10
11 void loop(){
12 BHY2.update();
13
14 Serial.print("Activity status: ");
15 Serial.println(activity.getActivity());
16 delay(500);
17 }

The Nicla Sense ME also has environmental sensor, we will use this in the sketch to get temperature and gas readings. This sensor is also capable of reading humidity, pressure and volatile organic compound. Making this board ideal for environmental monitoring especially in and around sensitive electronic equipment.

Next we will go through a sketch that allows us to see the readings from the sensors.

Instructions

Arduino IDE and Nicla Core

Start the Arduino IDE and install the nicla_mbed core. If you need help with installing the core for the Nicla Sense ME, please refer to this guide.

Now we need to add the Arduino_BHY2 library that contains the example sketch that we want, we can do this through the library manager in the Arduino IDE. After the library is installed you can access the example sketch directly through the IDE. You can find the sketch in File->Examples->Arduino_BHY2->Standalone. Now you can plug in your Nicla Sense ME and upload the sketch.

Image of the example in list
Image of the example in list

Next we will go through parts of the sketch and explain them.

Programming the Board

With this sketch we can get readings from the Nicla Sense ME's sensors. It includes accelerometer, gyroscope and temperature readings. This simple sketch will show how to get these readings, which can be useful for applications of the Nicla Sense ME.

In the setup part of the sketch we first use:

1#include "Arduino_BHY2.h"
2
3 SensorXYZ accel(SENSOR_ID_ACC);
4 SensorXYZ gyro(SENSOR_ID_GYRO);
5 Sensor temp(SENSOR_ID_TEMP);
6 Sensor gas(SENSOR_ID_GAS);

This defines the sensors that we will use later in the sketch. Giving our sensors the

accel
,
gyro
,
temp
and
gas
sensor class names.

We can then use these variable names when needed in the sketch. The next thing to do is to enable the sensors. We do that with this code that sets the sensors rate and a latency:

1accel.begin();
2 gyro.begin();
3 temp.begin();
4 gas.begin();

The library lets us activate the sensors with a simple call of the

begin()
function. Automatically setting the rate and latency of the sensor.

To print the readings in the serial monitor we use the following code, converting the readings into strings. With the temperature and gas values we limit the output to 4 digits so that it does not clutter the serial monitor.

1Serial.println(String("acceleration: ") + accel.toString());
2 Serial.println(String("gyroscope: ") + gyro.toString());
3 Serial.println(String("temperature: ") + String(temp.value(),3));
4 Serial.println(String("gas: ") + String(gas.value(),3));

Running the Sketch

After you have uploaded the sketch to the board, open the serial monitor in the Arduino IDE. You should see similar outputs to the ones shown in the image below. Make sure that the baud rate on your serial monitor is set to 115200.

Serial monitor output
Serial monitor output

Next Step

In this sketch we only get the readings of the sensor. A good next step could be to use these readings as parameters to trigger different reactions on the board, with for example the led on the board. Please take a look at our cheat sheet if you want quick references for the boards features. Take a look at the other tutorials we have for the Nicla Sense ME to discover more about this board.

Conclusion

In this tutorial you learned how to use the Nicla Sense ME on it's own, getting multiple readings from its sensors. Printing the readings in the serial monitor for easy viewing.

Tutorial Toolbox

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.