Fan Anomaly Detection with


Deploying Your Fan Anomaly Detector

This tutorial will guide you through the process of building a vibrational anomaly detector using NanoEdge AI Studio and deploying it to the SAMD21 development board.

We'll be capturing accelerometer data using the IMU2 Click board and use NanoEdge AI Studio to train a library that can model the nominal behavior of our USB case fan. Then we'll deploy that library to the SAMD21 MCU to detect anomalous events (in this case, fan blockage) in real-time.

Once you've completed this project, you should have the knowledge and starter code to build an anomaly detector that fits your own needs.


Hardware Tools

  • USB powered PC Case Fan
    • For example, this fan from Amazon. Any similar fan with a grill will work.
  • Double-sided tape or mounting squares (for adhering the development board to the fan)
  • Post-it (or similar to partially block fan intake)

Software Tools

Exercise Files

File Download
Windows Linux Mac OSX
Project and Source Files

Firmware Details

Firmware Configurations

The included project has 3 possible firmware 'modes' as well as the possible selection of the IMU2 or IMU14 sensors, amounting to 6 possible firmware configurations. The table below summarizes how to select each of these configurations.

MPLAB X Project Configuration Selection config/user.h Configuration Values Firmware Build Description
SAMD21_IOT_WG_BMI160 #define DATA_VISUALIZER_BUILD true #define DATA_LOGGER_BUILD false Data logger for MPLAB Data Visualizer using Bosch IMU2 sensor
SAMD21_IOT_WG_BMI160 #define DATA_VISUALIZER_BUILD false #define DATA_LOGGER_BUILD true Data logger for Cartesiam NanoEdge AI Studio using Bosch IMU2 sensor
SAMD21_IOT_WG_BMI160 #define DATA_VISUALIZER_BUILD false #define DATA_LOGGER_BUILD false NanoEdge AI Anomaly Detection using Bosch IMU2 sensor
SAMD21_IOT_WG_ICM42688 #define DATA_VISUALIZER_BUILD true #define DATA_LOGGER_BUILD false Data logger for MPLAB Data Visualizer using TDK IMU14 sensor
SAMD21_IOT_WG_ICM42688 #define DATA_VISUALIZER_BUILD false #define DATA_LOGGER_BUILD true Data logger for Cartesiam NanoEdge AI Studio using TDK IMU14 sensor
SAMD21_IOT_WG_ICM42688 #define DATA_VISUALIZER_BUILD false #define DATA_LOGGER_BUILD false NanoEdge AI Anomaly Detection using TDK IMU14 sensor

Anomaly Detection Firmware Benchmark

Firmware Build Parameters ROM Size RAM Size Inference Time
-O2 optimizations, 48MHz Clock ~46kB ~14kB ~153ms


Before we get started, you'll need to install and set up the required software as detailed in the steps below.

First, install the MPLAB X IDE and XC32 compiler. These are required to program the SAMD21. You can use the default free license for the XC32 compiler as we won't need any of the pro functionality here.

Secondly, download NanoEdge AI Studio. We'll use this to analyze our sensor data and generate the anomaly detection library. Head over to the NanoEdge AI Studio download page and fill out the form to get the free trial version. Once installed, we strongly recommend you peruse the NanoEdge AI Studio documentation to get familiarized with the development flow.

Finally, download the MPLAB X project which includes all the source code and configuration files required for this tutorial.

Connect the Mikroe IMU2 Click

While the SAMD21 board is powered off, plug the Mikroe IMU2 Click into the MikroBUS socket.

Mount the SAMD21 board to your fan

Before we start collecting data, we’ll need to attach the SAMD21 board to our fan. We recommend using double-sided tape to adhere the SAMD21 board to the fan in order to create a reliable coupling between the fan and sensor. My setup is shown in Figure 1.

Figure 1. Mounting Adhesive

Note that simply placing the board on top of the fan without bonding may give inconsistent results as any minor perturbation could change the positioning of the board thereby changing the vibrational patterns recorded by the IMU.

Program the MCU with Data Logger firmware

Before training our anomaly library, we’ll need to program the SAMD21 with the appropriate firmware to print the IMU sensor data to the USB UART port so that it can be imported into NanoEdge AI Studio. There are actually two avenues for this:


Use the ML Plugin within MPLAB Data Visualizer to collect your data and export it to a file readable by NanoEdge AI Studio.


Capture data directly from NanoEdge AI Studio.

For the remainder of this guide, we’ll assume you’re using option (b) from above. This is the default configuration of the project so we won’t be making any code changes in this step of the project.


If you haven’t already done so, connect the SAMD21 board to your PC via the USB port; take note of the serial COM port corresponding to the device as you’ll need it later.


Download the MPLAB X project archive (ZIP file), and extract the archive to some local directory; for the rest of this guide, we’ll refer to this directory as <project_path>.


Start MPLAB X.


Click File > Open Project… and in the resulting Open Project dialog box find the cartesiam_fan_demo.X project folder located at <project_path>/cartesiam/firmware/cartesiam_fan_demo.X where <project_path> is the path to the extracted archive from Step 2. See Figure 2 for guidance.

Figure 2. Opening the cartesiam_fan_demo.X project


From the project tree, open the user.h file under Header Files > config. See Figure 3 for guidance.

Figure 3. user.h Configuration File


(Optional) If you’re using MPLAB X ML Plugin:

  • Find the line in user.h that says #define DATA_VISUALIZER_BUILD false and replace it with #define DATA_VISUALIZER_BUILD true. Also replace the line that says #define DATA_LOGGER_BUILD true with #define DATA_LOGGER_BUILD false.
  • If you’re not using the ML Plugin then the default configuration (DATA_LOGGER_BUILD) will work fine.


In the Toolbar, select SAMD21_IOT_WG_BMI160 from the Project Configuration drop down box assuming you're using the IMU2 board as indicated in Figure 4; if you're using the IMU14 board you should select SAMD21_IOT_WG_ICM42688 here. This will select which sensor driver to load.

Figure 4. Project Configuration


Click the Make and Program button on the MPLAB toolbar as indicated in Figure 5.

Figure 5. Make and Program

Once programming completes, the board will reset and start logging three-axis accelerometer data over the USB UART port.

Generate NanoEdge AI Library

Now that the SAMD21 board is set up to stream accelerometer data, we can use NanoEdge AI Studio directly to capture the data we need to build our anomaly detection library.

The following sections will give a brief run-through of the steps required to generate a NanoEdge AI library. For a more detailed guide, check out Cartesiam’s own video guide, which is very similar to our own example.

Create a new project


Start NanoEdge AI Studio.


Create a new project with the following parameters:

  • Target: M0+
  • Max RAM: 16 kB
  • Sensor Type: Accelerometer three axes
Figure 6. NanoEdge AI Create Project


Click CREATE. This will take you to Step 2 of the development pipeline.

Note that it is required to select the three axes accelerometer here, any other configuration would require changes to the firmware provided in this tutorial.

Collect Regular Signal Data

Figure 7. Fan Setup


In the “Regular signals” step in the NanoEdge AI pipeline, click the Choose Signals button.


In the window that opens, click the From serial (USB) tab


In the From serial (USB) tab, set the following options

  • For Serial Port, select the COM port corresponding to the SAMD21 board
  • For Baud rate, enter: 115200
  • For Delimiter, choose: Space
  • Mark the checkbox labeled Max Lines and in the adjacent box enter 10 for Number of lines. With the default firmware configuration, this should correspond to ~25 seconds of data.


Take a second to check that the SAMD21 board is firmly mounted to the fan, then click the red record icon to start capturing data. Make sure when collecting this data that no spurious vibrations are being introduced by e.g., closing doors, keyboard typing, etc.


Once the collection is complete, click the Validate import button


NanoEdge Studio AI will now perform several validation checks on your data. The data you collected should pass all these tests.

Collect Abnormal Signal Data

Figure 8. Blocking Fan with Post-it


Attach the post-it note to the grill of the fan so that it obstructs air intake.


Repeat the steps detailed in the previous section to capture and verify the anomaly data.

Generate NanoEdge AI Library


Click the Optimize and Benchmark step in the NanoEdge AI pipeline.


Click Start. NanoEdge AI will now run some optimizations algorithms to generate a library for the target that works well for the data we previously collected.


Once optimization is complete, note down the number of iterations needed to give meaningful results. We’ll need to enter this number into the firmware configuration later on.

Compile NanoEdge AI Library

Finally, we can download our generated library. Note that we’re skipping the optional “Emulator” step - we’ll be going straight to hardware.


Click the Deploy step in the NanoEdge AI pipeline.


Click Compile and choose Development version in the resulting window to download your library.

By the way, don’t worry about the “hello world” code that NanoEdge AI generates - our firmware has this covered already.

Program the MCU with NanoEdge AI Firmware

With our library now built, it’s time to program the SAMD21 to utilize our newly generated NanoEdge AI library.


Unzip the contents of the NanoEdge AI archive downloaded in the previous section directly into <project_path>/cartesiam/firmware/neai. Note this should replace the existing placeholder files in our project.


Open up the cartesiam_fan_demo.X project in MPLAB X.


From the project tree, open the user.h file under Header Files > config.


In the user.h file, find the line that says #define DATA_LOGGER_BUILD true and replace it with #define DATA_LOGGER_BUILD false. This will disable the data logging functionality in the firmware and run the NanoEdge AI library instead.


Also in user.h, find the line that says #define LEARNING_ITERATIONS and replace the value at the end of the line with the number of iterations recommended by NanoEdge AI studio. In case you forgot, the recommended value is output in the “Optimize and Benchmark” step of the NanoEdge AI pipeline.


Click the Make and Program button to load the NanoEdge AI firmware.

Training and Testing the Anomaly Detector

Program Flow

In a typical machine learning application, we would deploy a fixed model to our MCU and be done; however, the library generated by NanoEdge AI only defines the skeleton of a model - the model itself will actually learn on the MCU. That being said, there are three states in the firmware that will be indicated by the green LED on the SAMD21 board:

Figure 9. Status LED


Initialization - Once the firmware has booted up, it will wait for you to hit SW0 before proceeding to training. During this state, the green LED will be turned off.


Training - When SW0 has been pressed, the firmware will pause for a few seconds and then start the training process. During this state, the green LED will blink slowly.


Inference - Once training completes, the firmware will go into the anomaly detection state indefinitely. During this state, the green LED will be solid green during regular operation and will blink quickly if an anomaly is detected.

Running the Firmware

At this point, your SAMD21 board should be on and loaded with the NanoEdge AI inferencing firmware.


(Optional) Connect to the SAMD21 serial port using a terminal emulator of your choice (PuTTY is great if using Windows) with the following settings:

  • Baud rate: 115200
  • Data bits: 8
  • Stop bits: 1
  • Parity: None
  • Flow Control: XON/XOFF

This step is optional, but it is useful for getting more detailed information on what’s happening in the firmware.


The procedure for the training step that’s done on the firmware is the same as was done in the “Regular signals” acquisition step in NanoEdge AI Studio, so start by checking that your SAMD21 board is properly mounted to your USB fan and that the fan is operating normally.


When you’re ready, press SW0 to start the training process. Note the firmware will give you a few seconds after the button press before starting training to avoid capturing any disturbance caused by touching the board. The status LED will start blinking slowly when training starts.


When training is complete (solid green LED state), try attaching the post-it to the fan intake again - the green LED should start blinking quickly indicating an anomaly - on the removal of the post-it the green LED should stop blinking.


That’s it! You’re now ready to start building your anomaly detection application.

To explore things further, you may want to experiment with changing some of the project parameters; for example, the NEAI buffer length (the number of samples that are considered by the NanoEdge AI library every time the detection function is called) and the IMU sampling rate. Configurable parameters can be changed through the previously mentioned user.h file in the included project (see code comments for details). Note that if you change any of the sensor (SNSR*) parameters or the Nano Edge AI buffer length (NEAI_BUF_LEN) you will need to go through the process of generating a new NanoEdge AI library, including capturing new data.

To learn more about the NanoEdge AI library, including how to tune the sensitivity and threshold of the detector, check out the documentation.

To learn more about the ML Plugin to MPLAB X Data Visualizer check out the documentation.

© 2021 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.