[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  Electronics
. -> [Folder]  Operational Amplifiers
. . -> [Subject]  How to use an op-amp as a comparator

How to use an op-amp as a comparator

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Posted by Nick Gammon   Australia  (23,003 posts)  [Biography] bio   Forum Administrator
Date Sun 27 Mar 2011 01:56 AM (UTC)

Amended on Tue 07 Apr 2015 05:56 AM (UTC) by Nick Gammon

This shows how to convert an analog signal (like, from a light sensor) into a straight digital on/off signal, which can then be used to trigger an interrupt.


An Operational Amplifier (op-amp) is designed to amplify the difference between the V+ and V- input pins. Since the amplifier has very high gain, in this configuration its output is either fully on, or fully off, depending on whether the input (in this case on V-) is greater than, or less than, the reference voltage we put on V+.

The LDR (light dependent resistor) is wired as a voltage divider between +5V and ground, with a 4.7K resistor being the other half of the divider. Some quick measurements showed that in this configuration, the middle of the divider (Pin 2, or V- on the op-amp) was around the 1 volt mark if I shone a torch on it.

Thus we needed around a 1V comparison voltage, achieved by using a 4.7K and a 1K resistor connected to Pin 3 (V+ on the op-amp). This would give a theoretical voltage of 0.877V (877mV), but was measured to be 900mV. The slight difference can be accounted for because the resistors were 5% tolerance.


Vout = Vin * R2 / (R1 + R2)
     = 5 * 1000 / (1000 + 4700) 
     = 5 * 1000 / (5700) 
     = 5000 / 5700
     = 0.877

When Pin 1 of the op-amp is on (high) I measured 3.6V on it. This sounds about right, as we are supplying the op-amp with 5V on the power pin, and the spec says that "High-level output voltage" is VCC - 1.5, that is 5.0 - 1.5, which is 3.5V.

Connecting up the oscilloscope and flashing a torch on the circuit board, we can see the transition of the output of the op-amp (pin 1 on the op-amp) compared to the input (pin 2):

The op-amp has cleaned up the wavy analog signal into a clean digital transition.

Notice also that the output from the op-amp is inverted with respect to the input (the output goes high when the input goes low). This is because the LDR is connected to the V- pin of the op-amp. If you wanted a non-inverted output just swap the inputs around (pins 2 and 3 of the op-amp).

We can see, too, that if we wave our hand between the torch and the sensor, we capture very brief transitions:

Zooming in, and placing a cursor (the dotted yellow line) on the 900mV voltage level, we can see that the op-amp indeed switches over at exactly the comparison voltage of 900mV.

Zooming in even further, we see that the rise time (time to switch over) is only 76 microseconds:

We can now detect these changes in a small program:

// Example of flashing pin 13 based on the value on pin 2, when an interrupt occurs

// Author: Nick Gammon
// Date: 27 March 2011

// interrupt service routine
void light_change ()
  digitalWrite (13, digitalRead (2));  

void setup()
  pinMode(13, OUTPUT);  
  attachInterrupt(0, light_change, CHANGE);
}  // end of setup

void loop() 

  // all done by interrupts
}  // end of loop

This program turns the LED on pin 13 on or off depending on whether or not the light sensor has reached our critical point. It is done in an interrupt service routine, so we can respond almost instantly, even if the program is doing something else.

If you wanted more control over the cross-over point, instead of the two resistors which set the 0.9V level, a potentiometer could be used instead, and then you just "dial-up" the point at which you want the interrupt to occur:

This particular op-amp chip actually contains two op-amps. You can use pins 5, 6 and 7 to access the second one (they share power and ground). Thus a single chip could actually be used to convert two analog signals into digital ones.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]