The HW-040 Rotary Encoder is an electro-mechanical component that translates the angular position or motion of a shaft into analog or digital output signals. This encoder is particularly useful in applications where precise rotational feedback is required, such as in user input devices (e.g., volume controls, menu selection), robotics, industrial controls, and position monitoring.
Pin Number | Name | Description |
---|---|---|
1 | GND | Ground connection |
2 | + | Power supply (5V) |
3 | SW | Push-button switch output |
4 | DT | Data output |
5 | CLK | Clock output |
// Define the connections to the Arduino
const int pinCLK = 2; // Connected to CLK on the rotary encoder
const int pinDT = 3; // Connected to DT on the rotary encoder
const int pinSW = 4; // Connected to SW on the rotary encoder
// Variables to hold the current and last encoder position
volatile int encoderPos = 0;
int lastEncoderPos = 0;
// Variables to keep track of the state of the pins
int lastCLK;
int currentCLK;
// Setup function runs once at the start of the program
void setup() {
pinMode(pinCLK, INPUT_PULLUP);
pinMode(pinDT, INPUT_PULLUP);
pinMode(pinSW, INPUT_PULLUP);
// Attach an interrupt to the CLK pin
attachInterrupt(digitalPinToInterrupt(pinCLK), readEncoder, CHANGE);
// Initialize the serial communication
Serial.begin(9600);
// Read the initial state of CLK
lastCLK = digitalRead(pinCLK);
}
// Main program loop
void loop() {
// Check if the encoder has moved
if (encoderPos != lastEncoderPos) {
Serial.print("Position: ");
Serial.println(encoderPos);
lastEncoderPos = encoderPos;
}
// Check if the button is pressed
if (digitalRead(pinSW) == LOW) {
// Reset the encoder position to 0
encoderPos = 0;
Serial.println("Reset to 0");
// Wait for the button to be released
while (digitalRead(pinSW) == LOW) delay(10);
}
}
// Interrupt service routine for reading the encoder
void readEncoder() {
currentCLK = digitalRead(pinCLK);
// If the current state of CLK is different from the last state
// then a pulse occurred
if (currentCLK != lastCLK) {
// If the DT state is different from the CLK state
// then the encoder is rotating clockwise
if (digitalRead(pinDT) != currentCLK) {
encoderPos++;
} else {
// Otherwise, it's rotating counterclockwise
encoderPos--;
}
}
// Update lastCLK with the current state for the next pulse detection
lastCLK = currentCLK;
}
Q: Can the HW-040 Rotary Encoder be used with a 3.3V system? A: Yes, but the output signal levels will be lower, which may require level shifting for some 5V microcontrollers.
Q: How can I increase the resolution of the encoder? A: The HW-040 has a fixed resolution of 20 pulses per revolution. To increase the effective resolution, you can use gear reduction or employ software algorithms to interpolate between steps.
Q: Is it possible to use the HW-040 Rotary Encoder without an interrupt? A: Yes, but using interrupts is recommended for better performance, especially at higher rotation speeds. Without interrupts, you may miss pulses if the encoder is turned quickly.