Go Down

Topic: Pulse counter - using internal comparator? (Read 6 times) previous topic - next topic


Ok guys,

I set up the sensor to the circuit and the arduino, and it works! I've set the treshold to 3.50 V and adjusted the sensor pot so the high is at 3.6 V and the low at 3.4. When i turn the wheel slowly i can see the output from the opamp shift from 0 to 5V and back. If i turn it fast i can't see it, but that's because my multimeter is too slow.

All thats left for me to do is compile a code so that the arduino timer counts the amount of pulses per second... I'll try to start with that myself but you'll probably hear from me again here asking for help :p

Anyhow, thanks for the solution and the awesome replies. If i don't encounter any problems anymore i'll post some pics here of the whole project



that's great news, gefeliciteerd  XD

keep us up to date if you wish.

veel succes



Oct 26, 2012, 02:07 pm Last Edit: Oct 26, 2012, 02:13 pm by Dyslexicbloke Reason: 1
I agree, once you have the values a couple of fixed resistors is a far better plan.

You might want to put the 10K, freed up from sensor scaling duties, back in as the threshold adjustment to compensate for any drift in signal as the sensor ages.

If it were me I would rebuild the circuit from scratch so you have a clean build, moving things generally leaves less than tidy soldering and potential issues.
I assume you are working on strip-board ... Get some aerosol varnish or better still PCB protection coating.
I use ' Servisol Plastic Seal 60 ' but there are plenty to pick from.

I am pleased it is working for you ... I told you op-amps were your friend!
If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.


Bedankt Paul :D Google translate? ;)

Al, is there a way to know ahead wich resistors to use? or should i just try different types until i get the desired sensor voltage?

I'll build it up from scratch now and post a picture from the final build ;)


the arduino timer counts the amount of pulses per second.

Set-up one timer to count the pulses and another to produce a time-base. Read the timer sections of the datasheet.


wich resistors to use?

Yes its easy to do ....

Set up like this ...
lets assume that the Max signal is going to be 12V and we don't know the Min
We need to scale that to get 12, scaled, somewhere between 0 and 5 we should be aiming for about 4, a bit of wriggle room is always good.

4 is 1/3 of 12 so we a 1:3 ratio or there about ... Using standard resistor values we could try 3K3 (that's 3.3K or 3300 ohms) and 6K8 (6K6 is not a standard value)

Checking ...
We have 10K1 10100 Ohms at 12V (V=IR Ohms Law - Voltage = Resistance X Current) so that is about 1.18mA (12 / 10100)
If the signal was only 9V it would be about 0.89mA, we need to know this to make sure we have enough adjustment if the assumptions RE sensor voltage are wrong.

Now we check the voltage that will be dropped by the bottom 3K3 resistor at both currents.
This is what go's to the op-amp ' - ' and must be within the range we can set as a reference on the ' + '
If the signal is 12V then ... 3300 X 0.00118 = 3.9V
If the signal is 9V then ... 3300 X 0.00089 = 2.9V

Both look OK to me.

We started with a 10K trimmer so I have done the calculation for an overall resistance, the input impedance of the circuit, of 'about' 10K
However it would probably make sense to go a little lower as it will make the circuit, overall, more resilient to noise.
2K2 and 1K, you do the calculations, is still only about 3.7mA overall at 12V giving 3.7V on the op-amp input.

This is where the low bias current into the op-amp comes in, If the op-amp was a low impedance input, taking a significant current, we would have to factor that into the calculations. Perfectly doable but easier if we can ignore it.

You may be thinking that this is easier to do if you just factor the numbers, it is all proportional after all.
You would be right in fact it is easier to do that way but I wanted to show you why it worked not just that it did.

Set your reference to about 3V and then connect the sensor.
Adjust the reference down until the pulses stop, mark the position or count the turns whatever.
Adjust the reference up until the pulses stop, mark the position or count the turns whatever.
Set the reference to the middle of these two points .. all done.

If you can rotate the wheel and get it to output a continually lo and continually high voltage by changing its position then use this to find the upper and lower reference voltages. rather than using the method above.

Shout if you need help .. its no bother
If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.


Ok guys, I'm back. Sorry for the late reply but I had a really busy weekend, so i didn't get the chance to work on the code quite as much as i hoped. I used a 36K and a 12K resistor, but i'll get back to the garage today and switch them, while testing again if it works. 2 pictures included show the circuit connected to the sensor and the arduino, while measuring the output from the opamp with the multimeter.

After setting the threshold, i turn the wheel a little bit and you see the change in voltage perfectly. Only weird thing i encountered was that, when i started i had a voltage(coming from the sensor,reduced by the resistor at) the '+'  at about 3.2V. It went up to 3.3 and down to 3.1 when i turned the wheel. I then set the threshold to 3.2V, but when i checked the + side again, the voltage was dropped about 0.1 V... This went on until i found a stable configuration at about 2.2 V i think... I'll check it again this afternoon and let you guys know.

About the code, I almost figured out how to get a timer started to count every second, but i'm struggling with the concept of counting the pulses with the other timer. Do i need to use attachintterupt every clock cycle to check if there is a change in signal? And how can i get the value of this timer to reset after every second?

I'm gonna try to put a readable code together and post it here later.


To count pulses you need two things ....
1.  An interrupt that will increment a counter
2.  A window, period of time, during which you count.

you would zero the counter and then wait for the time period, when the period is expired look at the number of pulses you have counted and do a little maths to determine the frequency, RPM, whatever you need.
Measuring this way will give you an average and smooth out small variations in pulse width and or period.

The other option is to look for an edge, the start or end of a pulse, and then measure the time to the next edge.
this could be the end of the current pulse or the beginning of the next one depending on weather you want width or period.
Obviously this is faster but it also only gives a result based on a single pulse width or period which is likely to fluctuate from one to the next.

Unless your input device, the wheel sensor, is extraordinary accurate you would want to look at several samples and then average the times.
If you are going to average the times then you may as well just count the pulses over some period which will achieve the same thing with less effort.
The longer the period or the more samples you use the more stable the result, but bear in mine the frequency with which you result updates will reduce proportionally.

You may want to think about using a concept called counter bins.
You count all the time, using your edge detecting interrupt, incrementing the 'current count bin' by one each time the ISR runs.
Each time your bin window, time period, expires, you set 'previous count bin' to 'current count bin' and then set 'current count bin' to 0

If you are not using a timer interrupt to measure your bin window then you will want to store the actual period when you update 'previous count' as it will vary slightly each time, with the actual variation being determined by the scan time and the method you are using to measure the period.


If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

Go Up