

A rotary encoder with an integrated push button is a versatile electronic component used to convert rotational position into an analog or digital signal. This component is widely used in various applications, including:








| Parameter | Value |
|---|---|
| Operating Voltage | 3.3V to 5V |
| Maximum Current | 10mA |
| Output Type | Digital (Quadrature) |
| Push Button Rating | 10mA @ 5V |
| Rotational Steps | 20 steps per revolution |
| Debounce Time | 5ms (recommended) |
| Pin Number | Pin Name | Description |
|---|---|---|
| 1 | GND | Ground |
| 2 | +V | Supply Voltage (3.3V to 5V) |
| 3 | SW | Push Button Output |
| 4 | DT | Data Output (Quadrature Signal B) |
| 5 | CLK | Clock Output (Quadrature Signal A) |
Power Supply:
+V pin to a 3.3V or 5V power supply.GND pin to the ground of your circuit.Rotary Encoder Outputs:
CLK pin to a digital input pin on your microcontroller.DT pin to another digital input pin on your microcontroller.Push Button Output:
SW pin to a digital input pin on your microcontroller.SW, CLK, and DT pins to ensure stable signals.// Rotary Encoder with Button Example Code for Arduino UNO
#define CLK 2 // Pin connected to CLK
#define DT 3 // Pin connected to DT
#define SW 4 // Pin connected to SW
int counter = 0; // Counter to track encoder position
int currentStateCLK;
int lastStateCLK;
bool buttonPressed = false;
void setup() {
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP); // Enable internal pull-up resistor
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If the state of CLK has changed, then we have a pulse
if (currentStateCLK != lastStateCLK) {
// If the DT state is different than the CLK state, then
// the encoder is rotating counterclockwise
if (digitalRead(DT) != currentStateCLK) {
counter--;
} else {
// Encoder is rotating clockwise
counter++;
}
Serial.print("Position: ");
Serial.println(counter);
}
// Update the last state of CLK
lastStateCLK = currentStateCLK;
// Read the state of the push button
if (digitalRead(SW) == LOW) {
if (!buttonPressed) {
Serial.println("Button Pressed");
buttonPressed = true;
}
} else {
buttonPressed = false;
}
}
Noisy Signals:
Unstable Readings:
SW, CLK, and DT pins.Incorrect Direction Detection:
CLK and DT pins. Ensure they are correctly mapped in the code.By following this documentation, users can effectively integrate and utilize a rotary encoder with a button in their projects, ensuring accurate and reliable performance.