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

Thread Starter

ba58smith

Joined Nov 18, 2018
62
Like using a switch bebounce function on the signal input?
That's certainly worth a try, as long as that spike in the rising edge doesn't get too "fat" anywhere in the engine's RPM range. I'll check that, and if it doesn't change much, I'll try this interrupt debounce method that I found:

long debounceTime = 300; // no idea how long to set this - will have to experiment
volatile unsigned long last_micros;
volatile unsigned long revCounter = 0;

void setup()
{
attachInterrupt (D5, isr, CHANGE); // attach the interrupt to pin D5
last_micros = micros(); // initialise last_micros
}
void loop()
{
// calculate the RPM using revCounter, then display it
}

void isr()
{
if (long (micros() - last_micros) >= debounceTime * 1000 ) {
revCounter++;
}
last_micros = micros(); // start timing the next change
}

Thanks for the suggestion!
 
Last edited:

Thread Starter

ba58smith

Joined Nov 18, 2018
62
You could also try a larger input resistor, such as about 40kΩ.
@crutschow - I went up to 20K, and it helped a LOT! My output is now close enough for my purposes, but it still fluctuates a little, so I will continue to try bigger ones. I will try 40K next. Please tell me how you calculated that it can handle that much - my calculation was to use 1.68K. (18V - 1.2 fwd voltage of the LED in the optocoupler = 16.8, divided by 10mA = 1.68K ohms - I think?) But that was probably the minimum calculation. How do you calculate the maximum?

Also, you originally suggested a 100nF capacitor on the output side, which I had, and used. Then, you said to start with a 10nF cap. I don't have anything smaller than 100nF, but when that didn't make it perfect, I changed it to 470nF, which is what it is now. Should I go back to the 100nF? What will happen if the cap on the output is too big?

Thanks so much - your input has been incredibly helpful!
 

Thread Starter

ba58smith

Joined Nov 18, 2018
62
UPDATE / SOLUTION: I ended up using a low pass filter (2k resistor and .1uF capacitor) between the tachometer and the opto-isolator, and that smoothed out the signal dramatically. The resulting signal was nice and smooth going into the opto, and a nice square-edged "wave" coming out. Thanks, everyone, for all the help!
 

BobaMosfet

Joined Jul 1, 2009
2,110
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.
Hi, yes, you have it pulled up. Sorry, I mis-read the schematic the first time. Beyond that, it's a filter issue, and you were able to fix it.
 
Top