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

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

Image of Encoder KY-040
Cirkit Designer LogoDesign 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!
Rotary Encoder Interface with STG Adapter for Signal Processing
Image of Encoder in STG: A project utilizing Encoder KY-040 in a practical application
The circuit consists of two rotary encoders (Kalamoyi P3022-V1-CW360) connected to two STG adapters. Each encoder's VCC, OUT, and GND pins are connected to the corresponding STG adapter, facilitating signal transmission and power supply management.
Cirkit Designer LogoOpen Project in Cirkit Designer
ESP32-Based Security System with RFID and Laser Tripwire
Image of CPE doorlock system: A project utilizing Encoder KY-040 in a practical application
This circuit is designed for a comprehensive security and access control system with motion detection, access via RFID, and a break-beam sensor. It includes a solenoid lock controlled by a relay, visual and audible alerts, and a robust power management system with solar and battery backup to ensure uninterrupted operation.
Cirkit Designer LogoOpen Project in Cirkit Designer
Configurable Battery-Powered RF Signal Transmitter with DIP Switch Settings
Image of fyp transmitter: A project utilizing Encoder KY-040 in a practical application
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.
Cirkit Designer LogoOpen Project in Cirkit Designer
Battery-Powered MIDI Controller with nRF52840, AS5600 Encoders, and OLED Display
Image of midi lr driver: A project utilizing Encoder KY-040 in a practical application
This circuit is a MIDI controller that uses an nRF52840 ProMicro microcontroller, multiple AS5600 magnetic encoders, an Adafruit TCA9548A I2C multiplexer, an OLED display, and several pushbuttons. The microcontroller reads the encoder positions and button states, displays information on the OLED screen, and sends MIDI signals based on user interactions.
Cirkit Designer LogoOpen Project in Cirkit Designer

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 Encoder in STG: A project utilizing Encoder KY-040 in a practical application
Rotary Encoder Interface with STG Adapter for Signal Processing
The circuit consists of two rotary encoders (Kalamoyi P3022-V1-CW360) connected to two STG adapters. Each encoder's VCC, OUT, and GND pins are connected to the corresponding STG adapter, facilitating signal transmission and power supply management.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of CPE doorlock system: A project utilizing Encoder KY-040 in a practical application
ESP32-Based Security System with RFID and Laser Tripwire
This circuit is designed for a comprehensive security and access control system with motion detection, access via RFID, and a break-beam sensor. It includes a solenoid lock controlled by a relay, visual and audible alerts, and a robust power management system with solar and battery backup to ensure uninterrupted operation.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of fyp transmitter: 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.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of midi lr driver: A project utilizing Encoder KY-040 in a practical application
Battery-Powered MIDI Controller with nRF52840, AS5600 Encoders, and OLED Display
This circuit is a MIDI controller that uses an nRF52840 ProMicro microcontroller, multiple AS5600 magnetic encoders, an Adafruit TCA9548A I2C multiplexer, an OLED display, and several pushbuttons. The microcontroller reads the encoder positions and button states, displays information on the OLED screen, and sends MIDI signals based on user interactions.
Cirkit Designer LogoOpen Project in Cirkit Designer

Common Applications

  • Volume control in audio systems
  • Menu navigation in embedded systems
  • Motor speed and position control
  • Robotics and automation
  • User interface controls for devices

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

  • Debouncing: The mechanical nature of the encoder may cause signal noise. Use software debouncing or external capacitors to filter out unwanted noise.
  • Pull-up Resistors: Ensure pull-up resistors are used for the SW, CLK, and DT pins if your microcontroller does not have internal pull-ups.
  • Power Supply: Operate the KY-040 within its specified voltage range (3.3V to 5V) to avoid damage.

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

  • The CLK and DT pins are used to detect rotation direction and steps.
  • The SW pin is used to detect button presses.
  • A debounce delay is added to avoid false triggers from the push button.

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.