Lessons and the learning game

Machine learning on a microcontroller

Build gesture recognition from scratch in plain Python on a Raspberry Pi 4, then export the trained model as C arrays for a microcontroller.

The course

Machine learning on a microcontroller is an expert Coding course in seven lessons. The task is a wristband that tells a wave, a punch and a circle apart from an accelerometer. There is no numpy and no library: every model is written in plain Python, so you see every number. The circuit is the gallery's traffic light build, one lamp per gesture and a button to make one. Every step is checked against what the Pi prints or what its lamps do, and every number in the lessons is one the program really prints.

  1. Data and features: raw (ax, ay, az) traces, six features (mean and standard deviation per axis), class averages that show where the mistakes will be, a shuffled 66/24 train and test split, and z-scores from the training set only.
  2. k-nearest neighbors: distance and votes, 21 of 24 at k = 3, choosing k without peeking at the test set, a confusion matrix, why units wreck an unscaled model, and why storing the whole training set is costly on a chip.
  3. Decision trees: the best first question by Gini impurity, a depth-2 tree printed as rules (22 of 24), overfitting made visible with three mislabeled gestures, and the tree printed as C if statements.
  4. A neuron: a weighted sum and a step, weights set by hand, the perceptron rule learning them, linear separability and where one neuron gives up, and a lamp fired by its output.
  5. Training on the Pi: softmax and cross-entropy, gradient descent with the loss falling epoch by epoch, learning rates too small and too big, and a 6-4-3 neural network trained by backpropagation (43 parameters).
  6. Weights in C: the z-score constants and weights printed as C arrays, int8 quantization with one scale, a check that the int8 model agrees with the float model on every test gesture, and the C classify function.
  7. Boss: gesture recognition: the finale.

The finale

The boss trains on boot, then classifies a fresh gesture on every press of the button and lights green for a wave, red for a punch and amber for a circle. It shows a confident mistake (a circle called a wave at 92 percent), adds a confidence threshold that answers "not sure" to a jiggle it was never taught, and measures itself honestly on 30 gestures from seeds no part of training ever used: 28 of 30.

What is different here

  • The sensor. The catalog kit is an ESP32 with an MPU6050. The Pi gallery has no accelerometer circuit, so the course reads its gestures from gestures.py, a short, clearly labeled model of a wrist that makes MPU6050-shaped traces with per-person size, speed, wrist tilt and sensor noise. The algorithms and the exported C are exactly what the real wristband would use; the course says so where it matters.
  • The engine. The Pi runs MicroPython 1.29: no numpy, so vectors are lists; random has one generator, so random.seed makes every run the same; dicts do not keep order, so classes are list indexes.
  • The button bounces like a real one, so the boss uses gpiozero's bounce_time.
  • No C compiler on the Pi: the C arrays and function are printed for you to paste into a sketch, and the course is honest that it stops there.