What is a Rotary Encoder,
Also known as a “Shaft” encoder, there are many versions that operate under this name.
The type I am referring to here is a mechanical device that generates two switching pulses 90 deg out of phase from each other.
The number of pulses per revolution varies depending on brand and make etc.
Two common examples are,
The following example is the bare bones encoder. These come in 2 types, a 3 pin and 5 pin.
Below shows 5 pins, the extra 2 pins shown on the other side of the device, serves as a momentary switch which is activated by depressing the shaft.
If using this type, it is recommended to apply a simple circuit for preferred operating function.
(circuit shown below A, B and C for rotary function, the other 2 for switching, just omit the switching section for a 3 pin encoder)
The following type is a 5 pin encoder already soldered onto a circuit for preferred operating function.
As you will see, it even shows what to plug in.
Both versions are plentiful on website online, and may even be available in your local electronics store.
Especially note if you require it to have the momentary switch option.
One difference you may notice between the two versions is the shaft design.
The bare bones version shows a slotted shaft with a threaded base for mounting into hardware, the soldered circuit version shows a “D” shape shaft with no threaded base, but has two mounting holes in the circuit board.
Be mindful of this when purchasing your items to ensure it meets your mounting requirements.
How do they work.
The below diagram shows the workings by depending on the direction the shaft is turned, when signal “A” received a rising edge,
It reads wether signal “B” is either HIGH or LOW.
In the diagram below, when signal “B” is HIGH, it is turning clockwise, when signal “B” is LOW, it is turning anti-clockwise,
Connecting to the Axoloti
This connecting a rotary encoder to the Axoloti will require two digital GPIO Input pins to read the rotating value,
And one additional digital GPIO pin to read the momentary switch if you choose to connect one.
Based on the encoder and circuit images above, connections are described as follows..
CLK – “Clock” used to trigger the rising edge.
DT – “Data” used to determine the direction delivering a HIGH or LOW signal during CLK rising edge.
SW – “Momentary Switch” depending on your encoder you may or may not have one.
+ – “Voltage Supply” With Axoloti this is 3.3v from a supply pin.
GND – “Ground”
CLK, DT and SW will each require their own digital GPIO pin, with “+” and “GND” connected as standard to 3.3v and GND pins.
Be mindful of using any analogue pins that you may require for any analogue readings.
When selecting the pin number in the GPIO digital Input object, be sure to select as “PULLDOWN”
The Patch, Object and Code
This section could go into various directions and create multiple debates about certain setups.
There are many de-bounce concepts, acceleration methods meaning the faster you turn the encoder, the higher the steps, and also many interrupt methods to support these ideas.
I will focus only on what appears to be the most common basic code setup which is how the object “Rotary Encoder Basic” is coded.
Rotary Encoder Basic.axo (1.1 KB)
Giving you the option to make whatever changes meet your needs.
Add the GPIO digital Input objects for the pins connected to “CLK” and “DT”
Object contains two inputs “trig” for the rising edge trigger, “dir” for determining the direction it is turning.
In the Patch, connect object connected to “CLK” to encoder object input “trig” and “DT” to “dir” respectively.
Contains only one output which is the current changing value weather decreasing or increasing.
The output can be connected to an Factory/disp/“i” object to display the value.
You can either modify the object used in the above explanation, or use the following code to help develop your own object.
Inlets:
Inlet_trig – (bool32.rising)
Inlet_dir – (bool32.rising)
Outlets:
Outlet_o – (int32)
Local Data:
int encoderPos;
int encoderLast;
int inval;
Int Code:
encoderPos = 0;
encoderLast = 0;
inval = 0;
K-rate code:
inval = inlet_trig;
if ((encoderLast == 0) && (inval > 0)) {
if (inlet_dir == 0) {
encoderPos--;
} else {
encoderPos++;
}
}
encoderLast = inval;
outlet_o=encoderPos;
Summary
Now that you have the basic setup, you can modify to whatever meets your needs.
You will see over time various versions appear in my contributions once it is up and running.
Some things you may want to consider,
- Minimums and Maximums.
- How to apply presets.
- Increment / Decrement size.