Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Switching from Mechanical Encoder to Optical Encoder

Status
Not open for further replies.
I recently acquired some of those cheap encoders off of Ebay (to help my 30 yr old son with music synth project), and the bounce was pretty awful (see attached waveforms). I implemented some hardware debounce using some programmable logic. So depending on your application, you may or may not care about bounce, but here is my hardware solution (I'm sure I will get some flak for the use of all the glue logic when it could be done with firmware).

Nasty output. No flak from me, sometimes you have bigger fish to fry at the software level so a little hardware (MC14490) goes a long way into making the I/O routines cleaner and faster when generating external interrupts with mechanical switches.
https://www.maximintegrated.com/en/app-notes/index.mvp/id/287

C:
if (INTCONbits.INT0IF) {
INTCONbits.INT0IF = LOW;
V.buttonint_count++;
hid0_ptr->bled_on = !hid0_ptr->bled_on;
}
if (INTCON3bits.INT1IF) {
INTCON3bits.INT1IF = LOW;
V.buttonint_count++;
hid1_ptr->bled_on = !hid1_ptr->bled_on;
}
https://github.com/nsaspook/e220
 
Last edited:
I would probe for clean sharp edges of A and B.

If I ma intrude on the topic with a question that maybe relevant. I had an encoder output connected to input with High Speed Counter set at 100Khz and it was counting accurately and then for a reason I changed the wiring and connected the output of the encoder to a SSR and the output of the SSR to the input of the PLC. The count increments were all over the place with every pulse counted sometimes as tens and sometimes as hundreds of pulses. So out of pure experimentation I put a high value (I think a 10k) resistor across the output of the SSR and boom that solves that problem. I reduced the resistor value which did not work. What was happening and why did the resistor solve that? Was it current leak?

Thanks
Kal
 
You always need a way to discharge capacitances and you always need a path for leakage currents to go. In some cases you need a sharp edge.

Any idea what the input circuit looked like and what SSR you used?
 
It's also important to remember that even an optical encoder can give you noisy looking signals. If the shaft happens to be positioned almost exactly at a transition point, then any mechanical vibration can cause an output to flicker. Manufacturers could possibly include some sort of hysteresis in their design to prevent this, but that would affect the resolution of the encoder, so I doubt they would do it.
 
It's also important to remember that even an optical encoder can give you noisy looking signals. If the shaft happens to be positioned almost exactly at a transition point, then any mechanical vibration can cause an output to flicker. Manufacturers could possibly include some sort of hysteresis in their design to prevent this, but that would affect the resolution of the encoder, so I doubt they would do it.

I imagine that's part of the reason for the detent, which is located at the 0/0 position. It ensures the shaft "springs" to an area with a known value, rather than on an edge somewhere.
 
Mikebits,
It is a bit confusing. Are you referring to mechanical encoders, and are your data from a mechanical encoder? Or, are you referring to optical encoders, and are any of your data from an optical encoder -- one that you are sure was an optical encoder?
John
Sorry for the confusion, I suppose that is due in part to my not knowing myself. When I googled rotary encoders, the images showed leds, so I sorta thought that is what I had. Well your question prompted me to tear into one of these encoders. Turns out I have the mechanical type (I should have known based on the size). Well anyways, my encoders are the mechanical type which may explain the nasty bounce. Attached is a dissected encoder.

photo-13.png
 
I agree on your use of the flip flops for your debouncer. Pretty definitive. Ganssle (http://www.ganssle.com/debouncing.pdf ) mentions them, but today so much is just a software or RC delay.

Thanks for clarifying. $.0.75 is almost enough for a dollar meal ... Oops, I guess McD stopped those last Summer. I haven't been back since.

John
 
You always need a way to discharge capacitances and you always need a path for leakage currents to go. In some cases you need a sharp edge.

Any idea what the input circuit looked like and what SSR you used?
The input circuit of the encoder was developed here and I made a couple of them to **broken link removed**two of those photo **broken link removed**. The relay is an NTE RS3-1D25-24T
The pulses out of the interface are sharp Ons and Offs but not so when I went through the SSR.
But I don't understand how putting a transistor across the output discharged the capacitor without path to ground?
 
Last edited:
Yep. No path to discharge the base of Q2 in the original circuit without that 10K resistor.

Lots of circuits have nuances. ib of an OP amp can't drop across a wire. That caused me considerable grief.

Every circuit element has parasitic elements. In the real world you have to account for them.

In a certain Tektronix design, 3 resistors in parallel cannot be replaced with a single resistor. The inductances of the resistors mattered.
 
I agree on your use of the flip flops for your debouncer. Pretty definitive. Ganssle (http://www.ganssle.com/debouncing.pdf ) mentions them, but today so much is just a software or RC delay.

Thanks for clarifying. $.0.75 is almost enough for a dollar meal ... Oops, I guess McD stopped those last Summer. I haven't been back since.

John

Hi,

As a side note, they now have 2 for 2 dollar means, where you can get two of something for 2 dollars. That means 2 McDoubles for 2 dollars for example.
 
Hi,

I agree that hardware is the best way, but FF's may not be the only alternative.
If there are capacitors on the two outputs then the energy from the bounce is stored and built up over a very short time, then the level changes slowly and does not go back up or back down too rapidly. That means that with the right cap value there may be no more bounce. Of course this has to be done just right, with the right cap value.

I have never tried this because the mechanical ones are working good with just the software so far. It's another avenue to explore though. A nice addition might be a Schmitt Trigger gate to clean up the capacitive waveform that will result.

Another idea i did not get to explore yet is what we might call "controlled cap filtering", or "dynamic filtering", or "non linear filtering". This would be where we connect the cap, but once we get a change in state we discharge (or charge) the cap by converting the input to an output and force the cap to discharge (or charge) to hold the state for a time. Or something along those lines.

With the ARM processor i think we would be able to read the encoder as an analog pin, then internally plot the curve of the cap and we would know where the encoder is down to a fairly fine resolution. This would probably be possible because the ADC is much fastter in that chip.

There may be other possibilities too that we havent looked at yet. For example, some kind of simpler 2 input filter that filters the two into one output. The output would ramp up for clockwise, ramp down for counter clockwise, or something like that.
 
Last edited:
Yep. No path to discharge the base of Q2 in the original circuit without that 10K resistor.

Lots of circuits have nuances. ib of an OP amp can't drop across a wire. That caused me considerable grief.

Every circuit element has parasitic elements. In the real world you have to account for them.

In a certain Tektronix design, 3 resistors in parallel cannot be replaced with a single resistor. The inductances of the resistors mattered.

That's odd cause when I connect the circuit directly to the PLC it works just fine and that's how I ended up doing it but when I was using the SSR I had the current leak problem and considering that on a SSR the load is isolated from the input that problem seemed to me a strictly SSR output problem.

Interestingly after I put that resistor purely out of lack of knowledge I did a search to find why it worked and found the following information which I've yet to understand. Not sure what power factor is

2.Reset Failure Using Load with Low Power Factor

If the power factor of the load is low (guideline: cosΦ=0.4 max.), the delay in the load current phase will increase relative to the load power supply voltage phase, and a large transient voltage (dv/dt) will be applied to the Solid-state Relay when it is about to turn OFF (i.e., the load current is near zero), resulting in the possibility of the Solid-state Relay not being able to turn OFF (commutation failure).

The Solid-state Relay has a built-in CR snubber circuit to limit the rate of change in the transient voltage, but leakage current will increase if the C value is increased, and reset failure in item 1 above may occur, so the C value is set to the greatest common factor.

Therefore, if reset failure occurs because the load power factor is low, the rate of change in the transient voltage can be limited to prevent reset failure by connecting a capacitor and resistor in parallel with the Solid-state Relay load terminals.

The capacitor and resistor must be checked to match the load, but previous experience shows that a resistor of 100 Ω/1 W and a capacitor of 0.1 μF/250 VAC will prevent reset failure.

Also, as mentioned above, the leakage current will increase, so check that the reset failure in item 1 does not occur.
 
Yep. No path to discharge the base of Q2 in the original circuit without that 10K resistor.

.


OK I just got it. The current in the base of Q2 is keeping the SSR from releasing the output. I see, but then how does putting a resistor across the output (and I mean jupmer the two output terminals) discharge the that current?
 
1. You were not supposed to use the SSR, but rather a transistor optoisolator.
2. Power factor only applies for AC circuits. When it's 1 or 100% ohms law works.

p/n junctions have capacitance and leakage currents. When a signal goes away it may not go away fully.
What you see a lot of times is the high is driver through a resistor and the low actually sinks current.

The SSS you picked would have all sorts of issues. Those SSR's are primarily made up of two FETS back to back so they can pass either polarity.
 
I tested it out again though I no longer need the SSR but I just wanted to see what you meant. You were right about the cap leakage. While the SSR was energized and its LED was on I turned the power off and the LED took a few seconds to go all off then I added the 10K and bingo , whenever I turn the power off the LED immediately goes off. Nice to be able to actually see electronic theory.
That issue with it no counting correctly though is still there but it's academic now as I will not be using it.

Thanks a lot
Kal
 
An interlocked dual-flipflop circuit can prevent this, but that circuit rarely is used.
I missed your comment about the flip flop before I made my last post, but as I mentioned, a software routine that tests for all transitions doesn't need any additional external hardware debounce. So the flip flop would just be an added bit of unnecessary hardware and expense.
 
Agreed, once you have something that executes software, there is no need for any hardware other than transient protection. I was talking about an alternate, more complex all-hardware implementation that prevents extra clocks or reversals caused by bounce. After all, edge detection was a hardware thing decades before all you typists tried to take over. Actual conversation with a former IT manager:

Him: I'd never hire a CIS major. All that degree means is that they can type.

Me: Dude, you have a masters in CIS. What does that mean?

Him: It means I can type *really* fast.

ak
 
Actually, I'm an EE, and only took three computer classes when I was in university, but ended up in the software end of things. I had a knack for discrete logic design and then ended up doing microcontroller based logic designs. I find circuit design to be fun, but it's mainly a hobby nowadays.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top