# Telling which way a KY-040 Rotary Encoder has been turned

I am stripping this problem down to the bare minimum.

I have KY-040 Rotary Encode, When it is turned clockwise I want a blue LED to blink for every increment it is turned. When it is turned anti clockwise I want a red LED to blink for every incremented is turned.

I can have anything between them (but not a PI or arduino type barebone computer)

I have looked at flip flop chips but with the wave signal that comes out of the KY-040 Rotary Encoder I got quickly confused.

• If you already have the code, why wouldn't you just flash the LEDs from the code? – Dave Tweed Mar 6 '17 at 12:57
• Easy. It has 2 outputs. One leads the other turning one way, and lags when turning the other way. If that's not enough hint, any rotary decoder interface circuit should show you how to do it. – Brian Drummond Mar 6 '17 at 12:57
• Hate to shock you Brian, but I have never knew rotary decoder interface circuit were a thing. – Ashley Kilgour Mar 6 '17 at 13:05
• cardinal rule: No datasheet ⇒ No sale – Nick Alexeev Mar 14 '17 at 17:15
• Well, you seem to understand rotary encoders as you could do this in software eight days ago http://electronics.stackexchange.com/questions/290679 - albeit with a yellow LED instead of a red one. – Finbarr Mar 14 '17 at 17:21

probably the 2 outputs have a quarter pulse lenght shift. Use one output as the known amount rotation indicator (=every rising edge means a known angle increment) and the other is the direction; checked when the angle increment occurs.

ADDENDUM: The fi2 output of the rotation sensor states how long the led light pulse is ON. Of course you might want to limit that in case the rotation stops just when fi2=High. For that you can add a monostable multivibrator between fi2 and the nand gates to limit the on-state lenght.

• Yeah that was what I was wanting to do. I dont care how many times it has been turned. So how would you do what you have stated usiing the two LED example I gave in the question? – Ashley Kilgour Mar 6 '17 at 13:19
• @AshleyKilgour the circuit is added – user287001 Mar 6 '17 at 14:21
• Cheers for that, so would i just need a maplin.co.uk/p/4013-cmos-logic-hcfhef-qx07h between the rotery enocode and the LED's? – Ashley Kilgour Mar 6 '17 at 16:59
• @AshleyKilgour 4013 is ok. But it needs a proper operating voltage (usually 5VDC) and its unnecessary inputs must be connected to gnd. You need also 4011 nand gate - same for its operating voltage and unused inputs. Your sensor, the rotary encoder must have 4000-series CMOS compatible output signals (if not, you need the proper buffer circuitry). Note that I know nothing excact numeric about your rotary encoder, only the general operating principle. 4000 series CMOS outputs can give only a weak current to leds. The resistors should be at least 1000 Ohms. – user287001 Mar 6 '17 at 20:34

simulate this circuit – Schematic created using CircuitLab

• R! is negative logic input.
• A Leads B for Up
• Alternatively using S with positive logic and Q bar outputs.
• SIM

I don't think it'll come as a surprise when I tell you that you'd often just write a bit of software that runs on a microcontroller to do exactly that.

However, the encoder logic is so simple that you can write it down as boolean combination of the sensors. So, with a very minimal amount of discrete logic gates, you can do that.

So, the mechanism for this kind of problem is to write down your problem as state machine (you're a CS person – you should know state machines) and then implement these states in hardware, simplifying where possible.

• So basically "Write down the problem and then implement it in hardware". – Bort Mar 6 '17 at 13:02
• @Bort hehe, yes :) well, my impression of OP was that she needed the nudge in the direction of "boolean logic", "state machine" and "logic gate", nothing more :) – Marcus Müller Mar 6 '17 at 13:04
• I don't want to have the code tied to the hardware, I could just as easily have two buttons one marked plus and minus. Being a c# I don't like things being tied together. – Ashley Kilgour Mar 6 '17 at 13:06
• @AshleyKilgour I don't understand your comment, sorry, the least. – Marcus Müller Mar 6 '17 at 13:08
• so, two things: a) my answer isn't only about software implementation. In fact, only the first sentence is. b) this is the ultimate web developer attitude. Seriously, your problem is to design a decoder for a specific kind of input. No surprise, with a different kind of input, you'll need a different decoder. What the hell were you expecting?! – Marcus Müller Mar 6 '17 at 13:22

Bi-Directional encoders usually give out two signals in quadrature. That is, two square waves 90 degrees out of phase. You can tell the direction by the state of the second line on the rising or falling edge of the first line.

BTW: Here is the Spec

Simply feed one phase into the clock of a D-Type latch and the other phase into the D-Pin. The output Q will change with direction of rotation. Use that to enable power to high end of the appropriate LED (Resisitor). Use one phase to drive the low end of both LEDS.

• Does not meet the Op's requirements. This solution won't blink for every increment of the state....it will only show the last direction of movement. – Jack Creasey Mar 14 '17 at 19:12
• @JackCreasey, I was assuming the OP knew how to do that part..but you are right... never assume anything in SO-Land – Trevor_G Mar 14 '17 at 19:14

Knowing direction for a quad encoder is a combo of knowing the OLD STATE, the NEW STATE, and having a lookup table to determine the direction.

For example, in the Figure in Trevor's Answer, lets say you start at the state of the top channel being zero, and the bottom channel being zero (i.e, "00"). If the next state is "10", you've moved forward, and if the next state is "01" you've moved backward.

So, $00|10 \rightarrow$ forward motion, meaning you add one to your count and $00|01 \rightarrow$ backward motion, and you subtract one. You'll find that there are 16 possibilities, and some of them can't exist unless you've skipped a tick.

The easiest way to implement this is probably a purpose-made integrated circuit, like this, which takes care of the whole thing. Second easiest is probably a microcontroller. Last would be writing out the state machine, generating a Karnough Map, and grunting out a flip flop implementation.

• Or if you are just using it to count pulses with a basic up-down counter just use one phase as a clock and connect the other phase to the Up_Down pin. – Trevor_G Mar 14 '17 at 17:45
• @Trevor, yes, but you lose half your resolution. – Scott Seidman Mar 14 '17 at 18:06
• Yes I know Scott, well 3/4 loss really. But it depends on if you need that much resolution. It all depends on usage. – Trevor_G Mar 14 '17 at 18:12
• @Trevor -- wasn't trying to invalidate the approach so much as to make the OP aware of the compromise. – Scott Seidman Mar 14 '17 at 18:13