Lesson plan

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.

4 chapters, 7 lessons, about 5 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

The unit

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.

Chapter 1. About 1 h 30 min

Data first

Turn raw sensor traces into features and split them honestly.

LessonTimeWhat 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
  1. What the sensor sees
  2. Six numbers instead of 75
  3. Look at the classes
  4. Hold some back
  5. Z-scores
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
  1. Ask the neighbors
  2. Accuracy
  3. Choosing k
  4. Which mistakes?
  5. Scales and size

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".

Chapter 2. About 1 h 10 min

Models you can read

Decision trees and a single neuron, and where each one breaks.

LessonTimeWhat 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
  1. The best first question
  2. Grow a tree
  3. Overfitting, made visible
  4. A tree is just if statements
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
  1. A weighted sum and a step
  2. Let it learn: the perceptron
  3. Where one neuron gives up
  4. Fire a lamp

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".

Chapter 3. About 1 h 20 min

Train and ship

Gradient descent on the Pi, then weights a microcontroller can run.

LessonTimeWhat 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
  1. Three scores and softmax
  2. Gradient descent
  3. Test it, with confidence
  4. The learning rate
  5. A tiny neural network
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
  1. The model as C arrays
  2. Shrink to int8
  3. Prove the small model
  4. The C function

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".

Chapter 4. About 55 min

Boss

Live gesture recognition on the traffic lights.

LessonTimeWhat 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
  1. Train on boot
  2. Make a gesture
  3. Three gestures
  4. Say "not sure"
  5. Measure it honestly
  6. What you built

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".

Your class

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.