Machine learning on a microcontroller lesson plan
Gesture recognition from scratch in plain Python on a Raspberry Pi: features, k-nearest neighbors, decision trees, a neuron, gradient descent and a tiny neural network, and weights exported as C arrays for a microcontroller.
Objectives and materials
By the end, students can
- Turn raw sensor traces into features and split them honestly.
- Decision trees and a single neuron, and where each one breaks.
- Gradient descent on the Pi, then weights a microcontroller can run.
- Live gesture recognition on the traffic lights.
Materials for the whole unit
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Data first
Turn raw sensor traces into features and split them honestly.
| Lesson | Time | What students do |
|---|---|---|
| Data and featuresGestures from a wrist accelerometer: raw traces, six features that summarize half a second in six numbers, class averages that show why it can work, an honest train/test split, and z-scores computed from the training set only. | 45 min |
|
| k-nearest neighborsThe simplest model there is: label a gesture like its nearest training gestures. Distance, votes, accuracy, choosing k, a confusion matrix, why the z-scores matter, and what k-NN costs a microcontroller. | 45 min |
|
Materials
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Check for understanding
Each student finishes "k-nearest neighbors" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Scales and size".
Models you can read
Decision trees and a single neuron, and where each one breaks.
| Lesson | Time | What students do |
|---|---|---|
| Decision treesA model you can read: the best first question by Gini impurity, a depth-2 tree printed as rules, overfitting made visible with three mislabeled gestures, and the tree turned into C if statements. | 35 min |
|
| A neuronOne artificial neuron: a weighted sum, a bias and a step. Set its weights by hand, then let the perceptron rule learn them, find where a single neuron gives up (a problem that is not linearly separable with one feature), and fire a lamp from it. | 35 min |
|
Materials
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Check for understanding
Each student finishes "A neuron" on their own, without opening the hints. The simulator checks 4 of its 4 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Fire a lamp".
Train and ship
Gradient descent on the Pi, then weights a microcontroller can run.
| Lesson | Time | What students do |
|---|---|---|
| Training on the PiGradient descent from scratch: softmax regression for all three gestures, the cross-entropy loss falling epoch by epoch, test accuracy and confidence, learning rates too small and too big, and a 6-4-3 neural network trained by backpropagation. | 45 min |
|
| Weights in CShip the model: print the trained weights and the z-score constants as C arrays a microcontroller sketch can paste in, quantize them to int8, prove the small model still agrees, and print the C classify function that uses them. | 35 min |
|
Materials
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Check for understanding
Each student finishes "Weights in C" on their own, without opening the hints. The simulator checks 4 of its 4 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "The C function".
Boss
Live gesture recognition on the traffic lights.
| Lesson | Time | What students do |
|---|---|---|
| Boss: gesture recognitionThe boss: the whole pipeline live. Press the button, a fresh gesture is recorded, featurized, z-scored and classified, and the traffic lights show wave, punch or circle; a confidence threshold says "not sure" to a gesture it was never taught; and 30 never-seen gestures measure it honestly. | 55 min |
|
Materials
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Check for understanding
Each student finishes "Boss: gesture recognition" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "What you built".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.