# problem with d flip-flop rotary decoder

Discussion in 'The Projects Forum' started by razie1, Jan 22, 2013.

1. ### razie1 Thread Starter New Member

Jan 22, 2013
8
0
hello
I'm currently trying to make a rotary decoder using a d-flipflop,
for now all i want is that if flash Led1 for left rotation and led2 for right.
The problem im getting is that when i rotate right, led 2 will flash like it's supposed to but led1 with also flash but very dim.

I hope someone can help me solve the problem?

Mike.

2. ### GopherT AAC Fanatic!

Nov 23, 2012
8,029
6,784
Start by putting a 330 to 470 ohm resistor in series with your LED. The output of the chip must be current limited and the LED must be current limited too. The LED will drop about 2 volts (red LED) and you will want to drive them at about 5 mA (from a Logic chip).

I'll look at your logic later (or someone else will likely post in the mean time).

3. ### WBahn Moderator

Mar 31, 2012
22,620
6,729
I'm assuming that the encoder is a quadrature encoder.

My quick gut reaction is that you are using one of the encoder outputs as your clock signal for your DFF and one of the reasons that quadrature encoders are used is that you can use the gray code behavior to deal with the bouncing outputs. But if you use a bouncing output as a clock signal, then your DFF will almost certainly see rising clock edges at both the rising and the falling edges of this signal.

4. ### razie1 Thread Starter New Member

Jan 22, 2013
8
0
ive added 330 resistors in series like you said which didn't have much effect on the rogue led flash just made it dimmer.

The encoder I'm using is a cheap mechanical quadrature and yes one one the outputs act as the clock.

so what would be the best way to remove or smooth out the bounce

5. ### WBahn Moderator

Mar 31, 2012
22,620
6,729
What are your design constraints? How many and what different kinds of parts are you allowed to use?

6. ### razie1 Thread Starter New Member

Jan 22, 2013
8
0
There are no design constraints, but would like to keep it simple as possible.

Mar 14, 2008
19,122
5,345
8. ### razie1 Thread Starter New Member

Jan 22, 2013
8
0
thanks i will try that out once i have ordered some 4011's

9. ### WBahn Moderator

Mar 31, 2012
22,620
6,729
I don't think so. If B is bouncing, then A is bouncing, too. The final outputs are generated by ANDing something with A, so if the other inputs is True and A is bouncing, then the output will bounce.

When I have used quadrature encoders it has been with a clocked system that sampled them periodically (fast enough to ensure that one of the phases wasn't missed). In that case, you don't care about the bouncing because it will resolve itself naturally. If you aren't sampling it, then it may be easiest to just debounce the signals. I'll have to think about that some more.

10. ### Brownout Well-Known Member

Jan 10, 2012
2,374
1,002
Pins 1 & 4 on your flip-flop should be connected to VCC.

11. ### crutschow Expert

Mar 14, 2008
19,122
5,345
After some thought I was able to use just one 7474 dual-D FF with no additional logic to do the quadrature detection while ignoring any switch bounce of the output that is changing states. It uses the D and CK inputs to detect the quadrature phase direction and the Reset inputs to generate the pulses for the phase direction. "A" blinks when the phase is A$\rightarrow$B and "B" blinks when the phase is B$\rightarrow$A.

The 7474 can be any standard or CMOS version of the device, subject to the voltage and logic ratings of the device.

Edit: Note that if the 7474 is a TTL device, then the input resistor to ground would need to be a smaller value to insure a logic low when the switches open. That value depends upon which type of TTL it is (standard, low-power, Schottky, etc.)

Last edited: Jan 24, 2013
Brownout likes this.
12. ### Brownout Well-Known Member

Jan 10, 2012
2,374
1,002
You genius bastard.

13. ### WBahn Moderator

Mar 31, 2012
22,620
6,729
I haven't looked at the details of the circuit yet, but my first reaction was, "Ugh. He's using asynchronous inputs and clocks together - recipe for disaster!". But as I was walking down the hall I was thinking about it and realized that this is an example of a situation in which playing this game can be both safe and elegant. The basic problem we are trying to address is that on the rising edge of each signal we don't care about bounces (multiple clock pulses don't hurt) but on the falling edge we don't want to see any of the bounces. But we can take advantage of the fact that the signals are in quadrature and use the other signal, along with state information, to hold the DFF in reset whenever we are at risk of seeing a falling edge in the other signal.

I'm not going to look at the details of your circuit until I use that notion to build my own and then compare the two.

14. ### crutschow Expert

Mar 14, 2008
19,122
5,345
There is one assumption I made about the bounce and that is, there is no bounce when the switch opens (which I believe is reasonable as bounce occurs from the contacts rebounding momentarily when they close). However if somehow there is a bounce when the switch opens then that would cause a circuit glitch (opposite LED turning on).

15. ### Brownout Well-Known Member

Jan 10, 2012
2,374
1,002
It really shouldn't because the D input should be stable due to the quadrature nature of the switch arrangement. So, you're double clocking a stable input, no glitch should result.

The only real hazzard was bouncing at release of the switch, when the input was in the opposite state, which you've fixed here.

Neither are the inputs and clocks asynchronous. The quadrature arrangement takes care of that.

16. ### razie1 Thread Starter New Member

Jan 22, 2013
8
0
Crutschow i tried the circuit yesterday and the leds chase each other, as if i just connected the leds directly to the encoder.

I don't know if it should be this way or that i just wired it wrong

17. ### crutschow Expert

Mar 14, 2008
19,122
5,345
Only one LED should blink for each direction of rotation. If not, then there is likely a wiring error or the encoder is outputting an incorrect signal.

18. ### razie1 Thread Starter New Member

Jan 22, 2013
8
0
Ok thanks i will double check my wiring.

19. ### razie1 Thread Starter New Member

Jan 22, 2013
8
0
Hi Crutschow I've double checked my wiring and still have the same outcome
so i think its down to the encoder
he is what the encoder outputs

left right
A-B A-B
0-0 0-0
0-1 1-0
1-1 1-1
1-0 0-1

20. ### crutschow Expert

Mar 14, 2008
19,122
5,345
Post the exact circuit you built.