esp:esp32:esp-idf:rotery-encoder
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| esp:esp32:esp-idf:rotery-encoder [2024/10/09 17:16] – oscar | esp:esp32:esp-idf:rotery-encoder [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== ESP32 - Rotary Encoder ====== | ||
| - | |||
| - | ---- | ||
| - | //How to reliably read rotary encoder on ESP32?// | ||
| - | ===== Background ===== | ||
| - | For usage on my ESP32 projects, I ordered several rotary encoders from Ali Express. It appeared that all of these gave many erratic and unreliable readings. Indicating CW when turning CWW and visa versa. Approximately out out the 20-30 readings was erroneous. | ||
| - | Initially I assumed contact bouncing, but the problem persisted after implementing an anti-bounce filter in the software. On a Raspberry this problem does not exist, so it seems that the ESP32 does not work well with this type of rotary encoders. | ||
| - | This page describes both cause and solution for this problem. | ||
| - | |||
| - | {{ : | ||
| - | |||
| - | ===== Problem cause ===== | ||
| - | It appears that the encoders I bought have RC filters on their output connections. Apparently, with the intention to eliminate contact bounce. However, side effect of this is that the rise and decay times of the rotary pulses do not have sharp edges, but curved edges. The image below is borrowed from www.allaboutcircuits.com (see links below) illustrates this. The ESP32 interrupt mechanism is not able to handle well interrupts and readings on slow edges. This resulted in erroneous readings. E.g. interrupts on negative edges showed a gpio value of 1 in the interrupt routine, where 0 is expected. | ||
| - | |||
| - | {{ : | ||
| - | |||
| - | ===== Hardware Modifications ===== | ||
| - | The rotary encoders I bought appeared to have resistors of 10k in series with the rotary contact (value of R in picture above). After replacing these resistors to 501 Ohm, the readings improved significantly. | ||
| - | The positions for S1 and S2 contacts on the back of the encoder are marked with red circles in the first image. | ||
| - | |||
| - | ===== Reading Method 1 ===== | ||
| - | This is the most simple approach, requiring only 1 interrupt (either on S1 or S2). It appeared to work flawless. | ||
| - | |||
| - | ===== Reading Method 2 ===== | ||
| - | This is a more complete/ | ||
| - | |||
| - | {{ : | ||
| - | |||
| - | Connecting S1 and S2 to 2 GPIO pins with interrupts triggered on both edges. With this we can track both S1 and S2 signals and detect a rotation when an expected sequence is received. As it can be seen from the waveform diagram, a clockwise motion generates A = …0011… and B = …1001… When we record both of the signals in bytes seqA and seqB by shifting in the last reading from right, we can compare these values and determine a new rotational step. | ||
| - | |||
| - | |||
| - | |||
| - | ===== Links ===== | ||
| - | * [[https:// | ||
esp/esp32/esp-idf/rotery-encoder.1728494206.txt.gz · Last modified: by oscar
