Lessons and the learning game

Build a bike computer

Speed, distance and cadence from two magnets, automatic lights, a screen and a ride log on microSD, all measured from the bike itself.

What you build

A bike computer like the ones on handlebars everywhere. A magnet on a spoke and another on a crank arm sweep past Hall switches; an Uno times them on interrupts and turns the times into km/h, meters and pedal turns a minute. It switches the headlight on at dusk, shows everything on an OLED and logs every ride to a card.

Each Hall switch has a magnet slider: drag it close and away again for one turn.

How it works

Interrupts. attachInterrupt() runs a short function the instant a sensor's pin falls, so no turn is missed while the screen or the card is busy. The function only notes the time and counts, and ignores anything within 50 ms of the last turn as a bounce.

Speed from a period. One turn is one circumference, 2105 mm for a 700x25c road tire. Speed is the circumference over the time for one turn: half a second a turn is 15.1 km/h. The sums are done in whole numbers, in tenths of a km/h.

Distance from a count. Turns times 2105 mm. Multiply first, divide last.

Cadence and gears. The crank sensor gives pedal turns a minute. Divide the crank's period by the wheel's and you get wheel turns per pedal turn, which is the gear.

Lights with hysteresis. On above one light level and off below another, so it never flickers at the edge of a streetlight.

A ride log. The first turn starts a ride and one CSV line a second goes to RIDE.CSV. Ten still seconds end it with a summary. Without a card it rides on.

The course

Build a bike computer has six lessons, all on the same wiring:

  1. Wheel magnet: a Hall switch, an interrupt and bounces.
  2. Speed and distance: a tire's circumference and whole-number math.
  3. Cadence: a second sensor, gears and coasting.
  4. Lights: on at dusk, off in daylight.
  5. Display: speed, distance and cadence on an OLED.
  6. Project: a ride log: every ride to microSD, with a summary.

Every step is checked against the running circuit, apart from the last, where you make the computer your own and share it.