Tag Archives: LM358

Use a Comparator or Op-Amp to Simplify Light Dependent Resistor Output

If your project calls for light sensitivity, it’s hard to beat light dependent resistors (LDRs), also known as photoresistors. They’re available for a few cents each, and their resistance varies based on how much light they receive. In the dark, these devices produce resistances in the megohm range, and can fall to hundreds of ohms or even less when exposed to sufficient light. You first instinct when prototyping this type of device is likely to use an analog input on an Arduino or similar dev board to sense voltage levels. This works quite well in many situations, but you may also want to consider a comparator or operational amplifier (op-amp) to turn this analog input into a simple on/off signal. You could also use one of these components by itself to produce a usable output without the use of a microcontroller.

LDR Analog Input to Microcontroller

An LDR setup for Arduino Analog Input. Illustration: Jeremy S. Cook in Fritzing

First, let’s examine how a microcontroller would see an LDR input. Using the circuit illustrated in the figure above with an Arduino Uno, an LDR is attached to 5VDC, then routed to the analog input A0. Voltage at the intersection of A0, the resistor, and LDR is divided between the fixed resistor and LDR, which decreases its resistance as light is applied. Voltage at this analog input increases with the lowered resistance in proportion to the amount of light the LDR sees.

The Arduino board is thus able to sense the resulting voltage level and convert it to an analog value. A threshold can be setup to respond to different light levels as on or off, or the analog signal can be used for proportional control. Note that the resistor in this illustration is just a placeholder; it would need to be adjusted based on your LDR sensitivity. You can also use a trimming resistor to tweak output values as needed.

Comparator Digitizes Analog Signal

What if you need light input, but just want an on/off value? Analog inputs can handle this programmatically, but if you’re using an Arduino Uno you’re restricted to the 6 analog pins. There’s also the normally minor issue of additional program complexity. If you need more performance out of your setup, you could turn to a comparator, or operational amplifier (op-amp) set up to act as one, to convert this analog value into a simple on/off signal.

Caption: An LDR and LM358 Op-amp setup to detect light as a binary signal to an Arduino Uno Illustration: Jeremy S. Cook in Fritzing

For example, if you were going to use an LM358 op-amp and LDR to detect light, you could tie the V+ (pin 8) to the 5V supply of your Arduino, ground (pin 4) to the Arduino’s ground, and output A (pin 1) to a digital pin on your Arduino board. The inverting input (pin 2) would be hooked to a voltage divider between +5V and ground, and your LDR would be setup in a voltage divider on the non-inverting input (pin 3). Here the LDR would act as the resistor from +5V to the op-amp input, and the set resistor would run to ground.

This will give you an on/off input to your Arduino without mucking about with any extra programming. Note that because of the way this op-amp operates, the output will be less than 5V, but will be sufficient to trigger the needed input. Obviously this will add some wiring complexity—more work than a few lines of code—so it’s not ideal in all situations.

Comparator Sans Arduino

You’re probably wondering at this point why you wouldn’t simply get a dev board capable of more analog inputs if that’s what is needed. After all, hooking up additional wiring or adding more complication to your PCB isn’t trivial. Certainly there are some applications that call for this, but for really simple electronics, you may not need a microcontroller at all.

Caption: An LDR and LM358 Op-amp setup to turn an LED on when there isn’t sufficient light available.
Illustration: Jeremy S. Cook in Fritzing

One such simple application would be a light that you want to come on when the ambient light drops below a certain threshold. In this application, you’d want to put the resistors only voltage divider on the non-inverting input (pin 3), while the LDR voltage divider would be placed on the inverting input (pin 2). This would cause the voltage on pin 2 to be larger than pin 3 when the light is on, turning the output (pin 1) on when there isn’t enough light.

Of course LDRs are but one type of sensor, and there are many models of op-amps and comparators with different characteristics available depending on your needs. If you’re just starting out with sensors and electronics, using a dev board like an Arduino is a great choice. As you advance in your knowledge, you might also consider analog electronics for your builds. While not appropriate or necessary for every project, it’s a great tool to have available when purely digital processing doesn’t quite fit your application.

Jeremy S. Cook and Zach Wendt are engineers who enjoy sharing how electronic components can best impact applications. Jeremy writes for a variety of technical publications. Zach works for Arrow Electronics, a major supplier of Arduino products.

Current Sensor Amplifier & Over Current Switch

Current sensor amplifier and over current switch project is based on ACS714-30A current sensor and LM358 Op-amp, ½ of  LM358 op-amp used as an amplifier for low voltage  and 2nd 1/2 LM358 op-amp used as comparator which provides over current TTL output, trimmer potentiometer provided to set the over current limit.  ACS714 sensor measures the current up to +/-30Amps, final output of the amplifier is 235mV/1A, and normally over current output is High-TTL, its goes low once the current over shoot than a set point. Circuit requires 5V DC and 40mA, Onboard LED indicates the power. Resistor divider R1, R3 provides bus voltage output for micro-controller interface to measure the bus voltage, choose appropriate value for R3, R1 as per your application and bus voltage, it’s should be less than 5V DC.

Current Sensor Amplifier & Over Current Switch – [Link]

Simple Pressure Sensor Amplifier & Over Pressure Switch

The pressure sensor amplifier built using LM358 op-amp and MPXM2051GS pressure sensor from NXP semiconductor.  The circuit provides 4V output for full scale pressure input 0-7.5PSI.  One op-amp is used as amplifier and 2nd op-amp is used as comparator to provide an output at set value that can be used as over pressure switch to control a pump or solenoid.  This is a low cost general-purpose circuit for those applications where +/-3% performance is acceptable. Multi turn potentiometers are provided for Offset, span adjust & over/under Pressure set point to control output devices like solid state relay, Pump, and solenoid.


  • Supply 12V DC
  • Pressure Sensor range 0-7.5PSI
  • Output 0-4V (Approx.)
  • PR1 Multi-Turn Potentiometer Offset
  • PR2 Multi-Turn Potentiometer Span Set
  • PR3 Multi-Turn Potentiometer Comparator (Switch) output Set
  • D1 Power LED
  • CN1 4 Header Connector Outputs & Supply Input

Simple Pressure Sensor Amplifier & Over Pressure Switch – [Link]

Heat Activated Cooling Fan Controller


Heat activated cooling fan controller is a simple project which operates a brushless fan when the temperature in a particular area goes above a set point, when temperature return normal, fan automatically turns off. The project is built using LM358 Op-amp and LM35 temperature Sensor. Project requires 12V DC supply and can drive 12V Fan. This project is useful in application like Heat sink temperature controller, PC, heat sensitive equipment, Power supply, Audio Amplifiers, Battery chargers, Oven etc

Heat Activated Cooling Fan Controller – [Link]

Sound to Light Effect


This project will turn your favorite music into light effects, a microphone picks up the sound and gives  light effects with 6 Red LEDs, ideal for creating fun atmosphere at parties & discos. Supply 9V-12V DC.


  • Supply 9V-12V DC (PP3 9V Battery)
  • On Board Preset for Gain Adjust
  • 6 On Board LEDs
  • On Board Condenser Microphone
  • SMD based small Board
  • PCB Dimension 45.72 X 28.58 MM
  • CN1 Supply Input
  • PR1 Sensitivity Adjust

Sound to Light Effect – [Link]

LPG Sensor Alarm


sameer @ sgprojects.co.in has build a LPG gas detection circuit based on LM358:

LPG gas detection project’s main idea is to implement the security system for detecting leakage of liquid petroleum gas in closed environment. In this project gas leakage is identified by using MQ6 gas sensor. A simple circuit with an Op-Amp IC is powered by a step down transformer and finally the alarm produces on detecting gas.

LPG Sensor Alarm – [Link]