A real-time anomaly detection system for Raspberry Pi that monitors sensor data and uses machine learning to detect abnormal behavior patterns. The system uses LED indicators to provide visual feedback for different states: normal operation, off state, and anomaly detection.
This project implements a machine learning-based anomaly detection system that:
- Reads analog sensor data using ADC (Analog-to-Digital Converter)
- Extracts features from sensor signals including statistical and frequency domain features
- Uses trained machine learning models to classify states and detect anomalies
- Provides real-time visual feedback through colored LEDs
- Supports data collection for training new models
- Raspberry Pi (tested with models that support GPIO)
- ADC device (PCF8591 or ADS7830)
- 3 LEDs (Red, Green, Blue) with appropriate resistors
- Analog sensor connected to ADC channel 7
- Freenove Kit components (recommended)
Based on the setup images in the repository:
- Blue LED: GPIO 17 (Off state indicator)
- Green LED: GPIO 27 (Normal operation indicator)
- Red LED: GPIO 26 (Anomaly detected indicator)
- ADC: Connected via I2C (addresses 0x48 for PCF8591 or 0x4b for ADS7830)
- Sensor: Connected to ADC channel 7

-
Clone this repository to your Raspberry Pi:
git clone <repository-url> cd raspberrypi-anomalies-detection-test
-
Create and activate a virtual environment:
python3 -m venv env source env/bin/activate -
Install the required dependencies:
pip install -r requirements.txt
Before running anomaly detection, you need to collect training data:
-
Collect "off" state data:
python pi_loop_sample.py
Edit the
statevariable in the script to"off"and run when your system is in the off state. -
Collect "on" state data:
python pi_loop_sample.py
Edit the
statevariable in the script to"on"and run when your system is operating normally.
Train the machine learning models using the collected data:
jupyter notebook train_AD.ipynbOr run all cells in the notebook to:
- Load and preprocess the collected data
- Train a K-Nearest Neighbors classifier for state classification
- Train a Local Outlier Factor model for anomaly detection
- Save the trained models and scaler to pickle files
Run the main anomaly detection system:
python pi_loop.pyThe system will:
- Continuously read sensor data
- Process the data in real-time
- Display predictions and LED status
- Show anomaly alerts when detected
Enanchements:
- reduce lag
- better anomalies detection
The system extracts features from a sliding window of 10 sensor readings:
- Statistical features: Mean and standard deviation
- Frequency domain features: Fourier transform coefficients (real and imaginary parts)
- K-Nearest Neighbors Classifier: Distinguishes between "on" and "off" states
- Local Outlier Factor: Detects anomalies by identifying outliers in the feature space
- Standard Scaler: Normalizes features for consistent model performance
- 🔴 Red LED: Anomaly detected
- 🟢 Green LED: Normal operation (on state)
- 🔵 Blue LED: Off state
├── pi_loop.py # Main anomaly detection script
├── pi_loop_sample.py # Data collection script
├── train_AD.ipynb # Model training notebook
├── requirements.txt # Python dependencies
├── on.csv # Training data for "on" state
├── off.csv # Training data for "off" state
├── knn_model.pkl # Trained KNN classifier
├── clf_model.pkl # Trained state classifier
├── scaler.pkl # Fitted data scaler
├── set_up1.jpeg # Hardware setup images
├── set_up2.jpeg
├── set_up3.jpeg
└── freenove_components.jpg # Component reference
You can adjust the following parameters in train_AD.ipynb:
- LocalOutlierFactor contamination: Currently set to 0.01 (1% of data expected to be anomalies)
- KNN neighbors: Set to 5 for classification, 20 for anomaly detection
- Buffer size: 10 samples for feature extraction window
The system samples at 10Hz (0.1-second intervals). Modify the time.sleep(0.1) in pi_loop.py to change the sampling rate.
# Check I2C devices
i2cdetect -y 1
# Enable I2C if not detected
sudo raspi-config
# Navigate to Interface Options > I2C > Enable# Add user to gpio group
sudo usermod -a -G gpio $USER
# Logout and login again# Update system packages
sudo apt update
sudo apt install python3-pip python3-venv i2c-tools
# Reinstall requirements
pip install -r requirements.txtThis project is open source. Please refer to the license file for details.
- Freenove Complete Starter Kit for Raspberry Pi
- scikit-learn for machine learning algorithms
- gpiozero for GPIO control


