Problem getting a "clean" signal from an optocoupler / opto-isolator

Thread Starter

ba58smith

Joined Nov 18, 2018
60
This is a follow-up to this thread, which I thought was solved, but it's not: https://forum.allaboutcircuits.com/threads/converting-12v-alternator-signal-to-high-low-into-a-ucontroller-with-an-opto-isolator.159447/

I've made a tachometer that takes input from the stator wire on a 12V alternator on my boat, into a 4N37 optocoupler, then into a Wemos D1 Mini (an ESP8266 microcontroller). If I can get a nice, clean pulse from the opto and into the Wemos, the software that converts those pulses into Hertz (and eventually RPM) works fine. I have tested it by using an Arduino Uno to pulse one of the digital pins on/off every few milliseconds, and using the output of that digital pin as the input into the optocoupler. When I do that, it works perfectly - 495 Hz out of the Arduino gives me 495 Hz out of the Wemos. (Same for 250 Hz, and 330 Hz, and any other frequency I've tested.)

But when I use the alternator's output as the input to my optocoupler, I get output that is too high, and erratic. At the engine's idle speed, the pulse coming from the alternator is about 260 Hz (258 to 260). That's what the math says it should be (alternator RPM, measured with a handheld digital tach, is 1,950. 1950 / 60 is 32.5 revolutions per second. 32.5 x 8, which is the number of pulses that the Balmar alternator outputs each revolution, is 260). That's also what my cheapo DSO138 oscilloscope says it is. But the output from the Wemos (counted using an interrupt on the input pin) shows from 285 to 310, with all kinds of numbers in between, changing all the time.

So it seems that I'm getting some noise, or something, in the circuit, that's present only when using the higher voltage from the alternator. My oscilloscope shows the voltage to be 16V to 18V, with what looks to me to be a nice, consistent signal shape. Picture below.
Alternator output.png (I didn't have the scope properly zero'd, which is why it shows -5.23V up to 11.68. It's actually 0V to 16.91.)

Below is the schematic of my circuit. I used a battery symbol for the alternator because Fritzing doesn't have a symbol for an alternator.
Tach schematic 4.png
(I didn't actually use a 7805 voltage regulator, but I can't find an object in Fritzing for the adjustable buck converter that I've actually used. I know a 7805 needs capacitors, but the buck converter seems to have those built in.)

What have I done wrong? What have I left out? What could cause an erratic, and "inflated", count on an interrupt pin on a microcontroller?
 

AlbertHall

Joined Jun 4, 2014
8,374
There may be some higher frequency noise present - too fast to show on the oscillogram.
Get the wemos to measure the pulse width and record the minimum width that it sees.
If this shows up short pulses them some low pass filtering is in order. This could be in hardware or in the wemos software.
 

Picbuster

Joined Dec 2, 2013
982
feed the output from opto into a smith trigger.
Or feed the output into a capacitor ( between d5 and opto pin 1. Creating a differentiator).
You are not interested in the wave form but in the interval up-going edge).
The last solution makes a clamp for negative spikes mandatory. (diode will do)

Picbuster
 

Dodgydave

Joined Jun 22, 2012
8,393
This is a follow-up to this thread, which I thought was solved, but it's not: https://forum.allaboutcircuits.com/threads/converting-12v-alternator-signal-to-high-low-into-a-ucontroller-with-an-opto-isolator.159447/

I've made a tachometer that takes input from the stator wire on a 12V alternator on my boat, into a 4N37 optocoupler, then into a Wemos D1 Mini (an ESP8266 microcontroller). If I can get a nice, clean pulse from the opto and into the Wemos, the software that converts those pulses into Hertz (and eventually RPM) works fine. I have tested it by using an Arduino Uno to pulse one of the digital pins on/off every few milliseconds, and using the output of that digital pin as the input into the optocoupler. When I do that, it works perfectly - 495 Hz out of the Arduino gives me 495 Hz out of the Wemos. (Same for 250 Hz, and 330 Hz, and any other frequency I've tested.)

But when I use the alternator's output as the input to my optocoupler, I get output that is too high, and erratic. At the engine's idle speed, the pulse coming from the alternator is about 260 Hz (258 to 260). That's what the math says it should be (alternator RPM, measured with a handheld digital tach, is 1,950. 1950 / 60 is 32.5 revolutions per second. 32.5 x 8, which is the number of pulses that the Balmar alternator outputs each revolution, is 260). That's also what my cheapo DSO138 oscilloscope says it is. But the output from the Wemos (counted using an interrupt on the input pin) shows from 285 to 310, with all kinds of numbers in between, changing all the time.

So it seems that I'm getting some noise, or something, in the circuit, that's present only when using the higher voltage from the alternator. My oscilloscope shows the voltage to be 16V to 18V, with what looks to me to be a nice, consistent signal shape. Picture below.
View attachment 176846 (I didn't have the scope properly zero'd, which is why it shows -5.23V up to 11.68. It's actually 0V to 16.91.)

Below is the schematic of my circuit. I used a battery symbol for the alternator because Fritzing doesn't have a symbol for an alternator.
View attachment 176847
(I didn't actually use a 7805 voltage regulator, but I can't find an object in Fritzing for the adjustable buck converter that I've actually used. I know a 7805 needs capacitors, but the buck converter seems to have those built in.)

What have I done wrong? What have I left out? What could cause an erratic, and "inflated", count on an interrupt pin on a microcontroller?
I would put the signal into a CD4093 Schmidt trigger to square it up better.
 

AnalogKid

Joined Aug 1, 2013
8,107
The 4N35 input diode needs reverse voltage protection. It probably is coming ON when hit with -16 V. Standard cheap visible LEDs come on at around -8 V. Add a 1N4004 diode across the input, anode to pin 1, cathode to pin 2. This might not solve the problem, but it definitely will improve long-term reliability.

ak
 

Thread Starter

ba58smith

Joined Nov 18, 2018
60
What does the output of the opto into the WeMos look like?
I don't really have a good, solid connection with my test probe to the pin on the Wemos - the pin is really short, and the cone of solder makes it hard to get a good connection. I'm going to remove the Wemos (it plugs into a header), and test the socket in the header where the opto output goes. When I do that, I'll post a picture of the output, with as much detail about the signal as I can get.
 

Thread Starter

ba58smith

Joined Nov 18, 2018
60
There may be some higher frequency noise present - too fast to show on the oscillogram.
Get the wemos to measure the pulse width and record the minimum width that it sees.
If this shows up short pulses them some low pass filtering is in order. This could be in hardware or in the wemos software.
"Get the Wemos to measure the pulse width": by timing every one that's sensed by the interrupt, I guess? Keep a max and min of a reasonable sample size, and display it?
 

Thread Starter

ba58smith

Joined Nov 18, 2018
60
feed the output from opto into a smith trigger.
Why not just use an opto with a Schmitt trigger built-in? I have one of those, but the pins are different, so I would have to remake my project. I may well end up doing that, though, if I can't find another way to solve this.

Or feed the output into a capacitor ( between d5 and opto pin 1. Creating a differentiator). You are not interested in the wave form but in the interval up-going edge). The last solution makes a clamp for negative spikes mandatory. (diode will do)
Picbuster
I agree with your statement that I don't care about the waveform, only the interval between the rising edges. RE: the clamp: where, exactly, would the diode go?
 

Thread Starter

ba58smith

Joined Nov 18, 2018
60
The 4N35 input diode needs reverse voltage protection. It probably is coming ON when hit with -16 V. Standard cheap visible LEDs come on at around -8 V. Add a 1N4004 diode across the input, anode to pin 1, cathode to pin 2. This might not solve the problem, but it definitely will improve long-term reliability.

ak
I don't mind adding a diode, but when is it hit with -16V? Sorry if that's a dumb question, but I don't see how the input is ever going to go negative. Thanks in advance for your clarification.
 
You may already have your answer in cleaning up the signal but, are you setting the interrupt for the RISING or FALLING edge? Does changing that have any effect?
 

Thread Starter

ba58smith

Joined Nov 18, 2018
60
What does the output of the opto into the WeMos look like?
I think I finally have a some really good pictures of the signal coming out of the alternator, and out of the opto, and I think they're very revealing! The most revealing is this one, I think. Something like it occurs infrequently, but often enough that it would definitely "inflate" the frequency counted by the ESP's interrupt pin:
Opto output 1.png
Resolution is 1V per horizontal line, so that little spike is a tad over 3V - more than enough to make this one pulse trigger the interrupt twice! I zoomed in even tighter (to 0.2ms) and it's even more dramatic on some of the pulses:
Opto output 1.png

Below is my best picture of the alternator's output, and as you can see, it not real smooth, and there's a little "bump" in the rising edge of each wave - and some of them have a downward spike in the rising edge, an upward spike in the falling edge, or in the one below, both edges. Both of them seem significant enough (at 2V per horizontal line) to cause an extra pulse to be counted, don't they?
Alternator output 2.png

Does that seem like a smoking gun to anyone else? And if so - how do I get rid of it? A diode and one or more capacitors have been suggested, but if those suggestions are what I should be doing, I still have questions about exactly how / where to put them in the circuit. If someone can help with that, that would be great!
 

Attachments

Last edited:

crutschow

Joined Mar 14, 2008
23,353
Does that seem like a smoking gun to anyone else? And if so - how do I get rid of it?
Yes, that looks like a likely culprit.

You could try increasing the value of the resistor in series with the opto input to make it less sensitive to the lower amplitude spikes.
With a 16V input you could probably go to at least 5kΩ.

If that doesn't work, than try adding a capacitor from the opto output to ground.
You'd have to experiment with the values.
Try 100nF to start.
 

Thread Starter

ba58smith

Joined Nov 18, 2018
60
Yes, that looks like a likely culprit.

You could try increasing the value of the resistor in series with the opto input to make it less sensitive to the lower amplitude spikes.
With a 16V input you could probably go to at least 5kΩ.

If that doesn't work, than try adding a capacitor from the opto output to ground.
You'd have to experiment with the values.
Try 100nF to start.
Thanks, @crutschow - I'll try both of those ASAP!
 

crutschow

Joined Mar 14, 2008
23,353
Based upon my simulations you should be able to go as high as 40kΩ for the input resistor.

Also, you should start out with a lower capacitor value at the output, if you go that route, maybe about 10nF.
 

BobaMosfet

Joined Jul 1, 2009
800
As the 4N37 is open-collector on the output, I'd put a pullup to 5V on it (or whatever your board logical level is... 3.3V?) so that it holds stable at a logical level until triggered. Because it's open collector, when the transistor is activated, the circuit is grounded, so your trigger is voltage dropping to ground, not going high. You could put an inverter to switch that logic, if you like. As for messy signal, that's what a capacitor will get you-- essentially a 'sample & hold' so you get a more stable wave-form.
 

Thread Starter

ba58smith

Joined Nov 18, 2018
60
As the 4N37 is open-collector on the output, I'd put a pullup to 5V on it (or whatever your board logical level is... 3.3V?) so that it holds stable at a logical level until triggered. Because it's open collector, when the transistor is activated, the circuit is grounded, so your trigger is voltage dropping to ground, not going high. You could put an inverter to switch that logic, if you like. As for messy signal, that's what a capacitor will get you-- essentially a 'sample & hold' so you get a more stable wave-form.
Boba - do you mean a pullup like the one shown in the schematic, btween the input pin and the ESP's 3.3V pin? Or add another one somewhere else in the circuit?

I don't care about the logic - in the software, the interrupt is set to to CHANGE. However, since the "noise" seems to be in the rising edge, maybe all I need to do is set the interrupt to FALLING, and multiply my count by 2. The falling edge seems very nice and clean.
 

Thread Starter

ba58smith

Joined Nov 18, 2018
60
Based upon my simulations you should be able to go as high as 40kΩ for the input resistor. Also, you should start out with a lower capacitor value at the output, if you go that route, maybe about 10nF.
I tried a 4.7K resistor on the input, and a .1uF cap on the output (that's the smallest cap I have, unfortunately), and I got this:
Perfect 5V.png Yay! (or so I thought) - I froze the screen 25 times and the image here is what every one looked like. But then, I noticed that the voltage was 4.99, and my ESP's working voltage is supposed to be 3.3. Yikes! Something must have happened to the internal voltage regulator, because, although I'm putting 5.00V into the ESP's "5V" pin, I'm getting 5.00 on the 3.3 pin, too. I realized that I'm using a variable voltage regulator to supply power to the ESP, so I simply adjusted it down to 3.3V. (Why did I input 5.00 to start with? Simply because the power input pin is labeled "5V". Doh! I should have always been giving it 3.3V.)

At any rate... adjusting the ESP's voltage from 5.0 to 3.3 changed the opto's output to this:
Imperfect 3_3V.png
I don't know why, but I don't suppose that's important. It did, and now I have that ugly spike on the rising edge again. I tried a bigger cap (I went from .1uF up to .47uF), but it didn't seem to make any difference.

Since the rising edge of the output comes from the falling edge of the input, and since that rising edge of the output is being created by the pullup resistor, I'm wondering:

1. Could the negative spike in the rising edge of the output be caused by a positive spike in the falling edge of the input? If so, would a diode on the input (like a "flyback" diode used when there's an inductive load) help?

2. Do I need a weaker pullup resistor? I'm using 10K now. My thought (maybe wrong!) is that a weaker PU would allow that rising edge to come up "stronger", and maybe eliminate that spike?

3. Should I use the ESP's internal PU instead of an external PU?

4. Should I just give up on that rising edge, and count the falling edges (all of which seem nice and smooth), and be done with it? @crutschow, would you agree that, since the falling edge of the output already looks nice and clean, that a bigger input resistor won't provide any additional benefit?

But wait - that won't work - the interrupt will see the falling edge of those spikes and count them, too, right? It seems like, one way or another, I have to get this output cleaned up.

This is frustrating, but I have to admit I'm getting a big kick out of using an oscilloscope for the first time, and seeing what those pesky electrons are really doing. Without the scope, all I could do is guess at what's wrong.
 
Last edited:

AlbertHall

Joined Jun 4, 2014
8,374
One way would be to get the ESP software ignore pulses narrower than some limit. How much is the input frequency going to change?
 
Top