Read a KY-040 rotary encoder with an Arduino

FunduinoPublished on

Drehrichtung und Schritte auslesen – mit Interrupt für den Taster

You need this

KY-040 Rotary Encoder - Incremental Encoder with Push Button, 5 V KY-040 Rotary Encoder - Incremental Encoder with Push Button, 5 V 1,84 € *Item no. FUN-1010566About the product — KY-040 Rotary Encoder - Incremental Encoder with Push Button, 5 V
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino

All 3 parts together 15,86 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

How does the encoder work?

The KY-040 rotary encoder is basically a rotary input module that closely resembles a potentiometer. Its special feature: You can easily read which direction you turned the knob and how many steps it moved. You can also feel the individual steps as you turn it.

The pins are labeled A, B, and C. Pins A and C are connected to each other, as are pins B and C. In each encoder position, both connections are either open or closed. When you turn the knob in either direction, the status changes: If both connections are closed, turning the knob opens them. If both are open, turning the knob closes them.

When you turn the knob clockwise, the status between pins A and C changes first. When you turn it counterclockwise, the status between pins B and C changes first. The controller reads and evaluates this sequence.

KY-040 rotary encoder from above with knob
The KY-040 rotary encoder from above
Wiring the KY-040 rotary encoder to an Arduino
Setup and wiring of the rotary encoder
1 / 2

Pinout

The KY-040 rotary encoder has five pins:

  • CLK (Serial Clock), data output 1 – connect to digital pin 6 on the microcontroller
  • DT, data output 2 – connect to digital pin 5 on the microcontroller
  • SW (Switch) – outputs a LOW signal when you press the button. Connect it to an interrupt pin.
  • + – power supply, connect to the 5V pin on the microcontroller
  • GND – connect to the GND pin on the microcontroller

You can find an overview of all interrupt pins on the official Arduino website.

You can connect CLK and DT to any digital pins you choose. In the sketch below, they are D5 and D6.

Example sketch

Now read the encoder's step position. You should also detect whether the button has been pressed. Display both in the serial monitor, including with the attachInterrupt() command.

You can find detailed documentation for this function here.

Note: The KY-040 module requires its own library. You can find the Encoder Library at github.com/PaulStoffregen/Encoder.

Serial monitor with encoder step positions
Output in the serial monitor
Read the step position and key press
#include <Encoder.h>    // Using the library
const int CLK = 6;      // Pin definitions. CLK to D6, DT to D5.
const int DT = 5;
const int SW = 2;       // The switch is connected to pin D2. IMPORTANT: Use an interrupt pin!
long altePosition = -999;  // Definition of the "old" position (This fictitious old position is needed so that the current position is displayed in the serial monitor later only when we move the rotary encoder)
Encoder meinEncoder(DT,CLK);  // A new encoder project is created here. The connection is made using the previously defined variables (DT and CLK).
void setup()   // Beginning of setup
{
  Serial.begin(9600);
  pinMode(SW, INPUT);   // The interrupt is installed here.
  attachInterrupt(digitalPinToInterrupt(SW), Interrupt, CHANGE); // As soon as the status (CHANGE) of the interrupt pin (SW = D2) changes, the interrupt command is executed.
}
void loop()
{
    long neuePosition = meinEncoder.read();  // The encoder's "new" position is defined. The encoder's current position is read using the Variable.Command().
        if (neuePosition != altePosition)  // If the new position is not equal to the old one (-999) (and only then!)…
        {
        altePosition = neuePosition;
        Serial.println(neuePosition);      // …the current position is displayed in the serial monitor.
        }
}
void Interrupt() // Beginning of the interrupt. When the rotary knob is pressed, the program automatically jumps to this point. After…
{
  Serial.println("Switch pressed"); //…after the signal is output, the program continues.
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.