Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Component Documentation

How to Use Rotary Encoder: Examples, Pinouts, and Specs

Image of Rotary Encoder
Cirkit Designer LogoDesign with Rotary Encoder in Cirkit Designer

Introduction

The KY-040 Rotary Encoder is an electro-mechanical device manufactured by GIAK, designed to convert the angular position or rotation of a shaft into digital signals. Rotary encoders are widely used in various applications such as volume controls, scrolling through menus, and as input devices in user interfaces. The KY-040 is particularly popular among hobbyists and is often used with microcontroller platforms like Arduino due to its simplicity and ease of use.

Explore Projects Built with Rotary Encoder

Use Cirkit Designer to design, explore, and prototype these projects online. Some projects support real-time simulation. Click "Open Project" to start designing instantly!
Arduino Mega and Nano-Based Dual Rotary Encoder Controller with AC-DC Power Supply
Image of dual_encoder_v1: A project utilizing Rotary Encoder in a practical application
This circuit features an Arduino Mega 2560 and two Arduino Nano microcontrollers interfacing with two rotary encoders for input. The system is powered by an AC-DC PSU board converting 220V AC to 5V DC, and the microcontrollers communicate with each other via serial connections. The setup is designed for reading rotary encoder inputs and potentially processing or transmitting the data.
Cirkit Designer LogoOpen Project in Cirkit Designer
Arduino UNO-Based Rotary Encoder Interface
Image of encoder: A project utilizing Rotary Encoder in a practical application
This circuit features a rotary encoder (로터리 엔코) interfaced with an Arduino UNO microcontroller. The encoder's outputs A and B are connected to digital pins D2 and D3 for rotation detection, while its push button is connected to D4, potentially for a user input function. The encoder, push button, and a switch are all debounced using resistors, and the microcontroller is set up to receive these signals for processing, although the provided code is empty and does not define specific behaviors.
Cirkit Designer LogoOpen Project in Cirkit Designer
Arduino Mega 2560 Multi-Encoder Interface System
Image of 엔코더: A project utilizing Rotary Encoder in a practical application
This circuit is designed to interface multiple rotary encoders with an Arduino Mega 2560 microcontroller. Each encoder's DT (data) and CLK (clock) pins are connected to specific digital input pins on the Arduino, allowing the microcontroller to read their rotational position changes. The encoders are powered by the Arduino's 5V output and share a common ground, suggesting that the circuit may be used for input devices in a user interface or control system.
Cirkit Designer LogoOpen Project in Cirkit Designer
RP2040 Zero Rotary Encoder Interface with Serial Monitoring
Image of test: A project utilizing Rotary Encoder in a practical application
This circuit features an RP2040 Zero microcontroller interfaced with a rotary encoder. The encoder's clock, data, and switch pins are connected to the microcontroller's GPIO pins 29, 28, and 27, respectively, allowing the microcontroller to read the encoder's state and print it to the serial monitor.
Cirkit Designer LogoOpen Project in Cirkit Designer

Explore Projects Built with Rotary Encoder

Use Cirkit Designer to design, explore, and prototype these projects online. Some projects support real-time simulation. Click "Open Project" to start designing instantly!
Image of dual_encoder_v1: A project utilizing Rotary Encoder in a practical application
Arduino Mega and Nano-Based Dual Rotary Encoder Controller with AC-DC Power Supply
This circuit features an Arduino Mega 2560 and two Arduino Nano microcontrollers interfacing with two rotary encoders for input. The system is powered by an AC-DC PSU board converting 220V AC to 5V DC, and the microcontrollers communicate with each other via serial connections. The setup is designed for reading rotary encoder inputs and potentially processing or transmitting the data.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of encoder: A project utilizing Rotary Encoder in a practical application
Arduino UNO-Based Rotary Encoder Interface
This circuit features a rotary encoder (로터리 엔코) interfaced with an Arduino UNO microcontroller. The encoder's outputs A and B are connected to digital pins D2 and D3 for rotation detection, while its push button is connected to D4, potentially for a user input function. The encoder, push button, and a switch are all debounced using resistors, and the microcontroller is set up to receive these signals for processing, although the provided code is empty and does not define specific behaviors.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of 엔코더: A project utilizing Rotary Encoder in a practical application
Arduino Mega 2560 Multi-Encoder Interface System
This circuit is designed to interface multiple rotary encoders with an Arduino Mega 2560 microcontroller. Each encoder's DT (data) and CLK (clock) pins are connected to specific digital input pins on the Arduino, allowing the microcontroller to read their rotational position changes. The encoders are powered by the Arduino's 5V output and share a common ground, suggesting that the circuit may be used for input devices in a user interface or control system.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of test: A project utilizing Rotary Encoder in a practical application
RP2040 Zero Rotary Encoder Interface with Serial Monitoring
This circuit features an RP2040 Zero microcontroller interfaced with a rotary encoder. The encoder's clock, data, and switch pins are connected to the microcontroller's GPIO pins 29, 28, and 27, respectively, allowing the microcontroller to read the encoder's state and print it to the serial monitor.
Cirkit Designer LogoOpen Project in Cirkit Designer

Technical Specifications

Key Technical Details

  • Operating Voltage: 5V
  • Pulse Number for One Revolution: 20
  • Detent Positions: 20 per revolution
  • Shaft Diameter: 6mm
  • Output: 2-bit quadrature code
  • Switch Life: 200,000 cycles
  • Operating Temperature: -30°C to +70°C

Pin Configuration and Descriptions

Pin Number Description
1 Ground (GND)
2 +5V (VCC)
3 Signal A (CLK)
4 Signal B (DT)
5 Push Button (SW)

Usage Instructions

Connecting to a Circuit

To use the KY-040 Rotary Encoder with an Arduino UNO, connect the pins as follows:

  • GND to Arduino GND
  • +5V to Arduino 5V
  • Signal A (CLK) to a digital pin (e.g., D2)
  • Signal B (DT) to another digital pin (e.g., D3)
  • Push Button (SW) to another digital pin (e.g., D4)

Important Considerations and Best Practices

  • Use pull-up resistors on the CLK and DT pins to ensure stable readings.
  • Debounce the rotary encoder in software to prevent false readings due to mechanical noise.
  • Implement an interrupt service routine to handle the rotary encoder's signals without missing pulses.

Example Arduino Code

// Define the connections to the Arduino
const int pinCLK = 2; // Connected to CLK on KY-040
const int pinDT = 3;  // Connected to DT on KY-040
const int pinSW = 4;  // Connected to SW on KY-040

// Variables to hold the current and last encoder position
volatile int encoderPos = 0;
int lastEncoderPos = 0;
boolean buttonPressed = false;

// Interrupt service routine for encoder CLK pin
void isr() {
  if (digitalRead(pinDT) != digitalRead(pinCLK)) {
    encoderPos++; // Clockwise
  } else {
    encoderPos--; // Counterclockwise
  }
}

// Setup function
void setup() {
  pinMode(pinCLK, INPUT_PULLUP);
  pinMode(pinDT, INPUT_PULLUP);
  pinMode(pinSW, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(pinCLK), isr, CHANGE);
  Serial.begin(9600);
}

// Main loop
void loop() {
  if (encoderPos != lastEncoderPos) {
    Serial.println(encoderPos);
    lastEncoderPos = encoderPos;
  }
  if (digitalRead(pinSW) == LOW && !buttonPressed) {
    Serial.println("Button Pressed");
    buttonPressed = true;
  } else if (digitalRead(pinSW) == HIGH && buttonPressed) {
    buttonPressed = false;
  }
}

Troubleshooting and FAQs

Common Issues

  • Jittery or Inconsistent Readings: This can be caused by mechanical noise or a lack of debouncing in the code. Implement software debouncing to mitigate this issue.
  • No Response from the Encoder: Ensure that the encoder is properly powered and that all connections are secure. Check for any damage to the encoder or its pins.
  • Button Not Working: Verify that the button pin is correctly connected and that the internal pull-up resistor is enabled in the code.

Solutions and Tips for Troubleshooting

  • Debouncing: Implement a simple debounce algorithm in the interrupt service routine or use a library that handles encoder debouncing.
  • Check Connections: Use a multimeter to ensure that there is continuity between the encoder pins and the Arduino pins.
  • Serial Output: Use Serial.println() statements to debug and monitor the encoder's output in real-time.

FAQs

Q: Can I use the KY-040 Rotary Encoder with a 3.3V system? A: Yes, but the output signal levels will be lower, which may require level shifting for some 5V systems.

Q: How can I increase the resolution of the encoder? A: The KY-040 has a fixed resolution of 20 pulses per revolution. To increase the effective resolution, you can use gear ratios or employ software algorithms to interpolate between pulses.

Q: Is it possible to use the encoder without an interrupt? A: Yes, but using interrupts is recommended to ensure all pulses are captured, especially at higher rotation speeds.