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

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

Image of KY-040
Cirkit Designer LogoDesign with KY-040 in Cirkit Designer

Introduction

The KY-040 is a rotary encoder module that allows for precise control of position and rotation. Unlike a potentiometer, which provides an absolute position, the KY-040 outputs incremental signals, making it ideal for applications requiring relative position tracking. It features a built-in push button and provides two output signals (A and B) that can be used to determine the direction and amount of rotation.

Explore Projects Built with 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!
Arduino Mega 2560 Bluetooth-Controlled Flame Detection System with Servo Actuation
Image of apv circuit 1: A project utilizing KY-040 in a practical application
This circuit uses an Arduino Mega 2560 to monitor four KY-026 flame sensors and control four micro servo motors. The HC-05 Bluetooth module allows for wireless communication, enabling remote monitoring and control of the system.
Cirkit Designer LogoOpen Project in Cirkit Designer
Arduino Nano Joystick-Controlled Bluetooth Module with Battery Power
Image of padelpro transmitter: A project utilizing KY-040 in a practical application
This circuit is a wireless joystick controller that uses an Arduino Nano to read analog signals from a KY-023 Dual Axis Joystick Module and transmits the data via an HC-05 Bluetooth Module. The system is powered by a 18650 Li-Ion battery with a rocker switch for power control.
Cirkit Designer LogoOpen Project in Cirkit Designer
Wireless Joystick-Controlled Interface with Arduino Nano and NRF24L01
Image of Transmitter 11: A project utilizing KY-040 in a practical application
This circuit features an Arduino Nano interfaced with a KY-023 Dual Axis Joystick Module for analog input, and an NRF24L01 module for wireless communication. The joystick provides x and y-axis control signals to the Arduino's analog inputs and a switch signal to a digital input, while the NRF24L01 enables the Arduino to communicate with other devices wirelessly. The 2x 18650 batteries supply power to the Arduino, which in turn powers the joystick and the NRF24L01 module.
Cirkit Designer LogoOpen Project in Cirkit Designer
ESP32-Based Security System with RFID and Laser Intrusion Detection
Image of CPE doorlock system upgrade: A project utilizing KY-040 in a practical application
This circuit is a security and access control system featuring motion detection, laser beam-break sensing, and RFID scanning, interfaced with a keypad and visual/audible indicators, powered by a solar-charged battery, and capable of controlling an electric lock via a relay.
Cirkit Designer LogoOpen Project in Cirkit Designer

Explore Projects Built with 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 apv circuit 1: A project utilizing KY-040 in a practical application
Arduino Mega 2560 Bluetooth-Controlled Flame Detection System with Servo Actuation
This circuit uses an Arduino Mega 2560 to monitor four KY-026 flame sensors and control four micro servo motors. The HC-05 Bluetooth module allows for wireless communication, enabling remote monitoring and control of the system.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of padelpro transmitter: A project utilizing KY-040 in a practical application
Arduino Nano Joystick-Controlled Bluetooth Module with Battery Power
This circuit is a wireless joystick controller that uses an Arduino Nano to read analog signals from a KY-023 Dual Axis Joystick Module and transmits the data via an HC-05 Bluetooth Module. The system is powered by a 18650 Li-Ion battery with a rocker switch for power control.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of Transmitter 11: A project utilizing KY-040 in a practical application
Wireless Joystick-Controlled Interface with Arduino Nano and NRF24L01
This circuit features an Arduino Nano interfaced with a KY-023 Dual Axis Joystick Module for analog input, and an NRF24L01 module for wireless communication. The joystick provides x and y-axis control signals to the Arduino's analog inputs and a switch signal to a digital input, while the NRF24L01 enables the Arduino to communicate with other devices wirelessly. The 2x 18650 batteries supply power to the Arduino, which in turn powers the joystick and the NRF24L01 module.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of CPE doorlock system upgrade: A project utilizing KY-040 in a practical application
ESP32-Based Security System with RFID and Laser Intrusion Detection
This circuit is a security and access control system featuring motion detection, laser beam-break sensing, and RFID scanning, interfaced with a keypad and visual/audible indicators, powered by a solar-charged battery, and capable of controlling an electric lock via a relay.
Cirkit Designer LogoOpen Project in Cirkit Designer

Common Applications and Use Cases

  • Volume control in audio devices
  • Menu navigation in embedded systems
  • Motor speed and position control
  • Robotics and automation systems
  • User input for microcontroller-based projects

Technical Specifications

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

Parameter Value
Operating Voltage 3.3V to 5V
Output Type Digital (Incremental)
Number of Pins 5
Push Button Built-in
Rotational Steps 20 steps per full rotation
Dimensions 32mm x 19mm x 30mm

Pin Configuration and Descriptions

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

Pin Label Description
1 GND Ground connection
2 + Power supply (3.3V to 5V)
3 SW Push button output (active LOW)
4 DT Data signal (Channel B)
5 CLK Clock signal (Channel A)

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 Output Pins:
    • Connect the CLK (Channel A) and DT (Channel B) pins to digital input pins on your microcontroller.
    • Optionally, connect the SW pin to a digital input pin if you want to use the push button.
  3. Read the Signals:
    • Monitor the CLK and DT signals to determine the direction and amount of rotation.
    • The SW pin can be used to detect button presses.

Important Considerations and Best Practices

  • Debouncing: The KY-040 outputs may produce noise or "bouncing" signals. Use software debouncing or external capacitors to ensure clean signal readings.
  • Pull-up Resistors: If the SW pin is not functioning as expected, ensure that a pull-up resistor is enabled in your microcontroller or added externally.
  • Power Supply: Ensure the module is powered within its operating voltage range (3.3V to 5V) to avoid damage.

Example: Using KY-040 with Arduino UNO

Below is an example Arduino sketch to read the KY-040 rotary encoder and detect rotation direction and button presses:

// Define KY-040 pins
#define CLK 2  // Clock pin (Channel A)
#define DT 3   // Data pin (Channel B)
#define SW 4   // Push button pin

int lastStateCLK;  // To store the previous state of the CLK pin
int currentStateCLK;  // To store the current state of the CLK pin
int counter = 0;  // Counter to track rotation
bool buttonPressed = false;  // Flag for button press

void setup() {
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  pinMode(SW, INPUT_PULLUP);  // Enable internal pull-up resistor for SW pin

  // Read the initial state of the CLK pin
  lastStateCLK = digitalRead(CLK);

  Serial.begin(9600);  // Initialize serial communication
}

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

  // If the state of CLK has changed, a rotation has occurred
  if (currentStateCLK != lastStateCLK) {
    // Check the direction of rotation
    if (digitalRead(DT) != currentStateCLK) {
      counter++;  // Clockwise rotation
    } else {
      counter--;  // Counterclockwise rotation
    }

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

  // Update the last state of CLK
  lastStateCLK = currentStateCLK;

  // Check if the button is pressed
  if (digitalRead(SW) == LOW) {
    if (!buttonPressed) {
      Serial.println("Button Pressed!");
      buttonPressed = true;  // Set the flag to avoid multiple prints
    }
  } else {
    buttonPressed = false;  // Reset the flag when the button is released
  }
}

Troubleshooting and FAQs

Common Issues and Solutions

  1. No Response from the Encoder:

    • Ensure the power supply is connected correctly and within the specified voltage range.
    • Verify that the CLK and DT pins are connected to the correct microcontroller pins.
  2. Incorrect Rotation Readings:

    • Check for loose or faulty connections.
    • Implement software debouncing to filter out noise from the encoder signals.
  3. Push Button Not Working:

    • Ensure the SW pin is connected to a digital input pin with a pull-up resistor enabled.
    • Verify that the button is being pressed fully.
  4. Erratic or Unstable Readings:

    • Add a small capacitor (e.g., 0.1µF) between the CLK and GND pins, and between the DT and GND pins, to reduce noise.

FAQs

Q: Can the KY-040 be used with 3.3V microcontrollers like the ESP32?
A: Yes, the KY-040 is compatible with 3.3V systems. Ensure the power supply and signal levels match the microcontroller's requirements.

Q: How do I increase the resolution of the encoder?
A: The KY-040 has a fixed resolution of 20 steps per rotation. For higher resolution, consider using a different encoder with more steps.

Q: Can I use the KY-040 for absolute position tracking?
A: No, the KY-040 is an incremental encoder and does not provide absolute position information. It is best suited for relative position tracking.