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

How to Use Encoder KY-040: Examples, Pinouts, and Specs

Image of Encoder KY-040Design with Encoder KY-040 in Cirkit Designer

Introduction

The KY-040 is a rotary encoder that detects rotational position and direction. Unlike a potentiometer, which provides an absolute position, the KY-040 outputs quadrature pulses that indicate relative movement. This makes it ideal for applications requiring precise control or incremental adjustments. Additionally, the KY-040 includes a built-in push button, expanding its functionality for user interface controls.

Explore Projects Built with Encoder KY-040

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 Configurable Battery-Powered RF Signal Transmitter with DIP Switch Settings: A project utilizing Encoder KY-040 in a practical application
Configurable Battery-Powered RF Signal Transmitter with DIP Switch Settings

This circuit appears to be a configurable encoder system with an RF transmission capability. The encoder's address pins (A0-A7) are connected to a DIP switch for setting the address, and its data output (DO) is connected to an RF transmitter, allowing the encoded signal to be wirelessly transmitted. The circuit is powered by a 9V battery, regulated to 5V by a 7805 voltage regulator, and includes a diode for polarity protection. Tactile switches are connected to the encoder's data inputs (D1-D3), and an LED with a current-limiting resistor indicates power or activity.

Open Project in Cirkit Designer

Common Applications

Technical Specifications

The KY-040 rotary encoder has the following key specifications:

Parameter Value
Operating Voltage 3.3V to 5V
Output Type Digital (Quadrature Pulses)
Number of Pulses per Revolution 20
Push Button Type Momentary (Normally Open)
Operating Temperature -40°C to +85°C
Dimensions 19mm x 15mm x 29mm

Pin Configuration

The KY-040 has 5 pins, as described in the table below:

Pin Name Description
1 GND Ground connection
2 + Power supply (3.3V to 5V)
3 SW Push button output (active LOW)
4 DT Data signal (used for detecting rotation direction, works with CLK)
5 CLK Clock signal (used for detecting rotation steps, works with DT)

Usage Instructions

How to Use the KY-040 in a Circuit

  1. Connect the Power Supply: Connect the + pin to a 3.3V or 5V power source and the GND pin to ground.
  2. Connect the Rotary Encoder Outputs:
    • Connect the CLK and DT pins to digital input pins on your microcontroller.
    • These pins will output quadrature signals to indicate rotation direction and steps.
  3. Connect the Push Button:
    • Connect the SW pin to a digital input pin on your microcontroller.
    • Use a pull-up resistor if your microcontroller does not have internal pull-ups.
  4. Read the Signals:
    • Monitor the CLK and DT signals to detect rotation.
    • Monitor the SW signal to detect button presses.

Important Considerations

Example Code for Arduino UNO

Below is an example of how to use the KY-040 with an Arduino UNO to detect rotation and button presses:

// Define pins for the KY-040 encoder
#define CLK 2  // Clock pin connected to digital pin 2
#define DT 3   // Data pin connected to digital pin 3
#define SW 4   // Switch pin connected to digital pin 4

int lastCLKState;  // To store the previous state of the CLK pin
int currentCLKState;  // To store the current state of the CLK pin
int counter = 0;  // Counter to track rotation steps

void setup() {
  pinMode(CLK, INPUT);  // Set CLK pin as input
  pinMode(DT, INPUT);   // Set DT pin as input
  pinMode(SW, INPUT_PULLUP);  // Set SW pin as input with pull-up resistor

  Serial.begin(9600);  // Initialize serial communication
  lastCLKState = digitalRead(CLK);  // Read the initial state of the CLK pin
}

void loop() {
  // Read the current state of the CLK pin
  currentCLKState = digitalRead(CLK);

  // Check if the CLK state has changed
  if (currentCLKState != lastCLKState) {
    // Determine rotation direction based on the DT pin state
    if (digitalRead(DT) != currentCLKState) {
      counter++;  // Clockwise rotation
    } else {
      counter--;  // Counterclockwise rotation
    }

    // Print the counter value to the serial monitor
    Serial.print("Counter: ");
    Serial.println(counter);
  }

  // Update the last CLK state
  lastCLKState = currentCLKState;

  // Check if the push button is pressed
  if (digitalRead(SW) == LOW) {
    Serial.println("Button Pressed!");
    delay(200);  // Debounce delay
  }
}

Notes on the Code

Troubleshooting and FAQs

Common Issues and Solutions

  1. No Response from the Encoder:

    • Ensure the power supply is connected correctly to the + and GND pins.
    • Verify that the CLK and DT pins are connected to the correct digital input pins on the microcontroller.
  2. Incorrect Rotation Direction:

    • Swap the connections of the CLK and DT pins to correct the direction.
  3. Button Not Detected:

    • Check if the SW pin is connected properly.
    • Ensure a pull-up resistor is used if the microcontroller does not have internal pull-ups.
  4. Noisy or Erratic Output:

    • Add software debouncing to filter out noise.
    • Use a small capacitor (e.g., 0.1µF) between the CLK and GND pins and between the DT and GND pins.

FAQs

Q: Can the KY-040 be used with 3.3V microcontrollers like the ESP32?
A: Yes, the KY-040 operates at 3.3V to 5V, making it compatible with 3.3V microcontrollers.

Q: How many steps does the KY-040 encoder have per revolution?
A: The KY-040 has 20 steps per full revolution.

Q: Do I need external pull-up resistors for the KY-040?
A: If your microcontroller does not have internal pull-ups, you will need to add external pull-up resistors for the SW, CLK, and DT pins.

Q: Can I use the KY-040 for absolute position sensing?
A: No, the KY-040 is a relative encoder and does not provide absolute position information.