Description
Rotary encoders are continuously variable position sensors that reports the relative position of the shaft and the direction of rotation. The built-in switch can be used to perform some action such as locking in settings that were selected by the rotary encoder.
These modules use two high quality Bourns PEC11R-4220K-S0024 vertical mount rotary encoders with switch, 24 detents per rotation and a knurled shaft.
PACKAGE INCLUDES:
- PSB-18 Dual Rotary Encoder module configured for either:
- PSB-18B breadboard style with straight connector, nuts/washers, 7-pin housing
- PSB-18S stand-along style with right-angle connector, standoffs, nuts/washers, 7-pin housing
KEY FEATURES OF DUAL ROTARY ENCODER:
- Reports relative position of the shaft
- Reports the direction of rotation of the shaft
- 360 degree continuous rotation
- Pushbutton pulls to ground
- 3.3 and 5V compatible
These encoders, often called rotary encoders, are popular for use as input devices. They can continuously rotate in both directions and output a waveform that allows the microcontroller to determine both the direction of rotation and the number of ‘clicks’ in that direction. The clicks are a slight detent that can be felt as the rotary encoder shaft is rotated. The microcontroller can also determine the speed of the rotation which might be useful for some applications. It is important to note that the rotary encoder does not provide any indication of its current position like a potentiometer can. It instead provides information on its relative movement.
Common applications include using them to navigate and select options in on-screen menus, as game input controllers or input control for robotic applications. We use two of these modules in our AudioPlatform to access and control all functions of that audio/MIDI device.
The knurled shaft provides good finger feel during use of the encoders, but if a more substantial feel/look is desired a standard potentiometer type of press-on knob can be added to the shaft. Just make sure that they are designed to work with a knurled shaft instead of the D-shape shaft commonly seen. Some knob options are listed below.
The breadboard style of module will plug into a solderless breadboard for making easy easy connections and the standoff style works well when working without a breadboard. In this case, the 7-pin housing is typically used with standard Dupont style breadboard jumpers of choice to connect the module to the microcontroller.
Both can be used for permanent mounting depending on whether you want the header to come straight out the back of the module or come out at a right angle.
Both versions come with washers/nuts on the rotary encoders and also come with the 7-pin housing for maximum flexibility.
Dual Rotary Encoder Module with 25mm long shafts
The standard rotary encoders have a 20mm long shaft. We also have longer 25mm rotary encoders that the modules can be assembled with for the same price.
The longer shaft can be useful in some stack-up situations or if you just like to have a longer shaft to work with. An example of the longer shaft is shown to the right. Both versions of the module can be made with these longer 25mm shafts. To order the longer version, just make a note in the Order Notes section during Checkout to “Please use 25mm rotary encoders”.
These will typically ship by the next business day.
Encoder Theory of Operation
The rotary encoder is an incremental encoder which means that it outputs a pulse or pulses each time that the shaft is rotated one detent (click). The position is kept track of relative to the position that the shaft was in when the controller is first powered on, so it does not have a sense of a specific physical position, but from the software perspective it knows it has gone 5 detents from where it started.
It is continuously variable which means you can continue to spin the shaft around in one direction or the other for as long as you want. It has no mechanical stop.
In addition, the encoder can also detect whether the shaft is being turned in the clockwise (CW) or counterclockwise (CCW) direction. Direction of rotation is determined by providing 2 separate pulse outputs that are 90 degrees out of phase with each other. Comparing the 2 outputs to see if they are equal or not when the A changes state tells you if the shaft was rotated CW or CCW as shown in the drawing below. The output speed of the pulses can also be used to determine how fast the rotary encoder is being turned which is useful in some applications.
Rotary encoder outputs are typically labeled as A and B but you will also sometimes see them labeled or referred to as CLK (Clock) and DT (Direction of Travel) respectively.
The A or ‘CLK’ output is the primary output pulse for determining the amount of rotation. Each time the shaft is rotated by one detent in either direction, the A output goes through one cycle of going HIGH and then LOW. There are 24 detents in a full rotation of the shaft so there would be 24 complete clock cycles. To confuse matters, the output may actual cycle 4 times per detent and in that case you divide the output pulse by 4 if you want 1 pulse per detent.
The B or ‘DT’ (Direction of Travel) output is the same as the A output, but it lags the A by a 90 degree phase shift. This output can be used to determine if the shaft is being rotated in a CW or CCW direction by the state of B when the A changes state. Simply put, if when the A changes state B = A, then the shaft was turned CCW. On the other hand if A changes state and B != A then the shaft was turned CW. Graphic below may help to clarify the logic.
Note that these outputs are simply connections to switches that switch between being closed (connected to ground) and being open. To pull the signal high, the pins used to read the rotary encoder outputs must have pullup resistors installed. Some modules available on the market have external pullups installed, but these really aren’t needed as virtually all microcontrollers can enable built-in pullup resistors on their pins that are used as inputs.
If this all sounds a bit complicated, there are some nice libraries available for common microcontrollers that hide all the messy details and we will use one of those in our example below. If you do want to try to program these at the hardware level, you can refer to some of the examples provided with our other rotary encoder options. The illuminated rotary encoder shows an interrupt based software example link and the basic rotary encoder module shows a polled example link.
Module Connections
There are 7-pins on the module with the following connections:
- A1 = Encoder 1 A/CLK
- B1 = Encoder 1 B/DT
- SW1 = Encoder 1 switch
- A2 = Encoder 2 A/CLK
- B2 = Encoder 2 B/DT
- SW2 = Encoder 2 switch
- GND = Ground
All connections except ground go to microcontroller pins set as inputs with pull-up resistors enabled. The ground pin must be connected to the microcontroller ground.
OUR EVALUATION RESULTS:
These are very useful for a number of different types of user input applications and are fun to work with.
The example program below uses the EncoderTool.h library which can be used with Teensy, Arduino and ESP32 microcontrollers. This library by Luni64 (Lutz Niggl) is a very powerful library with a lot of nice advanced features like automatically debouncing switches. The library can be added via the IDE library tool and is also found on GitHub here.
We happen to be using the breadboard compatible version of the module with our Mini Platform for Teensy 4.1 in our example here.
We are using the polled version of the library, but it also supports interrupts. Teensy provides interrupt support on all pins, so connecting to pins is pretty arbitrary, but if using something like an Arduino, only some pins support interrupts, so watch for that. With the polled setup used in this example, pretty much any pins should work.
This example reads both rotary encoders and sends the rotational information to the serial monitor. The switches reset the value back to 0.
Make the following connection or adjust the pin numbers to match your particular setup:
- A1 Connect to pin 41
- B1 Connect to pin 40
- SWT1 Connect to pin 39
- A2 Connect to pin 38
- B2 Connect to pin 27
- SWT2 Connect to pin 26
- GND Connect to microcontroller ground
Dual Encoder Example Program.
/* Dual Rotary Encoder Example * * The Left/Right encoders increment or decrement numbers on the serial monitor * The encoder pushbuttons reset the their value back to zero * This program uses the EncoderTool.h library downloadable from the IDE */ #include <EncoderTool.h> using namespace EncoderTool; // Instantiate 2 encoders as polled devices PolledEncoder knobLeft; PolledEncoder knobRight; // Assign pin number used for the encoders constexpr uint8_t A1_PIN = 41, B1_PIN = 40, SW1_PIN = 39; // Left Encoder constexpr uint8_t A2_PIN = 38, B2_PIN = 27, SW2_PIN = 26; // Right Encoder //=============================================================================== // Initialization //=============================================================================== void setup() { Serial.begin(9600); Serial.println("Two Rotary Encoder Test:"); // Initialize encoders. knobLeft.begin(A1_PIN, B1_PIN, SW1_PIN); knobRight.begin(A2_PIN, B2_PIN, SW2_PIN); } //=============================================================================== // Main //=============================================================================== void loop() { knobLeft.tick(); // tick updates the rotary encoder info. knobRight.tick(); // Check to see if either of the knobs were turned if (knobLeft.valueChanged() || knobRight.valueChanged()) { Serial.print("Left = "); Serial.print(knobLeft.getValue()); Serial.print(", Right = "); Serial.println(knobRight.getValue()); } // Check to see if either button is pressed if (knobLeft.buttonChanged() || knobRight.buttonChanged()) { if (knobLeft.getButton() == LOW && knobLeft.getValue() != 0) { knobLeft.setValue(0); Serial.print("Left = "); Serial.print(knobLeft.getValue()); Serial.print(", Right = "); Serial.println(knobRight.getValue()); } if (knobRight.getButton() == LOW && knobRight.getValue() != 0) { knobRight.setValue(0); Serial.print("Left = "); Serial.print(knobLeft.getValue()); Serial.print(", Right = "); Serial.println(knobRight.getValue()); } } }
Notes:
- None
Technical Specifications
Dimensions | PCB | 57.2 x 20.3mm (2.25 x 0.8″) |
Datasheet | Bourns | PEC11R-4220K-S0024 |
These modules were originally designed to fit an existing application, so the dimensions are not on convenient spacing. This doesn’t really impact the intended free-standing usage shown here, but the details are shown below if they are needed.