The course
Vectors, trig and matrices for code is an intermediate Coding course in seven lessons. Every lesson runs on one circuit: an ESP32-C3, a 2.4 inch ILI9341 screen on GPIO 18 to 21, and an LIS3MDL magnetometer on I2C (SDA 4, SCL 5). Each lesson opens its own sketch, and steps are checked against what the board prints.
- Vectors: a capsule drifting to a docking port. Add and scale for motion, length and normalize to split speed from direction, and the dot product for ahead or behind.
- Angles and atan2: four beacons on a radar. Radians, why atan(y / x) gets half of them wrong, compass bearings by swapping the arguments, wrapping to 360 and the shortest turn.
- Rotation: a ship outline turned by sine and cosine, why it turns clockwise on a screen, float drift over 3600 small turns, and the classic bug of turning about the screen's corner instead of a pivot.
- Matrices: six numbers that move, turn and grow at once. Why order matters, the determinant as an area, and an inverse that carries a screen tap back into the ship's own coordinates.
- Interpolation: lerp, a glide in frames, smoothstep easing, and turning a needle the short way around.
- Noise and smoothing: the compass heading with noise added. Averaging angles points south at north; averaging vectors does not. Then an exponential filter and its trade of lag against calm.
- Project: a compass needle on a screen.
The project
A ship's compass: the LIS3MDL's field vector through atan2, an exponential filter on the vector, a needle drawn through one rotation matrix about the dial's center, the short way around, big digits and a cardinal letter, and declination for true north. The steps turn the ship with the magnetometer's heading slider and end with a feature of your own.
The magnetometer
The LIS3MDL is the real chip's register model: it reports the Earth's field
in counts along x and y, and its heading, pitch and roll sliders turn it.
The sketches use the Adafruit library calls (mag.begin_I2C(),
mag.read()). The simulated field has no noise, so lesson 6 adds its own,
which also lets the sketch measure each filter against the true heading.
How the C3 engine differs
The board runs Mokxi's own firmware runtime, not the Arduino core, with
sinf, cosf, atan2f, sqrtf and fmodf from its own math library.
There is no floating point unit, so floats run in software; a frame of a few
hundred trig calls is no trouble. Every run is deterministic, so every
number in the steps can be checked.
Where next
- Charts on small screens: the same screen, with a live sensor dashboard.
- Game dev on microcontrollers: vectors and collisions in a space platformer.