Saturday, August 17, 2019

0000 0000 0000 1000

LM393 comparator (and the LM358 Op Amp)

Measuring stuff in the real world can be messy - for instance when the ambient light drops around dusk it can oscillate about a value which you may be trying to measure (e.g. to turn on an artificial light). I have dealt with this same scenario a couple of times.

One project I have measures light exactly in this way, but I have an attiny13 microcontroller providing software hysteresis by switching on if the sensor value is above "350", but not switching off until the value is below "250". These are nominal values.


Noisy signal tamed by hysteresis (green lines)
Another project uses an LM393 voltage comparator to poll a magnetic sensor and then feed a digital 1 or 0 to the microcontroller. The magnetic sensor is noisy, but the resultant digital signal rings loud and clear.

Configured as Schmitt trigger the LM393 hysteresis is provided according to the following diagram and formulae.




I've seen a lot of formulae and online calculators for hysteresis using op amps and comparators, but the equations shown above for Vth and Vtl actually work when measuring all of the parameters (including crucially the Vol and Voh numbers), as can be seen in the screenshot below from a spreadsheet.




So now we construct the circuit shown below based on those numbers, first with fritzing and then on a breadboard.






In the circuit above 9V supplies three key components. Firstly 9V is fed to the LM317 from a previous blog which then provides variable signal voltage to Vin.

Secondly the op amp/comparator needs to be powered itself and accepts 2V to 36V.

Finally 9V is fed to a voltage divider that supplies the reference voltage (in this case 5V).

When the LM317 signal is above the upper threshold (~5.5V) then the output of the LM393 is high (2.66V). When the signal drops below the lower threshold (~5.2V), the output drops to low (0.03V). 2.66V is enough to weakly drive a lot of LEDs, but most likely it is better to use a transistor switch as shown in the video below.





So we get lovely hysteresis after all of that, importantly based on some measured values. It's a bit of a dark art and I've certainly read much conflicting advice on the subject - but if you're keen there is some quality information available. Good luck.



No comments:

Post a Comment