
rotaryDecoderSwitch
Arduino library for a PCF8574 based rotary decoder - supports 2 rotary encoders with a switch.
Description
This library uses a PCF8574 to read the pulses of one or more rotary encoders.
As a PCF8574 has 8 lines up to 2 rotary encoders with a switch can be read over I2C.
The PCF interrupt line can be used to detect changes in the position of the encoders or if a switch is pressed.
If only 1 rotary encoders is connected one must use the lower bit lines as the
library assumes these are used.
Furthermore it is advised to connect the free PCF8574
pins to GND so you will not get unintended interrupts.
Note: This library works (limited) with the PCF8575 (lower half).
As always, feedback is welcome.
Double counting and mapping
Typical rotary encoders generate 1, 2 or 4 steps per click (see issue #18).
A code review shows this "double counting" is definitely generated by the
rotary encoder device.
The easiest way to handle a double step is add a divide by 2 or 4 e.g.
value = RE.getValue(re) / 2;
Since 0.4.2 the library provides getClicks() and a setStepsPerClick()
to handle the mapping from pulses to clicks internally.
getValue() still returns the (electrical) pulses, while getClicks()
returns the (physical) clicks.
Note the library allows a mapping per individual rotary encoder.
In #18 this problem is discussed (a bit) and in essence it can be interpreted
as a (minimal) mapping.
For non linear mapping, see e.g. - https://github.com/RobTillaart/multiMap
See also Interrupts section below.
Pull up resistors
To improve the quality of the pulses from the rotary encoder to the PCF8574
one should add pull up resistors on all input lines.
This includes the non-connected input lines to prevent interrupts due to noise.
The INT (interrupt) line must have a 10 kOhm pull up resistor.
The I2C lines SDA and SCL normally need pull up resistors.
Only if there are other devices on the same I2C bus, which already have a
pull up connected, there is no need to add extra resistors.
Typical values for pull ups for the input lines are 4.7 or 3.3 kOhm.
In case of (very) long wires the value may even be as low as 1 kOhm.
Related
PCF based rotary decoders
Supporting devices
Hardware
// connect up to 2 rotary encoders with a switch to 1 PCF8574.
//
// RotaryEncoder PCF8574 UNO R3 REMARKS
// -----------------------------------------------------------
// 1 pin A pin 0
// 1 pin B pin 1
// 1 switch pin 2 (switch to gnd)
// pin 3 (reserved)
// 2 pin A pin 4
// 2 pin B pin 5
// 2 switch pin 6 (switch to gnd)
// pin 7 (reserved)
//
// SDA A4
// SCL A5
//
Interface
#include "rotaryDecoderSwitch.h"
Constructor
- rotaryDecoderSwitch(const int8_t address, TwoWire *wire = Wire)
constructor to set the address and optional the Wire bus.
- bool begin(uint8_t deviceCount = 2) UNO ea. initializes the class.
deviceCount is the number of rotary encoders connected. (Max 2 per PCF8574)
Returns true if the PCF8574 address is found on the I2C bus.
- bool isConnected() returns true if the PCF8574 address is found on the I2C bus.
- uint8_t getRECount() returns number of rotary encoders from begin(),
convenience e.g. for for loops.
- void reset() reset all internal counters to 0.
Reads the device to update the last state.
- bool reset(uint8_t re) reset one rotary encoder counter to 0.
Reads the device to update the last state.
If re is out of range, false is returned, nothing is changed.
Direction
Not all rotary encoders are created equal, the direction they use as
"increase / positive" may differ.
Since 0.4.3 the library supports changing the "count" direction.
- bool setDirection(uint8_t re, uint8_t dir = 0) dir = 0 is default.
- uint8_t getDirection(uint8_t re)
Note: if one changes the direction there is no reset of the internal counter.
The user needs to call reset(re) manually to start with zero again.
Note: If one wants a permanent swapping of the direction one can swap
the A and B lines of the rotary encoder.
Core functions
- uint8_t readInitialState() read the initial state of the 2 rotary encoders.
Typically called in setup only, or after a sleep e.g. in combination with setValue().
This function returns the read state, saves an additional read8() call.
- bool checkChange() used for polling to see if one or more rotary encoders have changed.
This function does NOT update the internal counters.
- bool update() returns true if there is a change detected.
It updates the internal counters of the rotary encoder.
The counters will add +1 or -1 depending on rotation direction.
Need to be called before getValue() or before getKeyPressed().
Note that update() must be called as soon as possible after the interrupt occurs
or as often as possible when polling.
Returns false if there is no change since last read.
- bool updateSingle() returns true if there is a change detected.
It updates the internal counters of the rotary encoder.
This will add +1, +2 or +3 as it assumes that the rotary encoder
only goes into a single direction.
Typical use is for a RPM measurement.
Note that the getValue() can go 3x as fast if you turn in the opposite direction.
Returns false if there is no change since last read.
Note: a mix of single direction and bidirectional is not expected to work.
Note: update() also returns true if the device has made +2 or -2
steps. As it is undecidable the internal counter is not changed.
Technically it is possible that between last read 2 steps are made,
however chance is small unless the rotary encoder does make a lot of RPM.
Counters & keypresses
- int32_t getValue(uint8_r re) returns the rotary encoder (electric) pulse counter.
If the parameter re > 1 then 0 is returned.
The maximum value is � 2.147.483.647.
- bool setValue(uint8_r re, int32_t value = 0) (re)set the internal counter to value, default 0.
If the parameter re > 1 then false is returned, true otherwise.
- bool isKeyPressed(uint8_t re) returns true if the switch is pressed of the RE selected (re = 0 or 1).
Note one needs to call update() first!
Clicks
The clicks interface is functional a wrapper around the value functions.
- int32_t getClicks(uint8_t re) returns the rotary encoder
(physical) clicks counter.
Note one has to configure setStepsPerClick() correctly.
If the parameter re > 3 then 0 is returned.
The maximum value is � 536.870.911.
- bool setClicks(uint8_t re, int32_t clicks = 0) (re)set the internal counter to value, default 0.
- bool setStepsPerClick(uint8_t re, uint8_t spc) set the (electric) steps per (physical) click.
Typical values for rotary encoders are 1, 2 and 4.
If re > 3 or spc == 0 the function returns false.
- uint8_t getStepsPerClick(uint8_t re)
Returns the configured steps per click (default 1).
Read1 - Write1
Warning
The write1(pin, value) might alter the state of the rotary encoder pins.
Think of effect on the INT(errupt) pin.
So this functionality should be tested thoroughly for your application.
Especially the write1() is experimental, feedback welcome.
Read1() and write1() are functions to access the pins of the PCF8574 that
are not used for rotary encoders.
The user must guard that especially writing the pins do not interfere with the
rotary encoder pins.
The parameter pin is not verified if it is within range (0..7).
- uint8_t read1(uint8_t pin) reads a single pin (0..7).
Returns HIGH or LOW.
- bool write1(uint8_t pin, uint8_t value) writes a single pin (0..7).
Value should be LOW (0) or HIGH (other values).
As said before the user must guard not to interfere with the
rotary encoder pins.
- uint8_t read8() read all pins in one I2C IO action. When one need to access multiple
input pins this is faster but need some bit masking.
- bool write8(uint8_t bitMask) writes to multiple pins at once, e.g. to control multiple
LEDs in one IO action. As said before the user must guard not to interfere with the
rotary encoder pins.
A typical use-case is to use the 3 lower pins for 1 rotary encoder,
and the upper 5 for other IO.
Debugging
- int8_t getLastPosition(uint8_r re) Returns last position of the rotary encoder.
Returns 0 if re > 1.
Performance
As the decoder is based upon a PCF8574, a I2C device, the performance is affected by the
clock speed of the I2C bus.
All four core functions have one call to read8() which is the most expensive part.
Early tests gave the following indicative times (Arduino UNO) for the update()
function (with no updates it is ~8 us faster).
Note that above 500 KHz the gain becomes less while reliability of signal decreases.
(500 KHz is about 3x faster than 100 KHz in practice.)
As 400 KHz is a standard I2C clock speed it is the preferred one.
Note Teensy 4.1 seems to support only some distinct speeds.
| I2C speed |
time (us) |
delta |
%% |
Notes |
| 100 KHz |
234 |
|
|
|
| 200 KHz |
136 |
98 |
42% |
|
| 300 KHz |
100 |
36 |
26% |
|
| 400 KHz |
85 |
15 |
15% |
preferred max |
| 500 KHz |
78 |
7 |
8% |
|
| 600 KHz |
67 |
11 |
14% |
(strange outlier) |
| 700 KHz |
63 |
4 |
6% |
|
| 800 KHz |
- |
- |
-% |
|
(test results differ slightly from the rotaryEncoder class)
At 400 KHz it can update 2 rotary encoders in less than 90 us.
At a 50% update percentage this implies a max of about
5000++ update() calls per second in theory.
to be tested in practice
Note that a high speed drill goes up to 30000 RPM = 500 RPS = 2000 interrupts per second,
assuming 4 pulses == 360�. (not tested)
Interrupts
The PCF8574 has an INT pin which can be used to detect movement of one
of the connected rotary encoders.
In issue #5 (rotaryDecoder8) it became clear that some (mechanical) rotary
encoders can give more than one pulse per tick.
In issue #5 the pulse per tick ratio was 4.
This is a point of attention for your project if it is interrupt based.
The example rotaryDecoderSwitch_demo_interrupt.ino shows a possible solution
how to handle these multiple interrupts per tick.
A static counter in the interrupt handler only sets the "new data" flag to read
the device every n-th interrupt.
If you have different rotary encoders with different pulses per tick ratio's
one should use the greatest common divider of the ratios.
Ultimo this can imply that the interrupt handler should set the flag every
interrupt to prevent missing ticks.
Also if you want to be able to turning multiple rotary encoders simultaneously,
there is a (small) chance that two (or more) pulses share the same interrupt.
In a scenario with 2 rotary encoders, one could e.g. have only 7 interrupts instead of 8.
To guarantee the capture of every change one has to set the pulses to tick ratio
to 1. This causes extra reads (== time/overhead) that show no movement.
Note that if other devices e.g. switches are connected to the PCF8575,
there can be additional interrupts generated.
In such scenario setting the pulse to tick ratio to 1 is also the safest
way to capture all changes.
Future
Must
- update documentation
- keep in sync with rotaryDecoder library
Should
Could
Wont
Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,