Smart vibration sensor (Arduino)


I. Objectives of the tutorial

This project’s goal is to create a device that can be placed directly on a vibrating machine, that will learn its vibration patterns, and detect potential anomalies in its behavior.

This device will integrate and use Cartesiam’s NanoEdge AI Library, which will be selected via NanoEdge AI Studio.

Project constraints

The device will be able to:

  • register the user’s instructions (learn, detect, reset, etc.)
  • detect vibrations
  • learn vibration patterns
  • alert the user when an anomaly is detected

II. Requirements

1. Hardware

The required hardware for this project are:

2. Software

The required software for this project are:

III. Making a data logger

1. Objectives

This section’s goal is to create a data logger that can acquire vibrational data and log it via serial port (USB).

2. Putting the hardware together

Here is the hardware setup to reproduce, to connect the development board to the accelerometer via I2C:


3. Data logging

i. Getting started with Arduino

  1. Create your project.

  2. Download and Import Adafruit LIS3DH library:


ii. Collecting accelerometer data

  1. Clone the repository neai_arduino_vibration:

    $ git clone https://github.com/cartesiam/neai_arduino_vibration.git
  2. Connect the Arduino Nano 33 IoT board using the programming USB port and your fan to your computer.

  3. Put the accelerometer on your fan.

  4. Uncomment the line:

    #define DATA_LOGGING
  5. Deploy your code. Make sure to choose the correct board and port in the sketch Tools.

  6. Detect COM port, find port number and use the listed port as serial port.

    If you are on Linux:


    If you need to open a serial / COM port on Linux, check the FAQ (Section II. 9) for instructions.

    • Open a terminal and use the following command:

      $ ls /dev/ttyACM*
    • Copy the device name given by the command above (in our case /dev/ttyACM0) and use it in the following command:

      $ stty -F /dev/ttyACM0 115200 raw -clocal -echo icrnl

    If you are on Windows:

    • Open Device Manager > Ports (COM and LPT)
    • Copy the serial port used by your electronic board; here we will use COM7.
  7. Collect regular signals datasets:

    If you are on Linux:

    $ cat /dev/ttyACM0 > regular.csv

    If you are on Windows:

    • Download and install PuTTy.
    • Use the serial port that you copied from the previous step (here COM7) and use it in the following command:

      plink -serial COM7 -sercfg 115200 > regular.csv


    Data logging is in an infinite loop. Stop it when you want. You will need at least 100 lines.

  8. Collect abnormal signals datasets:

    • Put a Post-It on the fan’s grid (to partly obstruct it).
    • Repeat step 5 for abnormal signals collection, to obtain the abnormal.csv file.

IV. Using NanoEdge AI Studio to find the best library

1. Objectives

The goal of this section is to use NanoEdge AI Studio to find the static library that is most relevant to our embedded application.

2. Getting started with NanoEdge AI Studio

  • Click this link and fill the form to receive instructions on how to download NanoEdge AI Studio.
  • After confirming your email address, you will get a license key, that you will need when first launching the software.
  • For more information on how to set up NanoEdge AI Studio, of for offline activation instructions, please check the FAQ and/or the NanoEdge AI Studio docs.

3. NanoEdge AI Studio step-by-step guide

i. Creating a new project

In the main window, create a new project.

  • Select microcontroller type: ARM Cortex M0+

  • Choose the maximum amount of RAM, in kB: here 32kB

  • Select the sensor type: 3-axis accelerometer


ii. Importing signal files

In these two next steps (step 2 and step 3), you will import your two signal files:

  1. The Regular signals file, corresponding to nominal fan behavior, i.e. data acquired by sensors during normal use, when everything is functioning as expected: regular.csv.
  2. The Abnormal signals file, corresponding to abnormal fan behavior, i.e. data acquired by sensors during a phase of anomaly: abnormal.csv.

For each .csv file, you will consecutively:

  • Import the file.

    ../../_images/22_screen2_top2.png ../../_images/33_screen3_top2.png
  • Select the correct delimiter (space) and file path, and then validate import.

    ../../_images/import_regular_signal.png ../../_images/import_abnormal_signal.png
  • Make sure that your file is correctly formatted.

    ../../_images/test_regular_signal.png ../../_images/test_abnormal_signal.png

iii. Benchmarking the library

In this 4th step, you will start and monitor the library benchmark. NanoEdge AI Studio will search for the best possible library given the signal files provided in steps 2 and 3 (see previous section).

a. Starting the benchmark:

Click START to open the signal selection window.


Then, select the number of microprocessor cores from your computer that you wish to use (see below). When you are ready to start the benchmark, click Validate.

b. Monitoring the benchmark:

NanoEdge AI Studio uses 3 indicators to translate the performance and relevance of candidate libraries, in the following order of priority:

  • Balanced accuracy
  • Confidence
  • RAM

You can monitor in real time the performance of the candidate libraries, and the evolution of the 3 performance indicators.

../../_images/perf_plot1.png ../../_images/progress_plot.png

See Studio docs: graphs and performance indicators for more information.


The benchmark process may take some time.
Please be patient; have a break, grab a drink.

Only interrupt the benchmark for testing purposes, and don’t expect good results unless all performance markers are at 90% minimum.

A summary screen will appear as soon as the benchmark is complete. It shows the minimum number of learning iterations needed to obtain optimal performances from the library, when it is embedded in your final hardware application. In this particular example, NanoEdge AI Studio recommended that the learn() should be called 80 times, at the very minimum.



  • Never use fewer iterations than the recommended number, but feel free to use more (e.g. 3 to 10 times more).
  • This iteration number corresponds to the number of lines to use in your input file, as a bare minimum.
  • These iterations must include the whole range of all kinds of nominal behaviors that you want to consider on your machine.

iv. Downloading the NanoEdge AI Emulator

In the Studio: Step 5, you have you can download the NanoEdge AI Emulator associated to the library that was selected during the benchmark process (Step 4).

../../_images/5_emulator_top2.png ../../_images/download_links1.png

NanoEdge AI Emulator is a command-line tool that emulates the behavior of the associated library. It is a clone of the library (same behavior, performances) that can run locally on Windows or Linux, without being embedded in a microcontroller.

You can download the Emulator and use it via the command line, or via the interface provided in the Studio.

v. [PRO] Compiling and downloading the NanoEdge AI Library

In this last step (number 6), the library will be compiled and downloaded, ready to be used in your embedded application.



This is only available in the Full version of NanoEdge AI Studio. In this tutorial, you will be able to use the Emulator, and test the performances of the library “live”. You won’t be able to use the library directly and make a fully autonomous device (Section VI), unless you have the Full version.

Before compiling the library, several compilation flags are available, both checked by default (in our example and the hardware used int this tutorial, keep default settings).


If you ran the selection process several times, make sure that the correct benchmark is selected. Then, when you are ready to download the NanoEdge AI Library, click Compile.


After carefully reading the license contract terms, click Accept and compile. After a short delay, a .zip file will be downloaded to your computer.


It contains all relevant documentation, the NanoEdge AI Emulator (both Win32 and Unix versions), the NanoEdge AI header file NanoEdgeAI.h, and a .json file containing some library details.

Congratulations; you can now use your NanoEdge AI Library and Emulator!

V. Using NanoEdge AI Emulator to test your library

1. Objectives

With NanoEdge AI Emulator, you can emulate the behavior of your NanoEdge AI Library from the command line, or the interface provided through NanoEdge AI Studio or your development board via serial port and a python script to display the results on the screen.

The Emulator comes with each library created by the Studio (step 5). It has been compiled using the same algorithm. This means that for one library, there is one emulator. Using this tool, you can learn and detect as you would do in your embedded application.

You may learn using the regular signals used in the Studio, or any other dataset you consider as “nominal”. You may detect using the abnormal signals used in the Studio, or any other dataset you consider as “abnormal”.

In this section we explain how to test the library generated via NanoEdge AI Studio using NanoEdge AI Emulator.

2. Testing the library with NanoEdge AI Emulator

i. Via the interface provided in NanoEdge AI Studio

In the 5th step of the NanoEdge AI Studio step-by-step guide, you can test the library that was selected during the benchmark process (step 4). This step provides an interface to test the main NanoEdge AI Library functions via the NanoEdge AI Emulator.

To emulate the behavior of your NanoEdgeAI Library generated from Studio, using NanoEdge AI Studio interface, please refer the following section: using the Emulator within the Studio.

ii. Via command line

Download the NanoEdge AI Emulator via the links provided on the left side of the screen:


To emulate the behavior of your NanoEdgeAI Library generated from the Studio, using command line, please refer to the NanoEdge AI Emulator documentation.

VI. [PRO] Using your NanoEdge AI Library to build your final product

1. Objectives

In this section we will compile our static library and use its functions to build the desired features on our device.


Take some time to read the code we provide in the file NanoEdgeAI_tuto.ino.

2. Compiling and linking your NanoEdge AI Library

  1. In the Studio (Step 6), you download the NanoEdge AI Library. You will get a .zip file.

  2. Copy the repository NanoEdgeAI from the https://github.com/cartesiam/neai_arduino_vibration.git repository into the Arduino library folder Documents\Arduino\libraries.

    Then copy the static library libneai.a in the Documents\Arduino\libraries\NanoEdgeAI\src\cortex-m0plus directory.

    Finally, copy the NanoEdgeAI.h header file in the Documents\Arduino\libraries\NanoEdgeAI\src directory.

  3. Open your sketch, and comment this line:

    //#define DATA_LOGGING
  4. Import the NanoEdge AI library.

  5. In our particular example, NanoEdge AI Studio recommended that the learn() should be called 80 times (see graph in 4th step of NanoEdge AI Studio guide above). Feel free to increase this number, the more, the better.

  6. Connect the Arduino Nano 33 IoT board to your computer.

  7. Deploy the sketch on your board. Have a look at your serial for the detection results.

Now you are ready to play with your new smart sensor!

3. Playing around with your smart sensor

  1. Connect the fan to your computer, put the accelerometer on your fan.
  2. Set fan speed controller on the speed you want to learn (“Low”, “Medium” or “High”).
  3. Now you can easily check that the behavior of the NanoEdge AI Library conforms to your expectations, by switching speed controller position and creating anomalies (e.g. obstruct the grid of the fan and/or lightly tap the fan).


All NanoEdge AI Studio documentation is available here.
Step-by-step tutorials, to use NanoEdge AI Studio to build a smart device from A to Z:

Useful links: