Same-day dispatch before 2 PM · Free delivery over ₹999 · GST invoices Get the appPrices incl. GST
Geekobot
Snap & SendNEWGeeko AIWhere's myOrder
Log in
Products
  • Projects
    • Component guidesHow parts work, with code you can run
    • Learning pathsFrom your first LED to your first robot
    • Workshops & eventsOnline sessions for makers and classes
    • ElectrolarkFree guided builds, simulator and certificates
    • Students & teachersStudent discount and classroom help
    • CommunityShare builds and ask other makers
    • CalculatorsOhm's law, LED resistor, 555 and 100+ more
    • Circuit builderWire parts and run the circuit, powered by Electrolark
    • Geeko AIAsk what to buy or how to wire it
    • Snap & SendPhotograph a part or list, we find it
    • Part finderAnswer a few questions, get the right part
    • Compatibility checkWill these two parts work together?
    • Compare partsSpecs side by side
    • Business & bulk ordersGST invoices, credit and volume prices
    • Request a quotationSend your list, get a quote
    • Classroom kitsPer-student kits and lab bundles
    • Gift cardsFor students, teams and events
  • Atal Tinkering Lab
BOM to cart
Geekobot

Components, kits and know-how for makers, students, engineers, labs and businesses across India.

Ask Geeko AIOrders, parts and projects, any timeEmail supportReply within 1 working dayGet the Geekobot appAdd it to your home screen, free

Follow us

Shop

  • Boards & SBCs
  • Sensors & Modules
  • IoT & Wireless
  • Drone & FPV
  • 3D Printing
  • Filaments & Resins
  • Motors & Drivers
  • Power & Batteries
  • Semiconductors
  • Passive
  • Displays & LEDs
  • Switches & Relays
  • Wires & Cables
  • Prototyping
  • Tools & Test
  • Robotics
  • STEM Kits
  • Atal Tinkering Lab
  • Mechanical Parts
  • Audio

Orders

  • Track your order
  • Help & support
  • Shipping and delivery
  • Refunds and cancellation
  • Contact us

Learn

  • Projects
  • Geeko AI project assistant
  • Learning paths
  • Guides
  • Calculators
  • Electrolark: free guided builds

Part tools

  • Part finder
  • Compare parts
  • Will it work with my board?

Institutions

  • Classroom kits
  • Atal Tinkering Lab
  • Students & teachers
  • Workshops & Events

Business

  • Business & education
  • Request a quotation
  • BOM bulk upload
  • Quick order by SKU

Company

  • About Geekobot
  • Community
  • Gift cards
  • Terms & conditions
  • Privacy policy
  • Product directory
  • Sitemap

© 2026 Geekobot, a trade name of OLatus Systems Private Limited. All rights reserved.

20, 1st Floor, Neeladri Complex, 10th Cross, Sampige Rd, 2nd Main, Malleshwaram, Bengaluru, Karnataka 560003 · GSTIN 29AACCO0462F1ZM

+91 91810 15607 · connect@geekobot.in · Monday to Saturday, 10 AM to 2 PM IST

UPI · CARDS · NETBANKING · GST INVOICES

ShopProjectsGeeko AIGuides
Compare 0 parts →
Log in

Shop by category

Shop all components
  • Boards & SBCs1,910 products
    • Alternative Single Board Computers
    • Arduino Ecosystem
    • FPGA & Logic Boards
    • Microcontroller & Wireless Boards
    View all
  • Sensors & Modules4,701 products
    • Biomedical & Heart Rate
    • Current, Voltage & Power
    • Force, Weight & Pressure
    • Gas, Weather & Environmental
    View all
  • IoT & Wireless2,314 products
    • RF Modules
    • RFID & NFC
    • Antennas & RF Accessories
    • Cameras & Smart Vision
    View all
  • Drone & FPV3,558 products
    • Flight Control & Navigation
    • FPV Video & Optics
    • Frames & Structural Parts
    • Motors & Propulsion
    View all
  • 3D Printing2,204 products
    • 3D Printers
    • 3D Scanners & Digitizers
    • Laser Engravers & Cutters
    • Material Management & Upgrades
    View all
  • Filaments & Resins1,472 products
    • Filaments
    • Photopolymer Resins
    View all
  • Motors & Drivers463 products
    • BLDC Motor Drivers
    • DC & Geared Motors
    • DC Motor Drivers
    • Linear Actuators & Solenoids
    View all
  • Power & Batteries557 products
    • Battery Holders & Connectors
    • Breadboard Power
    • Chargers & BMS
    • DC-DC Converters
    View all
  • Semiconductors295 products
    • Diodes & Rectifiers
    • Interface & RF Encoder ICs
    • Logic ICs (74xx)
    • Logic ICs (CD4000)
    View all
  • Passive67 products
    • Capacitors
    • Circuit Protection
    • Crystals & Oscillators
    • Inductors
    View all
  • Displays & LEDs572 products
    • 7-Segment & Dot Matrix
    • Addressable LEDs
    • Character LCDs
    • Graphic LCDs
    View all
  • Switches & Relays184 products
    • Keypads & Touch
    • Push Buttons & Tactile
    • Relay Modules
    • Relays
    View all
  • Wires & Cables233 products
    • Board-to-Wire Connectors
    • Crimp & Splice
    • Headers & Dupont
    • Heat Shrink & Sleeving
    View all
  • Prototyping101 products
    • Breadboards
    • Interface Modules
    • PCBs & Perfboards
    View all
  • Tools & Test717 products
    • Bench Power Supplies
    • Hand Tools
    • Measuring Tools
    • Meters & Testers
    View all
  • Robotics180 products
    • Humanoid & Legged Robots
    • Robot Chassis & Platforms
    • Robotic Arms & Grippers
    • Wheels & Casters
    View all
  • STEM Kits268 products
    • Arduino Learning Kits
    • Coding & IoT Kits
    • Early Coding (Ages 3-8)
    • Invention Kits
    View all
  • Atal Tinkering Lab196 products
    • Package 1: Electronics, Robotics, IoT and Sensors
    • Package 2: Rapid Prototyping Tools
    • Package 3: Mechanical, Electrical and Measurement Tools
    • Package 4: Power Supply, Accessories and Safety Equipment
    View all
  • Mechanical Parts212 products
    • Bearings
    • Brackets & Mounts
    • Couplers
    • Fasteners & Standoffs
    View all
  • Audio109 products
    • Amplifier Modules
    • Buzzers & Sirens
    • Speakers
    View all
  1. Home
  2. ›Guides
  3. ›Sensors
  4. ›KY-040 / EC11 Rotary Encoder Module

Component guide · Beginner · 3 min read

KY-040 / EC11 Rotary Encoder Module

Pinout, Wiring and Code

By Geekobot Learning Team · Updated 29 Oct 2026

I'm using
Wiring, code and the Run it panels follow your board.
On this page
Short answerWhat the KY-040 rotary encoder isKY-040 rotary encoder pinoutWiring the KY-040 rotary encoderExample codeSetup and tuningWhat you needAlternatives and when to choose themTry itCommon mistakesFAQsParts and next steps
Short answer

The KY-040 is a rotary encoder module with a push button: turning the knob produces two square-wave signals (CLK and DT) whose order shows direction, and pressing it closes the SW switch. It has 20 detents per turn and spins endlessly, so it is ideal for menus, volume and setting values. Read it with interrupts for reliable counting.

Key specs at a glance

Type
Incremental mechanical encoder (EC11 style)
Resolution
20 detents (pulses) per revolution on most modules
Outputs
CLK (A), DT (B) quadrature; SW push button
Supply
3.3 to 5 V
Rotation
Continuous, no end stops
Pull-ups
10 kΩ on CLK and DT (module dependent)

Models in this guide

These are alternatives. Pick the one that fits your project; you only need one.

  • Option 1M274 KY-040 Incremental Rotary Encoder Brick Module – 20 CPR, 5V DC₹46
  • Option 2EC11 Rotary Encoder Module with Knob Cap – 5-Pin Header₹93
  • Option 3Green Rotary Encoder Module – 20 CPR Incremental Encoder, PCB + Brass, 0-5V₹62
  • Option 4DFRobot Fermion EC11 Rotary Encoder Module Breakout – 360° Rotation, 3.3-5V₹389
01

What the KY-040 rotary encoder is

Inside the encoder, two contacts open and close in a fixed quadrature pattern as the shaft turns. If CLK changes before DT, the knob turned one way; if DT changes first, it turned the other way. Counting the changes gives position.

Unlike a potentiometer, it has no start or end, which makes it perfect for scrolling menus on an LCD or OLED.

02

KY-040 rotary encoder pinout

The pins on the KY-040 rotary encoder and what each one does.

PinFunction
CLKOutput A
DTOutput B
SWPush button, pulls LOW when pressed
+3.3 to 5 V
GNDGround
PinFunction
CLK
Output A
DT
Output B
SW
Push button, pulls LOW when pressed
+
3.3 to 5 V
GND
Ground
03

Wiring the KY-040 rotary encoder

Use interrupt pins for CLK and DT. On an ESP32 any GPIO can take an interrupt. Enable INPUT_PULLUP on SW because many modules have no pull-up on the button.

KY-040 rotary encoder pinArduino UnoESP32
CLKD2 (interrupt)GPIO 32
DTD3 (interrupt)GPIO 33
SWD4GPIO 25
+5V3V3
GNDGNDGND
CLK
Arduino Uno:
D2 (interrupt)
ESP32:
GPIO 32
DT
Arduino Uno:
D3 (interrupt)
ESP32:
GPIO 33
SW
Arduino Uno:
D4
ESP32:
GPIO 25
+
Arduino Uno:
5V
ESP32:
3V3
GND
Arduino Uno:
GND
ESP32:
GND
04

Example code

The lookup table ignores contact bounce automatically, because a bounce produces an invalid transition that adds 0. Some modules give 2 edges per click; change the divider if each click counts double.

Count turns and read the button · Arduino C++Verify & upload in Electrolark
const int CLK = 2, DT = 3, SW = 4;
volatile int pos = 0;
volatile uint8_t last = 0;

void readEncoder() {               // full quadrature decode
  static const int8_t table[] = {0,-1,1,0, 1,0,0,-1, -1,0,0,1, 0,1,-1,0};
  uint8_t now = (digitalRead(CLK) << 1) | digitalRead(DT);
  pos += table[(last << 2) | now];
  last = now;
}

void setup() {
  Serial.begin(9600);
  pinMode(CLK, INPUT_PULLUP); pinMode(DT, INPUT_PULLUP); pinMode(SW, INPUT_PULLUP);
  last = (digitalRead(CLK) << 1) | digitalRead(DT);
  attachInterrupt(digitalPinToInterrupt(CLK), readEncoder, CHANGE);
  attachInterrupt(digitalPinToInterrupt(DT), readEncoder, CHANGE);
}

void loop() {
  static int shown = 0;
  int detents = pos / 4;           // 4 edges per click on most modules
  if (detents != shown) { shown = detents; Serial.println(shown); }
  if (digitalRead(SW) == LOW) { pos = 0; delay(200); }
}
05

Setup and tuning

Bounce

mechanical contacts bounce. If counts are jittery, add 10 nF capacitors from CLK and DT to GND.

Acceleration

for long value ranges, step by 10 when the knob turns fast.

Menus

combine with an OLED or 16x2 LCD: turn to scroll, press to select.

Precise angles

for motors use an optical or magnetic encoder instead.

06

What you need

Shop this list5 of 6 found in our store · ₹729

  • KY-040 incremental rotary encoder module: the classic moduleM274 KY-040 Incremental Rotary Encoder Brick Module – 20 CPR, 5V DC₹46
  • EC11 rotary encoder module with knob cap: with knobEC11 Rotary Encoder Module with Knob Cap – 5-Pin Header₹93
  • Rotary encoder module 20 CPR: alternative boardGreen Rotary Encoder Module – 20 CPR Incremental Encoder, PCB + Brass, 0-5V₹62
  • DFRobot Fermion EC11 rotary encoder breakout: 3.3 to 5 V breakoutDFRobot Fermion EC11 Rotary Encoder Module Breakout – 360° Rotation, 3.3-5V₹389
  • Uno R3 development board: the controller used in the wiring tableFind this part
  • 0.96 inch OLED display 128x64: to show readings without a computer0.96" OLED Display 128x64₹139Closest match, check the specs
07

Alternatives and when to choose them

  • ✓Potentiometers: absolute position with end stops, simplest to read.
  • ✓Rotary encoders guide: encoder types and resolution.
  • ✓Motor encoders: for measuring motor speed.
08

Try it: Encoder resolution calculator

Change the values and the diagram and results update as you type.

09

Common mistakes to avoid

  • !Polling in a slow loop and missing steps.
  • !Forgetting INPUT_PULLUP on the SW pin.
  • !Counting every edge as one click, so each click moves 2 or 4 steps.
  • !Using it to measure a motor's precise angle.
10

Frequently asked questions

What is the difference between a rotary encoder and a potentiometer?

A potentiometer gives an absolute position over about 270 degrees; an encoder gives steps and direction and turns endlessly.

Why does my encoder skip or count backwards?

Contact bounce or polling too slowly. Use interrupts, the lookup-table method and small capacitors.

How many pulses per turn?

Most KY-040 modules have 20 detents per revolution.

Does it work with ESP32?

Yes, at 3.3 V on any GPIO with interrupts.

Do I need a library?

No, but libraries such as Encoder or ESP32Encoder simplify the code.

Can it read the knob position after power-off?

No. It only counts changes; save the value to EEPROM if you need it.

Parts for this guide

  • M274 KY-040 Incremental Rotary Encoder Brick Module – 20 CPR, 5V DC
    ₹46View
  • EC11 Rotary Encoder Module with Knob Cap – 5-Pin Header
    ₹93View
  • Green Rotary Encoder Module – 20 CPR Incremental Encoder, PCB + Brass, 0-5V
    ₹62View
  • DFRobot Fermion EC11 Rotary Encoder Module Breakout – 360° Rotation, 3.3-5V
    ₹389View
  • Uno R3 Development Board (No Cable)
    ₹359View
  • 0.96" OLED Display 128x64
    ₹139View
Shop Encoders & Rotary Position Sensors

Calculators

Encoder resolution calculator

Terms in this guide

Quadrature outputTwo square-wave signals 90 degrees apart (A and B). Which one changes first tells the direction of rotation.

Projects that use it

Put this into practice: 5 step-by-step builds in our project library use parts from this guide.

Intermediate · 1–2 hoursPrecision servo positionerView projectBeginner · 1 hourKitchen countdown timer with a rotary knobView projectIntermediate · 2 hoursRoom thermostat with a rotary knobView project
Browse all 5 projects

Keep learning

How-toBeginnerRotary EncodersA rotary encoder is a knob that turns endlessly and outputs two square-wave signals, A and B, a quarter step apart.3 min read · Switches, Relays & InputComponent guideBeginnerPotentiometers and TrimmersA potentiometer is a three-terminal variable resistor: the outer pins connect across the full resistance and the middle pin (wiper) taps a point you set with the knob.3 min read · Passive ComponentsHow-toBeginnerSSD1306 OLED DisplayThe SSD1306 is the driver chip in most small monochrome OLED displays, such as the popular 0.96 inch 128x64 module.3 min read · Displays & LEDsHow-toBeginnerPush Buttons with ArduinoWire a push button between a digital pin and ground, set the pin to INPUT_PULLUP, and the pin reads LOW while pressed.3 min read · Switches, Relays & Input

Sources

  • Alps EC11 series rotary encoder data sheet

Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.

On this page
Short answerWhat the KY-040 rotary encoder isKY-040 rotary encoder pinoutWiring the KY-040 rotary encoderExample codeSetup and tuningWhat you needAlternatives and when to choose themTry itCommon mistakesFAQsParts and next steps
Need a hand?Ask Geeko AI which part fits your project, or talk to the Geekobot team.Ask Geeko AIHelp & support

Parts for this guide2 items · ₹108

M274 KY-040 Incremental Rotary Encoder Brick Module – 20 CPR, 5V DC · Green Rotary Encoder Module – 20 CPR Incremental Encoder, PCB + Brass, 0-5V

Run itChange the inputs and watch the output

The sketch prints only when the count changes.

Knob 0
Serial monitor9600 baud
 

A simulation of the sketch above, so you know what to expect. Real readings vary with your parts and wiring.

Ask the Community

Ask a question