Arduino Wheel Encoder Pulse Counting with Interrupts and L298N
Level 53 · Count the Turns · 30 minutes
Press a button and a wheel turns exactly five times, at any speed: count the pulses from a slotted disc with an interrupt.

Preview the project free; a course seat is required to run and submit. No hardware kit needed.
Your project challenge
Press GO and the wheel turns exactly 5 turns, then stops. Wire it up: - L298N: ENA D5, IN1 D7, IN2 D8; 6 V battery on VS - Speed sensor: VCC 5V, GND GND, D0 D2 - Knob: A0. Button: D4 The knob sets the speed, analogWrite 120 to 255. Count the pulses: 20 a turn.
Components you’ll use
- Arduino Uno
- TT Motor with Speed Sensor (LM393)
- L298N Motor Driver
- Battery
- Potentiometer
- Pushbutton
What you’ll build
A wheel that turns an exact number of times. Press GO and it turns 5 times, then stops, at whatever speed the knob sets.
How it works
- A disc with 20 slots sits on the motor’s shaft. The speed sensor shines light through it.
- Each time a bar blocks the light, D0 goes HIGH. That is 20 pulses a turn, so 5 turns are 100 pulses.
- The knob sets the speed: analogWrite 120 to 255 on ENA.
The key idea
Count the pulses with an interrupt. It runs your function the moment D0 rises, even while loop() is busy.
attachInterrupt(digitalPinToInterrupt(2), countPulse, RISING);
Stop when the count reaches 100. The wheel coasts on a little, so it may pass 101 or 102.
⚠ Watch out
Stopping after a fixed time only works at one speed. Turn the knob and the wheel goes too far or not far enough. Checking D0 in loop() with a delay misses pulses. A real sensor can count one slot twice, so builders add a 0.1 µF capacitor from D0 to GND.
The interactive editor includes the wiring guide, code editor and simulation controls.