Rotary Encoder Position Counter

On an Arduino Nano 33 BLE, decode a mechanical quadrature handwheel so that each full click counts once in the right direction, while contact chatter, rocking and invalid transitions never count, and a push button zeroes the position.

Board
Nano 33 BLE · Arduino
Difficulty
Medium
Topic
Data Handling & Algorithms
Access
Full plan

The task

Rotary Encoder Position Counter

A small CNC router gets a manual handwheel for jogging the spindle. The wheel is a mechanical quadrature encoder with clicks (detents). Mechanical contacts bounce and operators rock the wheel back and forth, but a wrong step here moves a cutting tool, so every click must count exactly once, in the right direction, and nothing else may count.

Hardware

  • Arduino Nano 33 BLE (nRF52840, 3.3 V logic). Serial console on Serial1 (D0/D1), 115200 baud. Serial is USB and is not seen by the tests. The motion controller reads the console.
  • Encoder channels A on D2 and B on D3, common pin to GND. Use the internal pull-ups. At a click (rest position) both channels are HIGH.
  • Turning one click clockwise, A goes LOW first, then B goes LOW, then A returns HIGH, then B returns HIGH. Counter-clockwise is the same with B leading.
  • D5: the wheel's push button (zero), to GND, LOW = pressed.

Write the sketch in sketch/sketch.ino. In setup() open Serial1 at 115200 baud, start at position 0, then print READY. Never block with delay(): a loop that reads the pins all the time is fast enough.

This is the opening of the task. Sign in with the full plan to read the rest and solve it.

What you get

  • Your firmware is built and run on an emulated board and checked by hardware-level tests.
  • A written walkthrough of the solution.
  • No development board or local toolchain needed.

New to embedded? Start with the roadmap: How to learn embedded systems