Saturday, June 1, 2013

Reading the Cam Angle - Part 2 (Filtering MX5 cam sensor signals)

About a week ago I was successful in properly firing the ignition coils based on the cam sensor signal.

Up until then I could fire a coil by sending a command directly to the micro and could also determine cam position based on the square wave signals from the cam sensor. The next step was bringing the two together and use the cam signal as a trigger for firing the coils. Unfortunately, because the cam sensor is located close to the ignition coils, the signal it was sending would be altered by the interference generated by the coils. This resulted in out of order firing and sometimes repeated firing even when the cam sensor wasn't turning!

To filter out the interference I did 2 things. The first was to use a low pass filter circuit and the second was to set up a voltage comparator with a 2.5v reference (half the supply voltage).

The low pass filter circuit will allow low frequency signals to pass through relatively unchanged, while high frequency signals are attenuated. Because the noise generated by the interference consisted of short duration pulses (can be thought of as high frequency) compared to the longer pulses of the cam sensor signal, a low pass filter of the correct value should remove/decrease only the parts that I don't want to measure. 

Low pass filter pic stolen from wikipedia :)

Imagine a high frequency signal on the Vin input. The capacitor will be charged to that voltage, but will take some time (depending on the resistor and capacitor values) to match the input voltage. If the signal is oscillating between -10v and +10v very quickly, then the slow charging capacitor may only get up to +2v before the input switches to -10v, then the cap tries to match that new input... but again may only get as far as -2v before the input switches back to +10v. In this way rapidly changing voltages (interference) are a much smaller part of the signal. You can also see how if a signal changes slowly (the part we want to measure), then the capacitor will have time to charge up to a matching voltage and the output will be nearly the same as the input.

To choose the right resistor and capacitor values you can use the formula 1/(2𝝿RC) to get your cutoff frequency in Hz. The "R" part is your resistor value and the "C" part is your capacitor value. The higher the frequency, the more it will be attenuated. The cutoff frequency is defined as the frequency where a signal is attenuated to 70.7%. If the cutoff value chosen is far enough lower than the interference signal, then it can be removed almost completely.
In the picture above the red shows the original signal and the blue shows the filtered signal. I'm not sure why it doesn't return exactly to the voltage before the interference spike, maybe the output needs a little more current draw to bring the voltage down, or maybe I had something setup wrong. Still, you can see that filtered signal is much cleaner.

At this point I had much more reliable operation, but still with the occasional glitch. What I could see was that even with the filtering I wasn't getting an ideal square wave. The top and bottom edges of the wave form weren’t always close to the ideal 0v and 5v, and could get a bit wavy. To fix this I used an opamp configured as a voltage comparator. An op amp has two inputs ("inverting" - and "non-inverting" +) and will try to amplify the difference between those two inputs as much as it can unless there is some kind of feedback. For example, if the - input is at 1.2v and the + input is at 1.3v, then the difference of 0.1v will be amplified all the way up to the maximum output voltage. Swap around those two values and the difference of -0.1v will be amplified to minimum output voltage.

In my case I have a 5v system where the square wave high(5v) and low(0v) represents two discreet logic levels. Stray too far from those values and you can't reliably interpret the signal. In my circuit I had one input connected to a 2.5v reference and the other input connected to the low pass filtered signal. Now if the input signal is anywhere above the reference, then it will be amplified as much as possible and the output voltage will be pushed to one extreme. Anywhere below the reference and the output is pushed to the other extreme. Also, by choosing which of the inputs - or + is the reference and which is the cam signal, I have the option of inverting the signal (output is opposite of input). So I could have the 2.5v reference on the + input and the signal on the - input. When the signal is low (say 1v), then difference of 1.5v will be amplified to the maximum value 5v (or just under 5v for the opamp i'm using). When it is high (say 3v) then a difference of -0.5v will be amplified to the lowest voltage which is 0v in this case.

Using these two filtering methods I was able to reliably fire the ignition coils based on the rotation of the cam sensor. I think working out how to clean up the signals has taught me a lot and helped me make a lot of progress with this project :D I guess there could be other more suitable filtering methods too. And as I learn about them I could incorporate them in this project.

Final exams are coming soon, so this project has been paused yet again. When I have time, the next step is to build up the circuitry for the injectors, fuel pump and hot wire air flow sensor. Then find a suitable, more powerful microcontroller, write up some code, and do a test run of the engine!

Edit on 2013-06-03: Added youtube video.