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...