TheSolderBenchnotes from my electronics bench

Log #112 · Nov 16, 2026

Arduino Bike Speedometer with a Hall Sensor and OLED

Build a bicycle speedometer and odometer with an A3144 Hall sensor, a spoke magnet and a 0.96 inch OLED. Measuring the wheel circumference, interrupt-based timing, speed, distance and max speed, and mounting it on a real bike.

Projects · 5 min read · Intermediate

Every bike computer works the same way: a magnet on a spoke passes a sensor on the fork once per wheel turn, and a small computer times the pulses. This project builds one with an Arduino Nano, an A3144 Hall effect sensor and a 0.96" OLED screen. It shows current speed, trip distance and maximum speed, and it teaches two techniques that are useful far beyond bikes: timing pulses with interrupts and turning rotations into distance.

A bicycle wheel with a magnet clipped to a spoke passing an A3144 Hall sensor zip-tied to the fork; the sensor's output goes to Arduino pin 2. An OLED screen shows 23.4 kilometers per hour in large digits with trip distance and maximum speed below
One pulse per wheel turn: speed comes from the time between pulses, distance from counting them.

Parts

Part Notes
Arduino Nano Small enough for a handlebar box
A3144 Hall sensor (or a KY-003 module) Digital, open-collector output
Neodymium magnet A spoke magnet from an old bike computer is perfect
0.96" SSD1306 OLED, I²C 128 × 64 pixels
Battery e.g. a protected 18650 with a charger/boost module
3-core cable, about 1 m From the fork to the handlebar
Small box, zip ties To mount everything

How it works

  • Each time the magnet passes the sensor, the sensor's output drops LOW: one pulse per wheel turn.
  • Distance = number of pulses × wheel circumference.
  • Speed = wheel circumference ÷ time between the last two pulses.

Using the time between pulses gives a fresh speed reading every wheel turn (about 3 times per second at 25 km/h), much faster than counting pulses over a fixed interval. How the A3144 detects the magnet is explained in Hall effect sensors explained.

Measure your wheel's circumference

This number decides your accuracy, so measure it instead of trusting a chart:

  1. Turn the wheel so the valve is at the bottom, and mark the floor at that point.
  2. Sitting on the bike (your weight compresses the tire), roll forward exactly one wheel turn until the valve is at the bottom again.
  3. Mark the floor and measure the distance in millimeters.

Typical values are about 2100 mm for a 700×25c road wheel and about 2050–2100 mm for a 26" mountain bike wheel, but your tire and pressure change it. A 2 % error in circumference is a 2 % error in every speed and distance.

Wiring

From To
A3144 VCC (left pin, flat face toward you) 5V
A3144 GND (middle) GND
A3144 OUT (right) D2
OLED VCC / GND 5V / GND
OLED SDA / SCL A4 / A5

The sensor's output is open-collector; the code enables the internal pull-up. Over a 1 m cable, add a 10 kΩ external pull-up from OUT to 5V and a 100 nF capacitor from OUT to GND at the Arduino end to keep noise out.

The code

Install Adafruit SSD1306 and Adafruit GFX from the Library Manager.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 display(128, 64, &Wire, -1);

const int HALL = 2;
const float WHEEL_MM = 2105.0;              // your measured circumference
const unsigned long MIN_GAP_US = 80000UL;   // ignore pulses closer than 80 ms
const unsigned long STOP_US = 3000000UL;    // no pulse for 3 s = stopped

volatile unsigned long lastPulseUs = 0;
volatile unsigned long periodUs = 0;
volatile unsigned long pulses = 0;

float maxKmh = 0;
unsigned long lastDraw = 0;

void onMagnet() {
  unsigned long now = micros();
  unsigned long gap = now - lastPulseUs;
  if (gap < MIN_GAP_US) return;             // double trigger: ignore
  periodUs = gap;
  lastPulseUs = now;
  pulses++;
}

void setup() {
  pinMode(HALL, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(HALL), onMagnet, FALLING);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  if (millis() - lastDraw < 250) return;    // update the screen 4 times a second
  lastDraw = millis();

  noInterrupts();                           // copy the shared values safely
  unsigned long p = periodUs;
  unsigned long last = lastPulseUs;
  unsigned long n = pulses;
  interrupts();

  float kmh = 0;
  if (n >= 2 && micros() - last < STOP_US)
    kmh = (WHEEL_MM / 1000.0) / (p / 1000000.0) * 3.6;
  float km = n * WHEEL_MM / 1000000.0;
  if (kmh > maxKmh) maxKmh = kmh;

  display.clearDisplay();
  display.setTextSize(3);
  display.setCursor(0, 4);
  display.print(kmh, 1);
  display.setTextSize(1);
  display.setCursor(98, 18);
  display.print("km/h");
  display.setCursor(0, 42);
  display.print("Trip ");
  display.print(km, 2);
  display.print(" km");
  display.setCursor(0, 54);
  display.print("Max  ");
  display.print(maxKmh, 1);
  display.print(" km/h");
  display.display();
}

How the code works

  • The interrupt runs the instant the sensor's output falls, so no pulse is missed even while the screen is being drawn. It records the time between pulses with micros() and counts them. Interrupts are explained in Arduino interrupts.
  • The 80 ms filter ignores a second trigger from the same magnet pass. At 80 ms per turn, a 2.1 m wheel would be going 95 km/h, so real pulses are never closer than that.
  • Copying with interrupts off: the four-byte variables can change halfway through being read; turning interrupts off for a few microseconds prevents a corrupted value.
  • Speed formula: circumference in meters ÷ period in seconds = meters per second; × 3.6 = km/h.
  • Stopped detection: if no pulse has arrived for 3 seconds, the speed shows 0 instead of the last speed forever. Speed is only calculated after two pulses, since the first one has no previous pulse to measure from.
  • micros() overflow every 70 minutes doesn't matter, because unsigned subtraction gives the right gap even across the rollover.

Mounting on the bike

  1. Magnet on a front-wheel spoke, about halfway between hub and rim.
  2. Sensor zip-tied to the fork, flat face toward the magnet, with a gap of 3–8 mm as the magnet passes. The A3144 only reacts to one pole: if nothing happens, flip the magnet.
  3. Run the cable up the fork and along the brake cable to the handlebar, with a loop so the steering can turn freely.
  4. Put the Arduino, screen and battery in a box on the handlebar, with the OLED behind a clear window.

Test before riding: spin the wheel by hand and watch the speed.

Power

The Nano and OLED draw about 30–40 mA. A protected 18650 with a small charger/boost module runs it for days of riding; see 18650 batteries and battery charging modules compared. A USB power bank also works if it doesn't switch itself off at such a small load.

Improvements

  • Save the total distance to EEPROM every kilometer, so it survives power-off.
  • Add a button to reset the trip and max speed, with debouncing.
  • Average speed: total distance ÷ total time moving (count only time with speed above 0).
  • Cadence sensor: a second Hall sensor and a magnet on the crank, on interrupt pin 3, gives pedaling RPM.

Troubleshooting

  • Speed always 0: no pulses. Check the magnet pole and gap, and test the sensor with an LED sketch first.
  • Speed jumps to double: the magnet triggers twice per pass. Move it slightly or increase MIN_GAP_US.
  • Speed reads a few percent wrong: re-measure the circumference while sitting on the bike.
  • Random pulses when riding over bumps: noise on the long cable. Add the external pull-up and capacitor.

— TheSolderBench · found a mistake? tell me

More pages from this notebook

Build an Obstacle-Avoiding Robot

A complete build with parts list, wiring table and full Arduino code for a two-wheel robot that scans with an ultrasonic sensor and steers around obstacles.

→ misses chair legs, raise STOP_CM

Automatic Plant Watering System

Build a soil-moisture-controlled watering system with a capacitive sensor, a relay or MOSFET, a small pump and Arduino code with safety limits.

→ limit waterings per day!

Digital Thermometer with an OLED Display

Build a waterproof digital thermometer with a DS18B20 probe and a 0.96 inch SSD1306 OLED, showing live temperature plus the minimum and maximum.

→ blank screen? try address 0x3D

Build a Reaction-Time Game

A two-button Arduino game that waits a random time, lights an LED and measures who reacts first in milliseconds, with anti-cheat and a buzzer.

→ use millis(), and catch early presses