Rotary Encoder Demo
The following demo is based on a 5V EC11 rotary encoder, which incorporates a push-to-make switch. The demo shows how to connect the encoder to an Arduino Uno.
The EC11 has five pins, as illustrated in the following photo:
The pins are, left to right:
- GND – ground connection.
- S1 – signal pin 1.
- S2 – signal pin 2.
- KEY – connected to the built in switch.
- 5V – 5V input.
When the rotary encoder's shaft is rotated the signal pins are set high and low in a sequence that indicates whether the shaft is being rotated clockwise (CW) or counter-clockwise (CCW). To detect the rotation wait for pin S1 (or pin S2) to change state. If S1 and S2 have different states then the rotation is CW. If the state of S1 and S2 is the same then the rotation is CCW.
The KEY pin is normally held high. When the encoder's built in switch is pressed the KEY pin is set low for as long as the switch is closed.
The following diagram shows how the rotary encoder is connected to an Uno. LEDs are used to indicate which direction the shaft is being turned and when the built in switch is pressed. The cathode of each LED is connected to ground via a 470Ω resistor.
The Uno is connected as follows:
Arudino pin | Attached to |
---|---|
5V | Rotary encoder 5V pin |
GND | Rotary encoder GND pin & the resistors connected to the LED cathodes. |
3 | Rotary encoder KEY pin |
4 | Rotary encoder S1 pin |
5 | Rotary encoder S2 pin |
8 | Anode of the LED used to indicate CCW rotation |
9 | Anode of the LED used to indicate CW rotation |
10 | Anode of the LED illuminated when the switch is pressed |
Note that the Uno digital pin connections used above are arbitrary: any digital pins can be used providing the demo code is changed accordingly.
The demo source code is available from the cahamo/demo-projects
repository on GitHub. The relevant code will be found in the rotary-encoder--uno
directory. All the C++ code is in rotary-encoder--uno/src/main.cpp
. The source is a modified version of rot-encode-demo.ino
taken from the DroneBot Workshop website. The changes made were to (a) rename some variables to match the names of the pins of the EC11 encoder and (b) to detect when the encoder's switch is pressed.
This demo is a PlatformIO project for use with Visual Studio Code. It can be modified for use with the Arduino IDE simply by copying the code from rotary-encoder--uno/src/main.cpp
into a new, blank Arduino API project. No Arduino libraries are required.
Once compiled and uploaded to the Uno, the code works as follows:
- Turning the rotary encoder shaft CCW lights the middle LED.
- Turning the shaft CW lights the right hand LED.
- Pressing down on the shaft closes the switch and lights the left hand LED.
In addition the code maintains a counter that is incremented when the shaft is turned CW and decremented when the the shaft is turned CCW. The direction the shaft is turned and the current counter value are written to the Uno's serial port. When the switch is pressed, "Switch pressed" is written to the serial port.
Finally, here's a video of the demo running. Note that colours of the LEDs do not match those shown on the diagram above.