I'm trying to get a microcontroller (stm32) to read a quadrature encoder, but running into an issue when at higher speeds line 1 seemingly gets narrower pulses until it gets out of sync with line 2.

In other words, line 1 starts off fine, but as speed picks up it gets shorter pulses. Eventually, with cycle time of ~150usec, it starts dropping off at the wrong time of the cycle, confusing the counter. See image below for the logic analyser view.

I'm not sure whether the problem is with hardware (misaligned detectors? although I don't see anything obvious) or with electronics and would really appreciate advice. Unfortunately I'm a noob with electronics and won't vouch that this simple circuit is enough to handle faster rotations. In particular, many schematics I found on the web use an op-amp, which I don't: example

Below is the schematic, followed by the logic analyzer output. Finally motor and encoder look like this.

circuit schematic

logic analyzer view

  • \$\begingroup\$ the link to the motor and sensor is broken \$\endgroup\$
    – jsotola
    Sep 29, 2020 at 0:47
  • \$\begingroup\$ Are you able to build a circuit around the driving LED pins as well as the photodiode receiver pins? Your schematic almost looks like it but I'm not sure what you control and what you don't control. (Your link to the motor and encoder didn't work for me, either.) \$\endgroup\$
    – jonk
    Sep 29, 2020 at 2:20
  • \$\begingroup\$ sorry, link fixed. \$\endgroup\$
    – oleg
    Sep 29, 2020 at 7:57
  • \$\begingroup\$ For such speeds, the suitable encoders come with totally different technology. It has a metal fringe, two photo-detectors placed 180 degrees apart for each track, fast comparators, push-pull drivers.... \$\endgroup\$ Sep 29, 2020 at 8:00

1 Answer 1


Optical sensors (phototransistors or photodiodes) have capacitance, and the 10k pulldown resistor is making an RC time constant. If you want fast operation, consider using a smaller pulldown, and (there will be a lessening of output potential) amplifying the signal with a comparator instead of using it directly for logic. A threshold around 100 mV might be appropriate, one LM393 dual comparator would work for both signals.

It's not clear what components your 'D2" and "D3" are, but photodiodes would be in opposite polarity; could these be phototransistors?

A possible circuit would be like this:


simulate this circuit – Schematic created using CircuitLab

  • \$\begingroup\$ Thanks for a detailed answer and esp. the diagram. I'll try it once I get my hands on a comparator (would opamp LM324 opamp work too?). Regarding D2/D3: it is probably a phototransistor (or 2?), it is the black box on this image. \$\endgroup\$
    – oleg
    Sep 29, 2020 at 17:44
  • \$\begingroup\$ @oleg: an LM324 has internal (deliberate) capacitors to slow the output, so it's not ideal. At 0.5V/us, it will work perhaps to 40 kHz, but that's not a lot of margin. \$\endgroup\$
    – Whit3rd
    Sep 29, 2020 at 20:27
  • \$\begingroup\$ Awesome! @Whit3rd thanks for your help. I did not have a comparator, but turns out STM32 has a few built in, so I used that. Also I've only built a part of the circuit with the voltage divider, but that was enough to fix the issue with the pulse length. (Now I have to figure out where these random blips come from, but perhaps that will be gone with the capacitor). Thanks again! \$\endgroup\$
    – oleg
    Sep 30, 2020 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.