Predictive Maintenance Using Machine Learning on Edge Devices
Getting Started with Sensor Featherwing and Portenta H7
Predictive maintenance is a type of condition-based monitoring, like temperature, vibration, and humidity, that uses sensor devices to monitor the condition of assets such as industrial machines. Any machine failure would cause a high loss of production. To prevent it, predictive maintenance can indicate when machines will require maintenance, allowing this to be scheduled and avoiding unplanned failures. Part of this monitoring process is Machine Learning (ML). ML typically requires high-performance processing, but with the technology getting advanced, it’s now possible to run ML algorithms on embedded devices. This article will show you how to get started with predictive maintenance, from reading the sensor data to developing the ML model that will classify a motor state. This project is a proof of concept and was done using a fan motor.
Project Material and Resources
782-ABX00042: Arduino Portenta H7
710-2501000201291: Würth Elektronik Sensor FeatherWing
485-4482: Adafruit male -male jumper wires
485-4472: Adafruit USB Type-C cable
Software and Tools:
- Visual Studio Code
- PlatformIO
- Edge Impulse
- PC
Project Technology Overview
The Würth Elektronik Sensor Featherwing development board features four sensors: temperature, humidity, acceleration, and absolute pressure sensors. All four sensors are connected over a shared I2C bus. This development kit is fully compatible with the Adafruit Feather ecosystem, but I’ll show you how to get started using an Arduino Pro board in this article.

The Arduino Portenta H7 is a dual-core processor featuring the STM32H747, including a 480MHz Arm® Cortex®-M7 and a 240MHz Arm Cortex M4. This allows it to run machine learning models on the high-processing M7, and the other processor can run low-level operations like reading sensor data or handling network communication (WiFi® and Bluetooth®).

Edge Impulse
Edge Impulse is a development environment for machine learning on embedded devices. It enables the collection of real world sensor data, develops a ML algorithm, and easily deploys the model on any edge device.

Hardware Setup
Assembling the hardware is straightforward as we’ll only need to connect four pins. We’ll read the sensor data from the FeatherWing through the I2C interface. So, connect the pins as follows:
Portenta | FeatherWing |
GND | GND |
3V3 | 3V3 |
I2C SCL | SCL |
I2C SDA | SDA |
Software Setup
The software setup consists of two parts: Setting up Visual Studio and PlatformIO extension to program the Portenta. PlatformIO enables quick and easy prototyping on Visual Studio Code for the Arduino platform. The second part is configuring Edge Impulse CLI on your PC to train your model.
Installing Visual Studio Code and PlatformIO:
- Download Visual Studio Code and follow the installation steps here
- Open VSCode Extension Manager on the left hand
- Search for the official PlatformIO IDE extension
- Install PlatformIO IDE (Figure 4)

Installing the Edge Impulse CLI:
The following steps are for Windows computers:
- Install Python 3
- Install Node.js v14 or higher. For Windows users, install the Additional Node.js tools (called Tools for Native Modules on newer versions) when prompted.
- Install the Arduino CLI
- Open a PowerShell command line and run it as an administrator
- Install the Edge Impulse CLI using that command: npm install -g edge-impulse-cli –force
Note: If you get an error using this command. Then, try using this workaround by running this command instead:Set-ExecutionPolicyBypass-ScopeProcess-Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iwr -Uri “https://raw.githubusercontent.com/edgeimpulse/ei-install-scripts/main/install-windows.ps1” -Outfile install.ps1; iex .\install.ps1; del .\install.ps1
Vibration Monitoring
The first part of this project is to program the Portenta H7 to read the G Force data from the Sensor FeatherWing accelerometer and convert this data to acceleration. Würth Elektronik offers a ready-to-go library that will quickly get you started with this. The files are uploaded on Mouser’s Github repo.
- Download the repository on your computer
- In Visual Studio Code: Click on File -> Open Folder -> from the downloaded repository, choose the folder Read sensor data, and click Open
- Click on the Explorer tab on the left-hand side panel. Then you can see the code project. Under src, you’ll find the main.cpp file. (Figure 5)

- To run the code, click on the PlatformIO icon on the left-hand side panel. Click Build and after it finishes building the project, click on Upload and Monitor. (Figure 6)

- You should start seeing the sensor readings printing in the terminal.
- To change these G Force readings to acceleration, we multiply each data by 80665f (Figure 7)

Machine Learning model training
Edge Impulse makes building a ML model very easy. You only need to collect the sensor data and train the model on the Edge Impulse platform. Since Edge Impulse doesn’t support the sensor we’re using, we’ll use the Data Forwarder option to push data to the Edge Impulse project. The Data Forwarder reads the Serial Monitor output and uploads the readings to the associated Edge Impulse project.
- Create an account on the Edge Impulse website
- Create a new project
- On your computer, open a PowerShell window and run the following command:
edge-impulse-data-forwarder - Enter your Edge Impulse username and password
- Choose the correct COM port that your Arduino board is connected to. (On windows, open device manager and then click on COM ports)
- Then, choose the project that you’ve created in Step 2
- The data forwarder should detect that data is being printed on your Serial port. It’ll ask you to write your variable’s names. Since we’re reading acceleration data, we’ll name it accX, accY, accZ. (Figure 8)

That’s it in the console, and now we’ll move to the edge impulse website to start collecting the data and train the model.
In your project, click on data acquisition on the left-hand side panel. On the right side, the platform should detect that your device is connected through the data forwarder, so you can start collecting the samples. (Figure 9)

For this project demonstration, I’ll be creating a ML model that classifies three states of a motor:
- Idle (the motor is off)
- Normal (the motor is on)
- Error (when there are any abnormal vibrations)
For each state, I’ve collected 4 min of data. Make sure to label it correctly.
Creating the impulse
Let’s create the impulse, which takes our raw data, uses signal processing to extract features, and then uses a learning block to classify new data. (Figure 10)
- Depending on your application, adjust the time series data
- For the processing block, choose Spectral Analysis
- For the learning block, choose Classification (Keras)
- Save the impulse.

Spectral Features
Using the Spectral features block, we’ll extract our input signal’s frequency and power characteristics. It can be customized depending on your application, so for this article, I applied a high-pass filter to analyze repetitive patterns in the vibrations from our motor. (Figure 11)

Neural Network Classifer
The neural network classifier takes some input data and outputs a probability score indicating the likelihood that the input data belongs to a particular class. For this project, I’ve set the training cycles to 100. The training output is shown on the right with its accuracy. (Figure 12)

Export the impulse as an Arduino library and navigate to Visual studio to integrate the Edge Impulse SDK to your project.
Adding the Edge Impulse SDK to your project code
You’ll get .zip folder that contains those files: (Figure 13)

Move those files to your src folder where you have the main.cpp file. The main.cpp needs to be extended with code to classify the new data that the sensor will read.
I’ll highlight the main functions of that code. You can find the complete code to this tutorial on Mouser’s GitHub repo under the folder SensorFeatherwing_Portenta_predictive_maintenance.
- void run_inference_background(): This function runs our ML model continuously and takes our sensor readings as input. These get classified inside this function.
- Void loop(): Inside our loop function, we read the sensor data and store them in the buffer array passed to the run_inference_background().
Build and run the code and start monitoring your motor. Below you can see my ML output classifying the new sensor data in just 4ms. (Figure 14)

Conclusion
This article showed how to get started with building machine learning models on embedded devices. As this article is a proof of concept, this tutorial is your first step into building a real-world predictive maintenance application.
About the Author
To know more, click here
Source: Mouser Electronics