← Back to Basic Projects

πŸ”’ Rotary Encoder Counter

Precisely count clockwise and counter-clockwise rotations β€” the foundation of all robot wheel odometry and CNC axis positioning.

πŸ“‹ Overview

A rotary encoder converts physical rotation into digital pulses. Unlike a potentiometer (which has a fixed range), an encoder has unlimited range and can count both direction and precise steps β€” making it ideal for motor position feedback in robotics.

Technical Insight: A mechanical rotary encoder generates two phase-shifted square waves (channels A and B). When turning clockwise, A leads B by 90Β°. Counter-clockwise, B leads A. By detecting the state of B when A makes a falling edge, we determine direction with a single interrupt service routine β€” no polling needed.

In simple terms: The encoder clicks like a gear. Each click fires an interrupt. By checking which signal came first, we add or subtract one from the count β€” like counting heartbeats with an ECG.

What you'll learn: Hardware interrupt usage (attachInterrupt), quadrature decoding logic, volatile variables for ISR safety, 7-segment display control, and encoder applications in robotics.

Estimated time: 40-55 minutes. Difficulty: ⭐⭐⭐ Intermediate β€” introduces interrupt programming.

🧩 Components Needed

ComponentSpecificationQtyNotes
Arduino Uno R35V1
Rotary EncoderKY-040 module120 detents per revolution
7-Segment DisplayCommon Cathode, 1-digit1Shows count 0–9
Resistors220Ξ©7One per segment
Breadboard + WiresFull-size1

πŸ—ΊοΈ Component Pin Mapping

Rotary Encoder Counter Component Pin Mapping Infographic

πŸ“– Step-by-Step Tutorial

1

Wire the Encoder

KY-040: CLK β†’ Arduino pin 2 (INT0), DT β†’ pin 3 (INT1), SW β†’ pin 4, VCC β†’ 5V, GND β†’ GND.
2

Wire the 7-Segment

Segments a–g β†’ pins 5–11 through 220Ξ© resistors. Common cathode β†’ GND.
3

Upload Code

Upload. Turn the encoder knob β€” count increments or decrements on the 7-segment display.
4

Push Button Reset

Press the encoder's built-in push button (SW pin) to reset count to zero.
5

Scale to Angle

With 20 detents/revolution, each click = 18Β°. Multiply count by 18 to display angle in degrees.
πŸ’‘
Mechanical encoders can 'bounce' and generate false pulses. The code uses a simple software filter (minimum pulse width check) to reject bounces. For high-speed applications, use a hardware RC filter (10kΞ© + 100nF) on each encoder pin.

πŸ’» Arduino Code

rotary_encoder_counter.ino
INO
// Rotary Encoder Counter β€” Volt X
// KY-040 on pins 2(CLK) & 3(DT), 7-seg on pins 5-11

const int CLK = 2, DT = 3, SW = 4;
volatile int count = 0;
int lastCLK;

// Segment patterns for digits 0-9 (a,b,c,d,e,f,g)
const byte SEG_PINS[7] = {5,6,7,8,9,10,11};
const byte DIGITS[10][7] = {
  {1,1,1,1,1,1,0}, // 0
  {0,1,1,0,0,0,0}, // 1
  {1,1,0,1,1,0,1}, // 2
  {1,1,1,1,0,0,1}, // 3
  {0,1,1,0,0,1,1}, // 4
  {1,0,1,1,0,1,1}, // 5
  {1,0,1,1,1,1,1}, // 6
  {1,1,1,0,0,0,0}, // 7
  {1,1,1,1,1,1,1}, // 8
  {1,1,1,1,0,1,1}, // 9
};

void showDigit(int n) {
  n = abs(n) % 10;
  for (int i = 0; i < 7; i++) digitalWrite(SEG_PINS[i], DIGITS[n][i]);

⭐Reviews & Ratings

β€”0 reviews
5β˜…
0
4β˜…
0
3β˜…
0
2β˜…
0
1β˜…
0
...

Loading reviews...

volt-X / Rotary Encoder Counter β€” Arduino Tutorial