A complete TinyML training and testing pipeline for slip detection using piezoelectric sensor data. The model uses a Decision Tree classifier optimized to run on Arduino Uno.
- Decision Tree Model: Lightweight and interpretable model perfect for embedded systems
- Arduino Uno Compatible: Generated C++ code runs directly on Arduino Uno (no ML framework needed)
- Complete Pipeline: Training, testing, and deployment code included
- Memory Efficient: Optimized for Arduino Uno's 2KB SRAM and 32KB Flash constraints
.
├── train_model.py # Main training script
├── test_model.py # Model testing script
├── generate_sample_data.py # Generate synthetic training data
├── arduino_slip_detection.ino # Arduino inference code
├── arduino_model.h # Generated Arduino model (created after training)
├── requirements.txt # Python dependencies
└── README.md # This file
- Install Python dependencies:
pip install -r requirements.txt- Install Arduino IDE (if deploying to hardware):
- Download from https://www.arduino.cc/en/software
If you don't have training data yet, generate synthetic data:
python generate_sample_data.py --n-samples 1000 --output data/training_data.csvNote: Replace with your actual piezoelectric sensor data. Expected CSV format:
- Columns:
feature_0,feature_1, ...,feature_N,label label: 0 = no slip, 1 = slip detected
Train the decision tree model:
python train_model.py --data data/training_data.csv --max-depth 5Parameters:
--data: Path to training data CSV--max-depth: Maximum tree depth (lower = smaller model, default: 5)--min-samples-split: Minimum samples to split node (default: 10)--min-samples-leaf: Minimum samples in leaf (default: 5)--output-model: Output path for saved model (default:model.pkl)--output-arduino: Output path for Arduino header (default:arduino_model.h)
The script will:
- Train the model
- Evaluate performance
- Save the model (
model.pkl) - Generate Arduino C++ code (
arduino_model.h)
Test the trained model:
python test_model.py --model model.pkl --data data/test_data.csv-
Copy generated model:
- Copy
arduino_model.hto your Arduino sketch folder
- Copy
-
Open Arduino IDE:
- Open
arduino_slip_detection.ino
- Open
-
Hardware Setup:
- Connect piezoelectric sensor to analog pin A0
- Optional: Connect LED to pin 13 for visual slip indication
-
Upload:
- Select board: Tools → Board → Arduino Uno
- Select port: Tools → Port → [Your Arduino Port]
- Click Upload
-
Monitor:
- Open Serial Monitor (9600 baud)
- View real-time predictions
The decision tree model uses the following features (extracted from sensor data):
Spectral Features:
- Feature 0: Peak amplitude
- Feature 1: RMS value
- Feature 2: Frequency domain energy
- Feature 3: Signal variance
- Feature 4: Zero crossing rate
Temporal Features:
- Feature 5: Signal range (max - min)
- Feature 6: Mean Absolute Value (MAV)
- Feature 7: Short-window slope / envelope change
- Real-time feature extraction from sensor readings
- Efficient decision tree traversal (no dynamic memory allocation)
- Serial output for debugging
- LED indicator for slip detection
The generated Arduino code is optimized for Arduino Uno:
- Static arrays (no dynamic allocation)
- Fixed-size buffers
- Minimal floating-point operations
- Compact tree representation
For smaller models (more memory constrained):
python train_model.py --max-depth 3 --min-samples-split 20 --min-samples-leaf 10For more accurate models (if memory allows):
python train_model.py --max-depth 7 --min-samples-split 5 --min-samples-leaf 2Modify extractFeatures() in arduino_slip_detection.ino to match your sensor characteristics and feature extraction method.
Adjust normalization constants in extractFeatures() based on your piezoelectric sensor's output range.
Model too large for Arduino Uno:
- Reduce
--max-depthparameter - Increase
--min-samples-splitand--min-samples-leaf - Use fewer features
Poor accuracy:
- Collect more training data
- Ensure features are well-separated between slip/no-slip classes
- Try different tree depths
- Check feature normalization
Arduino compilation errors:
- Ensure
arduino_model.his in the same folder as.inofile - Check that model was generated successfully
- Verify Arduino IDE version compatibility
MIT License - feel free to use and modify for your projects.
Contributions welcome! Please feel free to submit issues or pull requests.