LM339 rail-to-rail output

Thread Starter

0x10c

Joined May 18, 2018
10
Hello,

I have a problem using an LM339N - I'm not familiar with this device, so I'm hoping it's just a configuration mistake.

I'm attempting to convert a 3Vrms sine-wave at 600Hz to a square-wave between 0V and 5V.

I've set up an LM339N with a 5V and 0V supply (to pins 3 and 12) and have an a.c. signal source connected to one of the device comparator's inputs with 10k current limiting resistors on both inputs. The output of the comparator has a 10k pullup resistor to the +5V supply and a 10k current limiting resistor.

When I measure the output, instead of the 0V to 5V square wave I'm expecting, I'm getting 2.5V to 5V.

Can somebody explain to me why it's doing this, please?

Thanks :)
 

wayneh

Joined Sep 9, 2010
16,102
A current-limiting resistor? You've probably made a resistive voltage divider. Can you sketch out a schematic?
 

dl324

Joined Mar 30, 2015
8,953
Welcome to AAC!

When discussing circuits, it's best to post a schematic so we can tell exactly what you're talking about.

What are you using to measure the voltage?
 

Thread Starter

0x10c

Joined May 18, 2018
10
Hello,

thanks very much for replying so quickly.

Here's my circuit:



I'm measuring the output with an oscilloscope at Vout, relative to the 0V rail. The input is from a signal generator; both instruments are mains powered, so there's probably a common ground connection, despite me showing the ac signal isolated.

Apologies for the dubious quality of the diagram - I had to draw it in powerpoint...

Thanks :)
 

crutschow

Joined Mar 14, 2008
23,364
One of the input resistors needs a ground connection for the LM339's input bias current.

You don't need the 10k output resistor.
The current is already limited by the 10k resistor to +5V.
 

Thread Starter

0x10c

Joined May 18, 2018
10
Hello,

thanks for your help with this - I can see what you mean about the output resistor, so I'll change that.

I'm not sure what you mean about the input resistor needing a ground connection, though - if I ground either resistor on the supply side, won't I be grounding the supply as well? If I ground either resistor on the comparator side, wouldn't that just tie that input to ground?

I'm keen not to tie the signal supply to ground at all - ultimately, the ac source will be a variable reluctance sensor within an ABS circuit and any notable change in the circuit conditions causes the ABS system to go into an error state (i.e. the dash-light comes on).

Thanks :)
 

AnalogKid

Joined Aug 1, 2013
8,108
5 Vrms is 8.5 V peak-to-peak. That exceeds the common mode input voltage range for the part when powered by a single 5 V supply. So a little input signal attenuation isn't just a good thing, it is required.

There are several ways to adjust this circuit, but all of them involve establishing a DC path between at least one of the input pins and the 0 V rail. Not necessarily a direct connection, but something to give the input differential stage a reference. This can be done with two resistors setting up a 2.5 V reference point, or (because of the excellent input stage design), connecting one of the inputs directly to GND. If the sensor signal is completely floating from the vehicle ground, then there should be no problem connecting one end of it to GND as long as the other end has sufficient current limiting.

ak
 

crutschow

Joined Mar 14, 2008
23,364
I'm keen not to tie the signal supply to ground at all - ultimately, the ac source will be a variable reluctance sensor within an ABS circuit and any notable change in the circuit conditions causes the ABS system to go into an error state (i.e. the dash-light comes on).
Then I would think the sensor has a connection to ground somewhere.

Otherwise, you can't leave it floating, but you could add a high resistance (say 100kΩ) from one of the inputs to ground.
Certainly you should have that for proper test operation.
Depending upon some mains connection for ground is not good form.
 

Thread Starter

0x10c

Joined May 18, 2018
10
Hello,

thanks for your help.

I'll try grounding one side of the sensor via a high value resistor and see if that helps.

I agree that using the instruments as the ground path was questionable practice, but I was trying to replicate the vehicle with its ungrounded sensor setup. If there was a ground path via the signal generator/cro, though, shouldn't it have had the same effect as adding a ground path with a resistor?

I still can't really see why the comparator output is 2.5V to 5V rather than 0V to 5V.

Just to clarify the voltages in my diagram (I can see now that it's a bit confusing), the comparator is connected to a 5V regulator supplying 5V dc, not 5V rms. The signal is only 3V rms at it's maximum, so that's within the comparator supply rails (about 4.25V peak by my reckoning).

Thanks for your help :)
 

Papabravo

Joined Feb 24, 2006
12,405
The LM339 is an ancient device and may not be able to get as close to ground as you need it to be. Look around for more modern parts that have better capabilities.
 

ebp

Joined Feb 8, 2018
2,332
"IIf there was a ground path via the signal generator/cro, though, shouldn't it have had the same effect as adding a ground path with a resistor?"

Yes.

I can see no reason the comparator output should not be swinging nearly rail-to-rail. Are you sure you don't have your pullup on the Vout end of the other resistor instead of the comparator output?

[EDIT] - if you want to experiment with a fully floating input, you could transformer couple the signal from your generator. One of the tiny iron-core audio interstage transformers from an ancient transistor radio or small amplifier would be probably good, if you happen to have one kicking around. Otherwise you could experiment with the smallest AC mains transformer you might have. One with dual primary windings or dual secondary windings might be good - use both primaries as in and out or both secondaries as in and out. I recommend putting 50 to 100 ohms in series on the input side, just as a measure of protection for you signal generator.
 
Last edited:

Thread Starter

0x10c

Joined May 18, 2018
10
Hello,

thanks very much for your advice on this.

I very much like the idea of using an audio transformer to create an isolated input - I'll give that go if I can find one amongst my spare parts.

I originally tried this same circuit with a 741 as the comparator - received wisdom at the time suggested that an LM339 was a far better choice - could anybody recommend a better choice again?

I'm still not entirely clear about whether or how to ground one of the comparator inputs - I'd be very grateful if someboy could advise on which of the following (if any) would be the best approach:

...with the inverting input tied to ground via a 10k resistor:



...with the inverting input grounded as above, but with the current-limiting resistor removed (the above looks like a potential divider to me, which I'm pretty sure I don't want):


...with the inverting input grounded and no input resistors - hopefully the input impedance of the comparator is high enough to prevent the ABS system being affected by it:


Thanks very much for your help with this :)
 

crutschow

Joined Mar 14, 2008
23,364
I would use the first circuit except move the 10k ground resistor to the left side of the input resistor.

And remove the 10k output resistor.
 

Thread Starter

0x10c

Joined May 18, 2018
10
Hello,

thanks for that - that's the easiest option of all, so that's a bonus for me :)

I'll try it and see how things pan out - thanks very much :)
 

Thread Starter

0x10c

Joined May 18, 2018
10
Hello,

I went and saw how things panned out and they were both good and bad...

This comparator feeds into and arduino, which is counting the pulses to calculate vehicle speed - it then feeds a speed signal to the ecu and the speedo.

Up to this point, the speedo has shown zero - I believe this was due to the comparator not dropping to zero and, hence, the arduino not recognising the pulses (it triggers an interrupt and has to see a full rise from zero to five volts).

Now, the speedo is reading about double what it should be, (which is a giant leap forward, to be honest). I assume this means that the 2.5V problem has gone away but I suspect that the sensor signal is actually going outside the 5V rails of the comparator and the comparator is doing that weird thing where it switches back to zero when the signal exceeds the supply rail, a bit like this:



...again, apologies for the diagram - I haven't got my scope handy to test the circuit properly, so this is from memory.

I need to attenuate the input signal by the looks of it, but is there an easy way to do that using a potential divider on the input(s) somehow? Would tying the non-inverting input to 0V via a 10k resistor work?

Thanks for your help :)
 

Thread Starter

0x10c

Joined May 18, 2018
10
I can't seem to edit my last post - not that it really makes that much difference, but the over-driven comparator output almost certainly looked more like this:



:)
 

wayneh

Joined Sep 9, 2010
16,102
You could try a half wave rectifier. It would shave a bit off the peak and test your theory. It’s not cool to feed the comparator a voltage outside it’s power rail voltage range.
 

Thread Starter

0x10c

Joined May 18, 2018
10
It's an interesting idea, but it'd only take about 1.4V off the peaks.

Could I clamp the input voltage with reverse parallel diodes, do you think?

...I'd give it a go if I had some diodes handy to try it with...
 

wayneh

Joined Sep 9, 2010
16,102
It's an interesting idea, but it'd only take about 1.4V off the peaks.

Could I clamp the input voltage with reverse parallel diodes, do you think?

...I'd give it a go if I had some diodes handy to try it with...
I think that's a great idea for any automotive application.
 

Thread Starter

0x10c

Joined May 18, 2018
10
...then I think I'll give it a go, as it looks to me now like the comparator's picking up random noise - the speedo's all over the place :(

Thanks for your help :)
 
Top