Pulse counter - using internal comparator?

Hey guys,

I’m having a problem with the following: i’m using a 2 wire digital hall sensor (1 signal wire, 1 ground). The signal is represented in the pics below (scope didn’t have usb option, so i had to take them with my phonecamera). It is used as an ABS sensor in a ford fiesta.

I need to read the signal in the arduino and determine its frequency in order to log the speed of the car. I have tried attachInterrupt, didn’t work. I also tried the frequency conter from Page has moved , didn’t work…

I know i need to count rising/falling flanks and stack them in to a counter in order to determine the frequency, I just don’t know how to do it. Could it be that the signal is too small for the arduino to detect a rising/falling flank? Peak to peak value is 0.4Volts.

Could i solve this using the internal comparator in the arduino, and more importantly, how do i do this in the programming language? Or does anyone have any other ideas?

Thanks very much

your high of 0.4v is far to low to be seen as a high by the any of the AT chips.


The signal is, apart from small, AC by the looks of it.
I suggest you feed the signal to a resistor between the inputs of a comparator.
If you use something like a CA3140EZ you can supply it 5 volts, add a single resistor between + and - and a decoupling cap across the supply and you are good to go.
you will get 5v when the sensor current is positive and 0v when it is negative.
It also supports offset null adjustment if you need it, but I doubt you will.
The inputs are jFet so the sensor will be completely unaffected.

Once you have a good input either of the solutions mentioned earlier should work just fine.

PM me if you need any help there are loads of other simple cheap options.


Ok thanks guys this is a lot of help! 1 more question: Would it be possible to do it with a UA741? Because i still have some old ones, if it's not possible i'll pick up the other ic as soon as possible...

Appreciate your help

If the 741 has sufficient input impedance to avoid sinking your sensor signal it should be just fine. Set it up without any feedback that way you will have huge gain .....

Wait one ... Typing this set me it set me wondering if a 741 will work from a single rail. I don't think it will, you will have to check the data-sheet Having to muck about with a negative rail would be way more effort than getting a modern FET based op-amp designed for single rail operation.

The one I suggested is only one of many that would do the job, what you need is neither complex or demanding. They are very common, so easy to find, and cheap and fairly hardy.

UK eBay is listing 20 odd form about £2.20 in postage if you buy 2 of them.

Other common options LM393N (dual) 8 pin or LM339N (quad) 14 pin, both without offset null Designed for single rail operation and interfacing with TTL/Cmos, which is exactly what you want to do. These are even cheaper!


Actually, would not an arrangement of a simple transistor emitter follower do the job? Put a diode at the base to block negative the pulse, a bit a base bias maybe and I think you would see things happening?

I am sure you could do it with transistors, possibly even 1 transistor but BJT's are current amplifiers and therefore inherently low z unless you take steps to arrange them otherwise. A single rail FET op-amp / comparator wraps all that up for you with super high input impedance and low output impedance. No diodes required ... It will simply output high, well its high rail anyway, when + is a few mV greater than -.

Many will sink or source so the circuit could be configured either way IE low when + is a few mV less than -.

I stopped using BJT's years ago, apart from driver circuits or simple current sources. Once I got my head around op amps I couldn't understand why I ever struggled with them. For high current stuff I use FET's, all be it with a simple BJT totem-pole to drive the gate if I need them to be qiuck, and for open collector outputs I usually use ULN2003A's When using FET input op-amps you can effectively ignore the bias current which simplifies the design and the same component, with a sensible choice of feedback resistors can give you a gain from unity, a voltage follower, 'many thousands' : 1 But that's just me ...

Dyslexicbloke: I am sure you could do it with transistors, possibly even 1 transistor but BJT's are current amplifiers and therefore inherently low z unless you take steps to arrange them otherwise.

Steps such as using an emitter-follower you mean :)

I will take that on the chin RE z but wouldn't an emitter follower have a gain close to unity .. How will that help if the signal is only 25% of what is required as a minimum and an order of magnitude smaller than what is ideal.

Hay what would I know ... There are hundreds of ways to do this and half of those are probably valid approaches, I was just trying to suggest the simplest.


Yep, sort agree with you Dyslexical about using an op-amp. It's what I would mostly use these days anyhow and I guess 8 legs is not a lot more to deal with. Actually if the sensor is a stock standard magnetic flux pickup, isn't it then a current source anyhow. And you'd need to watch out for those nasty spikes as well.

There would be any number of ways to read this little pule in. I did think about the use of a FET, and I 'm sure this could be made to work as well.

@ Jasper, how have you gone with this so far, do tell?

I believe there is an internal comparator only on the Mega, not the 328, though I may be incorrect. What hardware are you using?

I’m using the arduino uno R3 with a SDboard from snootlab (mémoire), together with a BMP 085 breakout pressure sensor and a LPR530 gyro. Combined, they make a datalogger that measures height and is used to see if the car is drifting or not. I need the wheel speed from the car to synchronize my data from the SD card and the data that i retrieve from the car’s ECU.

I’ll go to the local electronics store today and see if they have the IC i need… I’ll keep you guys updated!


I’m on my way now to the store, just to be sure what you mean, this is how the circuit should look like? And should i just put 5V on V+?
(not that familiar with opamps :confused: )

Also, do you think i best use the output with attachIntterupt and then in to a counter? Or is there a better way to count the pulses per second? Keep in mind that the complete program also handles the pressure sensor and the gyro sensor + writing the data retrieved from them to the SD card…


[Edit 2]

Just went to the store, decided to go with the LM393N… Now i’m figuring out how’s the best way to solder the thing on the SD board prototype area, and waiting for the confirmation from you guys if the circuit is ok. Any suggestions for the resistor / capacitor i need to use? I have a capacitor of 2.2µF at hand, but i can go buy others anytime.

CircuitCA3140.bmp (2.81 MB)

Comments on your circuit ... Your -V rail should actually be 0V because the comparator is working on a single rail. Many op-amp tutorials you will find specify a bi-polar supply +/- because that how all the first op-amps worked, a long tome ago

You need a ground, 0V, on the bottom of your sense resistor so the current has somewhere to go. The inputs of the op-amp have a huge impedance, so high that you can consider it infinite for 'most' practical purposes. You are making a current detector ... so you need a current to detect.

The op-amp is in a configuration called open loop which means that it has no feedback circuit to modify the input based on the output. the result is that a slight positive difference between ' + ' and ' - ' will cause the output to swing all the way to the + rail, your 5V

Swap round the + and - pins, you will find it easier to contextualise. Don't enable the input pull-up on the Arduino, let the op-amp swing the input high or low. The output of the op-amp is bipolar so it will pull down, 0V, when low and up when high, 5V. No need for any biasing of the Arduino input.

The result will work like this ... When the output of the sensor is positive, relative to ground, current will flow through the sense resistor to ground making ' + ' more than ' - ' and the output will be high. The absolute values of + and - do not matter, this is refereed to as common mode signal, it is the difference that the op-amp reacts to.

When the current is flowing the other way ' - ' will be greater than ' + ' in relative terms, so the output will be low.

Have fun and pleas give me a shout if you run into problems. Al

Could i solve this using the internal comparator in the arduino

Absolutely yes.

No need for another chip.

Hang on ... We came into this thread from the premise that the voltage or the pulse wasn't high enough to lift a digital pin high.

If you are going analogue its a whole other ball of wax but I would still recommend buffering the input, if only to protect the uP. At the very least you would need to clamp it, coils of wire over magnets directly connected to a uP comparator is a poor idea.

At the risk of harping on .... The simplest way to buffer a voltage whilst clamping it between 5V and 0V is to use a single rail op-amp configured as a voltage follower.

If you let the magic smoke out of your uP chip it will stop working :open_mouth:


The simplest way to buffer a voltage whilst clamping it between 5V and 0V is to use a single rail op-amp configured as a voltage follower.

How about a jfet?

Spot on Al, rather the smoke of a cheap op-amp than a µcontroller.

And I think the comparator is only on the 2560, though I would need to verify that against the datasheets. I would only use the internal comparator when dealing with onboard logic at levels derived by the logic, not a inductive whatever prox-sensor.

Why use a JFET, an op-amp in my opinion is more suitable for the job and would be easier to setup.


Fets, even JFets configured like you would do a BJT as a follower generally make poor followers, prone to all kinds of issues.

I am no electronics guru and that is exactly why I think you will be better off with op-amps in virtually all input buffering scenarios.

A voltage follower requires 1 resistor, apart from the obligatory decoupling cap.

Signal to resistor to ' + '| ' - ' to 0V | output to uP and ' + ' provided the signal isnt over the voltage threshold of the op-amp's input give it 5V and forget it.

It relay doesn't get much simpler. Even if you are dividing a higher voltage signal it is still the bast way to go because the huge input impedance of the op=amp and resulting negligible bias current makes the divider essentially linear with out the need to get clever, well slightly cleverer anyway.

Ok guys, love the enthousiasm on this forum! :slight_smile:

I’f I understand correct the circuit should look more like this then? Except for the one difference that the LM393N has a ground pin, so i guess don’t need a connection from my sense resistor to ground anymore? I understand it should give either 0V or 5V on the output, and then i can use attachInterrupt() to acknowledge the flank change… Just to be sure of the signal from the sensor output, I emailed an engineer from Ford, he sent me a screenshot from the sensor in 1st gear . I was only able to measure at very low speeds because i had to turn the wheel manually. You can see it below.

Should i connect the ground from the signal also to the opamp, or just the signal wire? I attached the ground from the LM393 to the arduino ground pin.

I’ll solder the circuit and give it a try later :wink:

@dhenry: I was thinking the same, but i think the other guys are right and it’s safer to do it with an opamp… plus i lack the programming skills :wink:

arrr, I thought you said the signal was only 0.4 volts peak. On your image it is showing 1 volt per division and the signal is going between 2 volts and around 2.5 volts. So, need to work on that. Also I noticed it before, but that capacitor you have going from the power pin of the op-amp to + v, well change it so that your power + goes to the +Vcc of the op-amp. The capacitor essentially goes across your supply, that is between 0 volts and 5 volts.

If in fact the signal is like shown on the image, you may need to amplify it somewhat with the op-amp, there may be not enough difference for the comparator to discriminate. You'll probably need to put in a muti-turn trim pot, that is, a 10 turn trim pot that will allow you some precise adjustment for the comparator to work.

Dyslexical, are you able to make up a drawing at all?