Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

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

Image of Rotary EncoderDesign 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!
Image of Arduino UNO-Based Rotary Encoder Interface: 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.

Open Project in Cirkit Designer
Image of Arduino Mega 2560 Multi-Encoder Interface System: 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.

Open Project in Cirkit Designer

Technical Specifications

Key Technical Details

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:

Important Considerations and Best Practices

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

Solutions and Tips for Troubleshooting

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.