Debouncing rotary encoder circuits

Click For Summary

Discussion Overview

The discussion revolves around debouncing rotary encoder circuits, particularly in the context of a Raspberry Pi project utilizing a Teensy++ microcontroller. Participants explore both hardware and software approaches to effectively manage signal noise and ensure accurate readings from rotary encoders.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their circuit design for debouncing rotary encoders, including the use of a resistor, capacitor, and diode, and seeks advice on capacitor ratings to filter out switch bounce while maintaining responsiveness.
  • Another participant shares a software debouncing method that involves using a timer and buffer bytes to establish a "debounced value" based on a defined constant.
  • A participant explains the behavior of A-B output encoders, noting that noise can cause transitions between states and suggesting that faulty encoders should be replaced.
  • Further elaboration on the differences between quadrature and absolute encoders is provided, with a focus on how each type handles contact bounce and noise.
  • One participant proposes a specific algorithm for cleaning up Gray code from absolute encoders, detailing the logic operations involved in the process.

Areas of Agreement / Disagreement

Participants express varying opinions on the effectiveness of hardware versus software debouncing methods. There is no consensus on the best approach, and multiple models for handling encoder noise are discussed.

Contextual Notes

Some participants highlight the importance of encoder type (quadrature vs. absolute) in relation to noise sensitivity, and there are mentions of potential issues with faulty components. The discussion includes various assumptions about the behavior of encoders and the effectiveness of proposed solutions.

Who May Find This Useful

Individuals interested in electronics, particularly those working with rotary encoders in projects involving microcontrollers and signal processing, may find this discussion beneficial.

shushi_boi
Messages
44
Reaction score
0
Hi,
In this Raspberry Pipboy Project,


Niel Corbett attempted to recreate the pip boy as a real life functional device, and in this project he is using two rotary encoders.

Here' the link to the code for his project,
https://bitbucket.org/selectnone/ra...ls.ino?at=master&fileviewer=file-view-default

x9foObt.png


and the micro controller that he is using for this project is the Teensy++ 2 [uses 5V for its pins] which is connected to the Raspberry Pi (although currently he decided to just us the Raspberry Pi GPIO i/o pins directly instead).

So, I was interested in taking the circuitry for the rotary encoders a step further and I'm trying to debounce the circuit.
Generally this is how one wires an encoder's circuit in order to debounce it;
+5V > Resistor > Encoder + Teensy > Capacitor > Ground

Based off of that, I drew this schematics on how I believe this circuit should look.
qbEBf2g.png


For the Resistor, I am using a 10K Ohm 1/2 Resistor, and between the capacitor and the encoder, I am using an 1N4001 Diode
http://www.engineersgarage.com/electronic-components/1n4001-diode

Based on how I have the circuit set up, does this circuit appear to work?

Also, about the polarized capacitor that I am using, I'm not too sure on what specific ratings to look for on capacitors for this project as I hardly ever deal with them as a hobbyist. In order to find a capacitor that will make the rise time slow enough so as to filter out switch bounce but also fast enough to allow the encoder to work fast enough, what kind of ratings should I look for in a capacitor for this project?

Thank you for your time, and I apologize for asking, I'm barely new to electronics and I have a passion to learn about them :)
 
Engineering news on Phys.org
I have used software debouncing in a couple of projects. It goes somewhat like this:
  • Set up a timer to generate clock interrupts each millisecond or so
  • Assign one buffer byte and a "debounced value" byte to each input
  • Decide on a "debouncing constant" (how many reads should be equal before you accept the input value). A value of 5 is usually OK.
  • At each interrupt, shift the buffer bytes left and insert the value of the corresponding input into the LSB
  • Whenever the "debouncing constant" number of bits agree in the buffer bytes (all 1's or all 0's), update the "debounced value" byte
 
  • Like
Likes   Reactions: shushi_boi and jim hardy
Firstly; as an A–B output encoder is rotated it generates two approximate square waves that are phase shifted by about 90° like sine and cosine. Any noise due to sitting on the edge of a transition will simply jump backwards and forwards between two adjacent states.
Secondly; any contact noise in an A–B encoder, on either the A or the B contact will do the same as above.
Contact noise on two outputs would suggest you should replace the encoder.

If you are polling the A–B signals, then noise could waste some processor time handling irrelevant transitions.
If you use interrupts to detect changed A–B state, then the processor could run out of time handling noise.

The software de-bounce solution is counter-intuitive and avoids time constants.
It works by updating virtual copies of the A and B status in an internal register.
When the external A signal changes state, read the status of signal B and copy that to the virtual B register.
When the external B signal changes state, read the status of signal A and copy that to the virtual A register.

Repetition of edge oscillation or contact transition bounce will then make no change to the internal virtual registers.
The processor need only change the position count register when an internal virtual A or B register changes.
There will be a maximum delay of one quarter of a cycle in the encoder position appearing in the virtual register.

I have done the same thing externally in hardware by using 'D' flip-flops and XOR gates to clean up a signal.
 
  • Like
Likes   Reactions: jim mcnamara, nsaspook and shushi_boi
Baluncore said:
Firstly; as an A–B output encoder is rotated it generates two approximate square waves that are phase shifted by about 90° like sine and cosine. Any noise due to sitting on the edge of a transition will simply jump backwards and forwards between two adjacent states.
It depends on the type of encoder. The quadrature encoder (which you are discussing) is not very sensitive to contact bounce, but an absolute encoder may get into trouble.
 
Svein said:
The quadrature encoder (which you are discussing) is not very sensitive to contact bounce, but an absolute encoder may get into trouble.
Faulty components should be replaced.

An absolute encoder will use some form of Gray code. The elegant thing about a Gray code is that, like the quadrature encoder, only one bit ever changes at the time.
https://en.wikipedia.org/wiki/Gray_code#Position_encoders

Jitter and noise removal from an absolute encoder that uses a Gray code follows exactly the same process as is used with the quadrature encoder. When an input bit changes, sample all the other bits that did not change. That is easily done with a microcontroller using “word wide” boolean operators XOR, AND and OR to detect, mask and write the parallel bits into the virtual register.

The stable Gray code can be converted to a binary number by the same microcontroller using the XOR and shift operations.
https://en.wikipedia.org/wiki/Gray_code#Converting_to_and_from_Gray_code
 
Here is an algorithm that will clean up the Gray Code from an absolute encoder with a maximum lag of one step. It will also debounce or debobble two bit A~B code.

The Gray code input now is x_now. The previous code was x_was, the clean position register is master. x_diff will be the bits that have changed, zeros will be the 0s to insert, ones the 1s to insert.

x_diff = x_was EXOR x_now
If x_diff is zero you can stop processing here.
zeros = x_now OR x_diff
ones = x_now AND NOT( x_diff )
master = master AND zeros
master = master OR ones
x_was = X_now

Done.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
Replies
2
Views
6K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
6
Views
9K
  • · Replies 3 ·
Replies
3
Views
4K