Working voltage: 5V or 3.3V
Pulses per rotation: 20
Dimensions
Pinout
1...GND
2...+5V or +3.3V supply
3...SW connects pin to GND when shaft is pressed
4...DT direction pulse. Pin has 10K pullup connected to supply.
5...CLK clock pulse. Pin has 10K pullup connected to supply.
Example Arduino Sketch:
- /* FILE: Rotary_Encoder_Example
- DATE: 31/03/23
- VERSION: 1.0
- AUTHOR: Andrew Davies
- BY: HobbyComponents.com
- 31/03/23 version 1.0: Original version
- This sketch uses the pin interrupt feature of the Arduino to automatically
- record the state of the encoder when it is rotated. The result will be stored
- as a position in the variable 'pos'. To use this sketch you will need
- the following rotary encoder:
- https://hobbycomponents.com/sensors/502-ky-040-rotary-encoder-module
- Connect it to your Arduino as follows
- Encoder.....Arduino
- CLK.........3
- DT..........2
- SW..........4
- +...........5V or 3V depending on your Arduino
- GND.........GND
- This sketch is provided free to support the open source community.
- PLEASE SUPPORT HOBBY COMPONENTS so that we can continue to provide free content
- like this by purchasing items from our store -
- HOBBYCOMPONENTS.COM
- You may copy, alter and reuse this code in any way you like, but please leave
- reference to HobbyComponents.com in your comments if you redistribute this code.
- This software may not be used directly for the purpose of selling products that
- directly compete with Hobby Components Ltd's own range of products.
- THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
- EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
- HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
- INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
- REASON WHATSOEVER.
- */
- /* Define digital pins used to read the encoder */
- #define DT 2
- #define CLK 3
- #define SW 4
- // Delay in ms to wait for encoder contacts to settle
- #define DEBOUNCE_DELAY 5
- byte pos = 0, lastPos = 0, lastState;
- void setup()
- {
- Serial.begin(9600);
- pinMode(SW, INPUT_PULLUP);
- pinMode(CLK, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(CLK), encoder, CHANGE);
- lastState = (digitalRead(DT) << 1) | digitalRead(CLK);
- }
- void loop()
- {
- // Check if encoder position counter has changed
- if(pos != lastPos)
- {
- Serial.println(pos);
- lastPos = pos;
- }
- // Check if encoder is pressed
- if(digitalRead(SW) == false)
- Serial.println("Pressed!");
- delay(100);
- }
- // Interrupt service routine to read the encoder state
- void encoder()
- {
- // Wait for encoder contacts to settle
- delay(DEBOUNCE_DELAY);
- // Read the encoder outputs
- byte state = (digitalRead(DT) << 1) | digitalRead(CLK);
- // If the state has changed then update the counter
- if(state != lastState)
- (state == 3 || state == 0) ? pos-- : pos++;
- lastState = state;
- }
Libraries, example code, and diagrams are provided as an additional free service by Hobby Components and are not sold as part of this product. We do not provide any guarantees or warranties as to their accuracy or fitness for purpose.