Skip to content

Real-time motor speed classification using TinyML on Raspberry Pi Pico W. MLP neural network trained with TensorFlow deployed on embedded hardware (5.3 KB model). Classifies motor vibration into 4 speed levels using MPU6050 accelerometer with live OLED display feedback. Complete ML workflow from data collection to edge deployment.

Notifications You must be signed in to change notification settings

JonasSouza871/Motor_Vibration_Classification_TinyML

Repository files navigation

Motor Speed Classification with TinyML

Python TensorFlow TensorFlow Lite C C++ Raspberry Pi Pico Jupyter scikit-learn License: MIT

Overview

This project implements a TinyML solution for real-time motor speed classification using accelerometer data from an MPU6050 sensor deployed on a Raspberry Pi Pico W microcontroller. The system uses a neural network trained on vibration data to classify motor operation into four distinct speed levels (0-3), providing real-time feedback through an OLED display.

The project demonstrates the complete machine learning workflow from data collection and preprocessing to model training, optimization for embedded systems, and deployment on resource-constrained hardware.

Technologies Used

Machine Learning & Data Science:

  • TensorFlow 2.12+ / Keras for neural network development
  • TensorFlow Lite Micro for embedded deployment
  • scikit-learn for data preprocessing and evaluation
  • NumPy and Pandas for data manipulation
  • Matplotlib and Seaborn for visualization

Embedded Systems:

  • Raspberry Pi Pico W (RP2040 microcontroller)
  • Pico SDK 2.1.0
  • C/C++ (C11/C++11 standards)
  • CMake build system

Hardware Components:

  • MPU6050 6-axis accelerometer/gyroscope (I2C interface)
  • SSD1306 OLED display 128x64 (I2C interface)

Development Environment:

  • Jupyter Notebook for model development
  • Visual Studio Code with Pico extension

Repository Structure

.
├── data/                           # Training data (CSV files)
│   ├── nivel0.csv                  # Motor speed level 0 samples
│   ├── nivel1.csv                  # Motor speed level 1 samples
│   ├── nivel2.csv                  # Motor speed level 2 samples
│   └── nivel3.csv                  # Motor speed level 3 samples
├── models/                         # Trained models
│   ├── motor_classification_model.keras    # Keras model
│   └── motor_classification_model.tflite   # TensorFlow Lite model (5.3 KB)
├── notebooks/                      # Jupyter notebooks
│   └── motor_speed_classification_tinyml.ipynb  # Model training pipeline
├── firmware/                       # Embedded firmware
│   ├── src/                        # Source code
│   │   ├── main.c                  # Main application logic
│   │   ├── mpu6050.c               # MPU6050 sensor driver
│   │   ├── ssd1306.c               # OLED display driver
│   │   └── tflm_wrapper.cpp        # TensorFlow Lite Micro wrapper
│   └── libs/                       # Header files
│       ├── motor_model.h           # TFLite model array
│       ├── scaler_params.h         # StandardScaler parameters
│       ├── mpu6050.h               # MPU6050 interface
│       ├── ssd1306.h               # SSD1306 interface
│       └── tflm_wrapper.h          # TFLite wrapper interface
├── images/                         # Analysis visualizations
├── pico-tflmicro/                  # TensorFlow Lite Micro library
├── CMakeLists.txt                  # Build configuration
├── requirements.txt                # Python dependencies
└── README.md                       # This file

Analysis

Dataset Distribution

The dataset consists of accelerometer readings from four different motor speed levels, evenly distributed across training, validation, and test sets.

Dataset Split

Model Performance

The neural network achieved high accuracy in classifying motor speed levels, with the following training curves:

Model Accuracy and Loss

Classification Results

Confusion Matrix

The confusion matrix demonstrates the model's ability to correctly classify motor speed levels:

Confusion Matrix

Classification Report

Detailed precision, recall, and F1-score metrics for each speed level:

Classification Report

How to Execute

Training the Model

  1. Install Python dependencies:
pip install -r requirements.txt
  1. Run the Jupyter notebook:
jupyter notebook notebooks/motor_speed_classification_tinyml.ipynb
  1. Execute all cells to train the model and generate the TFLite file.

Deploying to Raspberry Pi Pico W

  1. Install Pico SDK and toolchain following the official guide.

  2. Clone TensorFlow Lite Micro library:

git clone --recurse-submodules https://github.com/raspberrypi/pico-tflmicro.git
  1. Build the firmware:
mkdir build
cd build
cmake ..
make
  1. Flash the generated Motor_Classification_TinyML.uf2 file to the Pico W by:
    • Holding the BOOTSEL button while connecting the Pico to USB
    • Copying the .uf2 file to the mounted RPI-RP2 drive

Hardware Setup

  • Connect MPU6050 to I2C0: SDA (GPIO 0), SCL (GPIO 1)
  • Connect SSD1306 OLED to I2C1: SDA (GPIO 14), SCL (GPIO 15)
  • Power both devices with 3.3V and GND from the Pico

System in Action

MPU6050 Sensor Mounted on Motor:

OLED Display Showing Real-time Classification:

Key Results

  • Model Size: 5.3 KB (TensorFlow Lite format)
  • Inference Frequency: Real-time classification with 1-second update rate
  • Classification Accuracy: High performance across all four speed levels
  • Memory Footprint: Optimized for RP2040's 264 KB SRAM
  • Deployment: Successfully runs on Raspberry Pi Pico W with live sensor data

The system demonstrates effective TinyML deployment, achieving accurate motor speed classification on resource-constrained hardware while maintaining real-time performance.

Limitations

IoT Connectivity

The original project scope included IoT functionality to transmit motor speed data to a remote dashboard for monitoring and analysis. However, this feature could not be implemented due to memory constraints on the Raspberry Pi Pico W.

Technical Challenges:

  • The RP2040 microcontroller provides 264 KB of SRAM
  • TensorFlow Lite Micro inference engine requires significant memory allocation
  • MPU6050 sensor data buffering and processing consume additional RAM
  • Display driver and OLED framebuffer occupy ~1 KB
  • Adding WiFi stack (lwIP) and HTTP/MQTT client libraries would exceed available memory

Attempted Solutions:

  • Model quantization and optimization (already implemented)
  • Reducing tensor arena size (limited by inference requirements)
  • Minimizing sensor buffer sizes (constrained by accuracy needs)

Future Improvements:

  • Implement periodic data logging to onboard flash storage
  • Use a separate communication module for IoT connectivity
  • Consider edge computing architecture with local gateway

Despite this limitation, the core functionality of real-time motor classification and local display remains fully operational and demonstrates the viability of TinyML for industrial monitoring applications.

Data Source

The training data was collected from accelerometer measurements of a motor operating at four distinct speed levels. Each CSV file contains six-axis sensor readings (accelerometer X, Y, Z and gyroscope X, Y, Z) from the MPU6050 sensor, captured at different motor speeds representing operational states from idle to maximum speed.

Data collection methodology:

  • Sensor: MPU6050 6-axis IMU
  • Sampling rate: Consistent across all speed levels
  • Features: Accelerometer (accel_x, accel_y, accel_z) and Gyroscope (gyro_x, gyro_y, gyro_z)
  • Labels: 4 classes (nivel 0, nivel 1, nivel 2, nivel 3)

Dataset was collected using an SD card module with MPU6050 sensor on Raspberry Pi Pico. Full implementation available at: Datalogger_Acelerometro

Author

Jonas Souza

Electrical Engineering

GitHub

License

This project is licensed under the MIT License.

About

Real-time motor speed classification using TinyML on Raspberry Pi Pico W. MLP neural network trained with TensorFlow deployed on embedded hardware (5.3 KB model). Classifies motor vibration into 4 speed levels using MPU6050 accelerometer with live OLED display feedback. Complete ML workflow from data collection to edge deployment.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published