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, which provide an absolute position, the KY-040 generates incremental signals that allow users to track relative movement. It features a knob that can be rotated in either direction and a push-button switch for additional functionality. The module is widely used in applications requiring user input, such as volume control, menu navigation, and motor control.

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 Nano-Based Wireless Joystick and Motion Controller
Image of hand gesture: A project utilizing ky-040 in a practical application
This circuit features an Arduino Nano microcontroller interfaced with an HC-05 Bluetooth module, an MPU-6050 accelerometer/gyroscope, and a KY-023 Dual Axis Joystick Module. The Arduino Nano is powered by a 9V battery through a rocker switch and communicates with the HC-05 for Bluetooth connectivity, reads joystick positions from the KY-023 module via analog inputs, and communicates with the MPU-6050 over I2C to capture motion data. The circuit is likely designed for wireless control and motion sensing applications, such as a remote-controlled robot or a game controller.
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
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 UNO Controlled Joystick Interface with LCD Feedback and Audio Alert
Image of 우주게임: A project utilizing ky-040 in a practical application
This circuit features an Arduino UNO microcontroller connected to a KY-023 Dual Axis Joystick Module, an I2C LCD 16x2 Screen, a Piezo Speaker, and a Pushbutton. The joystick provides two analog inputs to the Arduino for X and Y axis control, while the pushbutton is connected to a digital input for user interaction. The LCD screen displays information via I2C communication, and the Piezo Speaker is driven by a digital output from the Arduino for audio feedback.
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 hand gesture: A project utilizing ky-040 in a practical application
Arduino Nano-Based Wireless Joystick and Motion Controller
This circuit features an Arduino Nano microcontroller interfaced with an HC-05 Bluetooth module, an MPU-6050 accelerometer/gyroscope, and a KY-023 Dual Axis Joystick Module. The Arduino Nano is powered by a 9V battery through a rocker switch and communicates with the HC-05 for Bluetooth connectivity, reads joystick positions from the KY-023 module via analog inputs, and communicates with the MPU-6050 over I2C to capture motion data. The circuit is likely designed for wireless control and motion sensing applications, such as a remote-controlled robot or a game controller.
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 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 우주게임: A project utilizing ky-040 in a practical application
Arduino UNO Controlled Joystick Interface with LCD Feedback and Audio Alert
This circuit features an Arduino UNO microcontroller connected to a KY-023 Dual Axis Joystick Module, an I2C LCD 16x2 Screen, a Piezo Speaker, and a Pushbutton. The joystick provides two analog inputs to the Arduino for X and Y axis control, while the pushbutton is connected to a digital input for user interaction. The LCD screen displays information via I2C communication, and the Piezo Speaker is driven by a digital output from the Arduino for audio feedback.
Cirkit Designer LogoOpen Project in Cirkit Designer

Common Applications

  • Volume and brightness adjustment
  • Menu navigation in embedded systems
  • Motor speed and position control
  • Robotics and automation systems
  • DIY electronics 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 Pulses per Revolution 20
Push-Button Switch Integrated
Dimensions 32mm x 19mm x 30mm

Pin Configuration

The KY-040 module has 5 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 switch output (active LOW)
4 DT Data signal (used to determine rotation direction when combined with CLK)
5 CLK Clock signal (used to generate pulses during rotation)

Usage Instructions

The KY-040 rotary encoder is easy to integrate into circuits and works seamlessly with microcontrollers like the Arduino UNO. Below are the steps to use the module effectively:

Connecting the KY-040 to an Arduino UNO

  1. Connect the GND pin of the KY-040 to the GND pin on the Arduino.
  2. Connect the + pin of the KY-040 to the 5V pin on the Arduino.
  3. Connect the CLK pin to a digital input pin on the Arduino (e.g., pin 2).
  4. Connect the DT pin to another digital input pin on the Arduino (e.g., pin 3).
  5. Connect the SW pin to a digital input pin (e.g., pin 4) if you want to use the push-button functionality.

Sample Arduino Code

The following code demonstrates how to read the rotation direction and button press from the KY-040 module:

// Define pins for the KY-040 rotary encoder
#define CLK 2  // Clock pin
#define DT 3   // Data pin
#define SW 4   // Switch 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);  // 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 rotation direction
    if (stateDT != 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 the CLK pin
  lastStateCLK = currentStateCLK;

  // Check if the button is pressed
  if (digitalRead(SW) == LOW) {
    if (!buttonPressed) {
      Serial.println("Button Pressed!");
      buttonPressed = true;
    }
  } else {
    buttonPressed = false;
  }
}

Important Considerations

  • Debouncing: The KY-040 may produce noisy signals during rotation or button presses. Use software debouncing techniques or external capacitors to filter out noise.
  • Power Supply: Ensure the module is powered within its operating voltage range (3.3V to 5V).
  • Pull-Up Resistors: The push-button switch requires a pull-up resistor. The Arduino's internal pull-up resistor can be enabled using INPUT_PULLUP.

Troubleshooting and FAQs

Common Issues

  1. No Response from the Encoder

    • Solution: Verify all connections, especially the GND and power pins. Ensure the CLK and DT pins are connected to the correct digital input pins on the microcontroller.
  2. Incorrect Rotation Direction

    • Solution: Swap the connections of the CLK and DT pins on the microcontroller.
  3. Button Not Working

    • Solution: Ensure the SW pin is connected to a digital input pin and configured with a pull-up resistor.
  4. Noisy or Erratic Readings

    • Solution: Implement software debouncing or add a small capacitor (e.g., 0.1µF) between the CLK/DT pins and GND to filter noise.

FAQs

  1. Can the KY-040 be used with 3.3V systems?

    • Yes, the KY-040 is compatible with both 3.3V and 5V systems.
  2. What is the resolution of the KY-040?

    • The KY-040 generates 20 pulses per revolution, but the effective resolution depends on how the signals are processed (e.g., full-step or half-step decoding).
  3. Can I use the KY-040 for absolute position tracking?

    • No, the KY-040 is an incremental encoder and only tracks relative movement. For absolute position tracking, additional circuitry or sensors are required.
  4. Is the KY-040 suitable for high-speed applications?

    • The KY-040 is designed for manual input and may not perform well in high-speed applications due to signal noise and mechanical limitations.