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

A rotary encoder is an electromechanical device that converts the angular position or motion of a shaft or axle to an analog or digital code. Unlike potentiometers, rotary encoders provide full rotation without limits. They are commonly used in applications that require precise control over parameters such as volume, selection, or navigation in user interfaces, as well as in robotics and industrial controls for monitoring movement.

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 Name Description
1 Vcc Power supply (3.3V to 5V)
2 GND Ground connection
3 A Output A - Quadrature output 1
4 B Output B - Quadrature output 2
5 SW Pushbutton switch (active low, optional)

Usage Instructions

Connecting to a Circuit

  1. Connect the Vcc pin to the power supply (3.3V or 5V).
  2. Connect the GND pin to the ground of the power supply.
  3. Connect the A and B pins to two digital input pins on a microcontroller, such as an Arduino UNO.
  4. If available, connect the SW pin to another digital input pin for the pushbutton feature.

Important Considerations and Best Practices

Example Code for Arduino UNO

// Define the pins connected to the encoder
const int pinA = 2; // Encoder output A
const int pinB = 3; // Encoder output B
const int buttonPin = 4; // Encoder button pin

volatile int encoderPos = 0; // Variable to store the encoder position
bool lastEncoded = LOW; // Last encoded signal from pin A

// Interrupt service routine for encoder A change
void doEncoderA(){
  bool currentEncoded = digitalRead(pinA);
  if (digitalRead(pinB) != currentEncoded) {
    encoderPos++;
  } else {
    encoderPos--;
  }
  lastEncoded = currentEncoded;
}

// Interrupt service routine for encoder B change
void doEncoderB(){
  bool currentEncoded = digitalRead(pinA);
  if (digitalRead(pinB) == currentEncoded) {
    encoderPos++;
  } else {
    encoderPos--;
  }
}

void setup() {
  pinMode(pinA, INPUT);
  pinMode(pinB, INPUT);
  pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up

  // Attach the interrupt service routines to the encoder pins
  attachInterrupt(digitalPinToInterrupt(pinA), doEncoderA, CHANGE);
  attachInterrupt(digitalPinToInterrupt(pinB), doEncoderB, CHANGE);
}

void loop() {
  // The main program can read encoderPos to determine the current position
  // or use the button state for additional functionality.
}

Troubleshooting and FAQs

Common Issues

Solutions and Tips for Troubleshooting

FAQs

Q: Can I use a rotary encoder for continuous rotation? A: Yes, rotary encoders are designed for continuous rotation without limits.

Q: How do I determine the direction of rotation? A: The direction can be determined by the sequence of the A and B outputs. If A leads B, for instance, the encoder is rotating in one direction; if B leads A, it's the opposite direction.

Q: What is the purpose of the SW pin? A: The SW pin is connected to a pushbutton switch built into the encoder. It can be used as a select button or for any other input purpose.

Q: How do I increase the resolution of my encoder? A: The resolution is fixed based on the encoder's design. To achieve higher resolution, you would need to use an encoder with a higher PPR specification.