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 designed for precise control of position and rotation. Unlike potentiometers, rotary encoders can rotate infinitely in either direction, making them ideal for applications requiring continuous or incremental adjustments. The module also includes a built-in push button, adding an extra layer of functionality for user input.

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 or menu control in audio and video devices
  • Navigation in user interfaces (e.g., scrolling through menus)
  • Robotics for position and speed control
  • CNC machines and 3D printers for precise adjustments
  • DIY electronics projects requiring rotary input

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 Encoder)
Number of Pins 5
Push Button Integrated (Momentary Switch)
Rotational Steps 20 steps per full rotation
Dimensions 32mm x 19mm x 30mm (approximate)

Pin Configuration and Descriptions

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

Pin Label Description
1 GND Ground connection for the module.
2 + Power supply pin (3.3V to 5V).
3 SW Push button output. Active LOW when the button is pressed.
4 DT Data pin for the rotary encoder. Outputs pulses based on rotation direction.
5 CLK Clock pin for the rotary encoder. Outputs pulses based on rotation movement.

Usage Instructions

How to Use the KY-040 in a Circuit

  1. Connect the Pins:

    • Connect the GND pin to the ground of your circuit.
    • Connect the + pin to a 3.3V or 5V power source.
    • Connect the CLK and DT pins to digital input pins on your microcontroller.
    • Optionally, connect the SW pin to another digital input pin to use the push button.
  2. Read the Encoder Output:

    • The CLK and DT pins generate pulses as the encoder is rotated. The sequence of these pulses determines the direction of rotation (clockwise or counterclockwise).
    • The SW pin outputs a LOW signal when the button is pressed.
  3. Debounce Signals:

    • Rotary encoders and push buttons can produce noisy signals. Use software debouncing or external capacitors to ensure stable readings.

Important Considerations and Best Practices

  • Power Supply: Ensure the module is powered within its operating voltage range (3.3V to 5V).
  • Pull-Up Resistors: Use pull-up resistors on the SW, CLK, and DT pins if your microcontroller does not have internal pull-ups enabled.
  • Signal Processing: Use interrupts or polling to read the encoder signals effectively, especially in time-sensitive applications.
  • Mechanical Limitations: Avoid applying excessive force to the rotary shaft to prevent damage.

Example Code for Arduino UNO

Below is an example Arduino sketch to read the KY-040 rotary encoder and push button:

// Define pin connections for the KY-040 module
#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 counter = 0;       // Variable to store the encoder position
int lastStateCLK;      // Previous state of the CLK pin
int currentStateCLK;   // Current state of the CLK pin
bool buttonPressed = false; // Flag for button press

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

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

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

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

  // Check if the state of CLK has changed
  if (currentStateCLK != lastStateCLK) {
    // Read the state of the DT pin
    int stateDT = digitalRead(DT);

    // Determine the rotation direction
    if (stateDT != currentStateCLK) {
      counter++; // Clockwise rotation
    } else {
      counter--; // Counterclockwise rotation
    }

    // Print the current counter value
    Serial.print("Position: ");
    Serial.println(counter);
  }

  // Update the last state of the CLK pin
  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 triggers
    }
  } else {
    buttonPressed = false; // Reset the flag when the button is released
  }
}

Troubleshooting and FAQs

Common Issues and Solutions

  1. No Output from the Encoder:

    • Ensure all connections are secure and correct.
    • Verify that the module is powered within the specified voltage range.
    • Check for loose or damaged wires.
  2. Unstable or Erratic Readings:

    • Add software debouncing to filter out noise from the encoder signals.
    • Use external capacitors (e.g., 0.1µF) between the CLK/DT pins and ground to reduce noise.
  3. Push Button Not Responding:

    • Verify that the SW pin is connected to a digital input pin.
    • Ensure the microcontroller's internal pull-up resistor is enabled or use an external pull-up resistor.
  4. Incorrect Direction Detection:

    • Swap the connections of the CLK and DT pins to correct the direction.

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 matches the microcontroller's voltage.

Q: How many steps does the encoder have per rotation?
A: The KY-040 typically has 20 steps per full rotation, but this may vary slightly depending on the module.

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 feedback. You must track the position in software.