Op-Amp Basics with the LM358: Amplifiers, Comparators and Sensor Conditioning

The LM358 is the cheapest op-amp in every student kit, and it is all you need to learn the two circuits that matter: the inverting and non-inverting amplifier (gain = resistor ratio) and the comparator (sensor threshold into a digital signal). This guide works the gain math with real resistor values, explains single-supply limitations, shows sensor signal-conditioning circuits for LDRs, thermistors and current sensing, and lists the classic wiring mistakes that make an op-amp circuit sit at the rail.

Written by Projectech14 min readPublished
For B.E./B.Tech Electronics and E&TC students learning analog circuits for sensor projects Topics: LM358, Op-Amp, Signal Conditioning, Comparator, Sensor Interface
Illustration of an LM358 op-amp chip on a breadboard with resistors, an oscilloscope showing amplified waveforms, and multimeter probes.
Illustration generated for this guide.
In this guide

Every sensor project hits the same wall sooner or later: the sensor's output is too small, too wobbly, or the wrong shape for the microcontroller to read directly. A thermistor gives you a resistance, not a voltage. A photodiode gives you microamps. A microphone gives you millivolts riding on noise. The component that turns all of these into clean, ADC-friendly signals is the operational amplifier — and the LM358, at a few rupees a chip, is the one you will meet first.

This guide teaches op-amps the way a builder needs them: the two amplifier configurations and their gain math, the comparator as a sensor-threshold device, single-supply behaviour (the LM358's most misunderstood trait), and complete signal-conditioning circuits for the sensors students actually use. The worked values use standard E12 resistor values you can buy anywhere.

What an op-amp is, in one paragraph

An operational amplifier is a differential amplifier with enormous open-loop gain (typically 100 dB, i.e. 100,000×), two inputs (inverting − and non-inverting +), and one output. You never use that raw gain — you wrap the op-amp in a resistor network that trades the excess gain for precision, setting the closed-loop gain with resistor ratios. The two golden rules of ideal-op-amp analysis (which hold well enough for every circuit in this guide):

  1. No current flows into the inputs (input impedance is very high — megaohms on the LM358's bipolar inputs, effectively infinite for analysis).
  2. With negative feedback, the output drives itself to whatever voltage makes the two inputs equal (the "virtual short").

If your circuit has negative feedback (output connected back toward the − input through resistors), apply those two rules and the gain falls out of simple algebra. If it has positive feedback or no feedback, the output slams to one of the supply rails — which is exactly how the comparator works.

Why the LM358 specifically

  • Dual op-amp in an 8-pin DIP — two independent amplifiers in one ₹10–15 chip, breadboard-friendly.
  • Single-supply operation — runs from one 5 V rail (3 V to 32 V range), which is what Arduino/ESP32 projects have. Most classic op-amps (741) need ±12 V split supplies; the LM358 does not.
  • Input common-mode range includes ground — you can sense signals all the way down to 0 V on a single supply. (It does not include the positive rail — see limitations.)
  • Output swings close to ground but not to the positive rail: on a 5 V supply, the output tops out around 3.5 V. This single fact explains half of all "my amplifier doesn't work" complaints.

LM358 pinout (DIP-8, notch up, pin 1 top-left)

Pin Function Pin Function
1 Output A 8 VCC+
2 Inverting input A (−) 7 Output B
3 Non-inverting input A (+) 6 Inverting input B (−)
4 GND / VCC− 5 Non-inverting input B (+)

Power: pin 8 to 5 V (or your single supply), pin 4 to ground. Add a 100 nF decoupling capacitor between pin 8 and pin 4, as close to the chip as possible — an op-amp without supply decoupling oscillates or behaves erratically, and this capacitor is the most skipped component in student builds.

The limitations you must design around

  • Output swing: 0 V to roughly (VCC − 1.5 V). On 5 V: 0–3.5 V. Your amplified signal must fit inside this window.
  • Not rail-to-rail input: inputs work from 0 V up to about (VCC − 1.5 V). A signal near 5 V on a 5 V supply reads wrong.
  • Slew rate 0.5 V/µs, gain-bandwidth 1 MHz: fine for sensor signals (DC to a few kHz), hopeless for video or fast pulses. A 1 MHz gain-bandwidth product means at a gain of 100, your bandwidth is ~10 kHz.
  • Input offset voltage: up to ~7 mV worst case on cheap grades. At a gain of 1000, that is 7 V of output error — the output would be railed. Keep high-gain stages below ~200× per stage, or null the offset.
  • Crossover distortion: the output stage has a small dead zone around zero that distorts low-level AC signals. For precision audio, look elsewhere; for sensor DC levels, irrelevant.

Circuit 1: the non-inverting amplifier

The input goes to the + pin; a voltage divider from output to the − pin sets the gain. Input impedance is very high (the signal sees the op-amp's input directly), which makes this the right choice when the source is high-impedance (a voltage divider sensor, a piezo).

Gain: A = 1 + Rf / R1

Worked example — amplify a 0–0.5 V sensor signal to 0–3 V for a 3.3 V ADC:

  • Target gain = 3 / 0.5 = 6
  • Rf / R1 = 5 → choose R1 = 10 kΩ, Rf = 47 kΩ (standard value, gain = 1 + 4.7 = 5.7, output 0–2.85 V — fits the ADC with margin)
  • Feed the sensor into pin 3 (+), connect R1 from pin 2 (−) to ground, Rf from output (pin 1) back to pin 2 (−).

Resistor choice guidance: 1 kΩ–100 kΩ is the comfortable range. Below 1 kΩ the divider loads the output and wastes current; above ~1 MΩ, the LM358's input bias current (tens of nA typical, up to ~250 nA worst case) flowing through megohms creates millivolt offsets that then get amplified. 10 kΩ-class values are the sweet spot.

Circuit 2: the inverting amplifier

The input goes through Rin to the − pin; Rf feeds back from output to −. The + pin is tied to a reference (ground, or mid-supply — see below).

Gain: A = −Rf / Rin (the minus sign = the output is inverted)

Worked example — amplify an AC-coupled 100 mV peak audio/sensor signal by −10:

  • Rin = 10 kΩ, Rf = 100 kΩ → gain −10, output 1 V peak inverted.
  • Input impedance equals Rin (10 kΩ here) — the source must be able to drive that. This is the inverting configuration's main weakness versus the non-inverting one.

The single-supply catch: with the + pin at ground and a single 5 V supply, the output cannot swing negative, so an AC signal's negative half clips at 0 V. The fix is to bias the + pin at mid-supply (2.5 V) with a resistor divider (two 10 kΩ from 5 V to ground, plus a 10 µF capacitor from the divider node to ground to keep it quiet), AC-couple the input through a capacitor, and treat 2.5 V as your signal's "zero". The output then swings 2.5 V ± the amplified signal, as long as it stays inside the 0–3.5 V window. This virtual-ground technique is worth learning properly — half of all single-supply analog design is just disciplined bookkeeping of where "zero" sits.

Circuit 3: the comparator — your sensor's digital output

Remove the feedback entirely (or add a little positive feedback — below), and the op-amp becomes a comparator: output high if V+ > V−, output low if V+ < V−. This turns any analog sensor into a clean digital threshold signal:

  • Dark detector: LDR in a divider into V+, threshold voltage (potentiometer) into V−. Output goes high when it gets dark — straight into a digital pin.
  • Temperature switch: thermistor divider vs a setpoint pot — a fan or heater relay driver without any code.
  • Battery-low flag: divided battery voltage vs a reference — an LED or MCU interrupt when the cell drops below the threshold.

Add hysteresis — the professional touch that separates working comparators from chattering ones. Without it, a slowly moving sensor signal hovering at the threshold makes the output oscillate as noise crosses the trip point. Feed a fraction of the output back to the + input through a resistor (R_hyst, typically 10–100× the threshold divider's impedance). This creates two thresholds — one for rising, one for falling — separated by a hysteresis band of roughly VCC × (R_div / R_hyst). A 50–100 mV band kills chatter from ordinary sensor noise. Every comparator in a student project should have hysteresis; the cost is one resistor.

One caution: the LM358's output swings to ~3.5 V max on a 5 V supply, which reads as HIGH on 5 V logic but is marginal for some 3.3 V inputs (usually fine, but verify your MCU's VIH). And a bare comparator output driving a relay coil needs a transistor relay driver (the standard module circuit shown in the Bluetooth home automation project) — the LM358 sources only ~20–40 mA.

Sensor signal-conditioning circuits that actually get built

Thermistor temperature sensing (NTC 10 kΩ)

The classic divider: NTC thermistor to 5 V, 10 kΩ fixed resistor to ground, tap to the ADC. At 25 °C the tap sits at 2.5 V; it moves roughly −20 to −40 mV/°C depending on the region. Two refinements:

  1. Buffer the tap with a non-inverting follower (output tied to − input, gain = 1) if the ADC or wiring loads the divider — the follower's high input impedance stops the measurement from sagging.
  2. Convert resistance to temperature in code with the Beta equation or a lookup table — the divider voltage is nonlinear in temperature, and students who assume linearity get errors of several degrees at the extremes. The Beta formula: 1/T = 1/T0 + (1/B)·ln(R/R0), with T in kelvin.

LDR light sensing with adjustable threshold

LDR + 10 kΩ divider into a comparator with hysteresis (Circuit 3), threshold set by a 10 kΩ potentiometer. This is the entire sensing stage of automatic street-light and solar-tracker projects — one LM358 half, four passives, no code involved in the decision. Calibrate the pot at the actual light level where switching should happen, not on the bench under tube lights.

Current sensing with a shunt resistor (low-side)

To measure a motor or load current: put a small shunt (e.g. 0.1 Ω, 2 W) in the ground return, amplify the voltage across it with a non-inverting gain of ~20–50, and read the result. 1 A through 0.1 Ω = 100 mV; gain 33 (Rf = 330 kΩ / 10 kΩ → gain 34) gives 3.4 V at 1 A. Keep the shunt on the low side (ground return) so the amplifier's inputs stay near ground — inside the LM358's happy common-mode range. High-side current sensing needs a different part; do not force the LM358 there.

Pulse / heartbeat signal chain (what the oximeter modules do inside)

A photodiode or phototransistor watching blood-volume changes produces a tiny AC signal (millivolts) on a large DC background. The standard chain: transimpedance or first gain stage → high-pass filter to strip the DC → second gain stage → comparator with hysteresis to produce clean pulses. The pulse oximeter module project packages this chain in a module, but understanding the stages is what lets you debug it when the readings are noisy — and the same chain appears in every ultrasonic distance meter style analog front end.

Reading the LM358 datasheet: the five lines that matter

Datasheets intimidate students because they are 20 pages long. For the LM358 you need five lines, and you need to know what each one costs you:

  1. Supply voltage range: 3 V to 32 V (single supply). Confirms you can run it from your Arduino 5 V or a 3.3 V rail. Below 3 V it misbehaves — do not try to run it from two AA cells.
  2. Input common-mode voltage range: 0 V to VCC − 1.5 V. Your inputs must live in this band. A sensor divider that swings to 4.5 V on a 5 V supply will read wrong at the top end — scale the divider down.
  3. Large-signal voltage gain: ~100 dB (100,000×). This is the open-loop gain you throw away to buy precision with feedback. It also tells you the virtual-short approximation is good to about 1 part in 100,000.
  4. Gain-bandwidth product: 1 MHz. Divide by your closed-loop gain to get usable bandwidth. Gain of 10 → ~100 kHz; gain of 1000 → ~1 kHz. If your sensor signal is faster than that, you need a faster part (or less gain per stage).
  5. Input offset voltage: 2 mV typical, 7 mV max. Multiply by your gain to get the worst-case output error. Gain of 50 → up to 350 mV of offset at the output. If that breaks your measurement, lower the gain, add a second stage, or calibrate it out in software.

How to calibrate out offset in software: short the sensor input (or cover the LDR, disconnect the probe — whatever "zero signal" means for your sensor), read the ADC value, store it as the zero offset, and subtract it from every later reading. This removes op-amp offset, sensor bias, and ADC offset in one step. Re-run the calibration when the temperature changes a lot.

Worked design: a complete soil-moisture interface

Tie the circuits together with one end-to-end example — a resistive soil-moisture probe feeding an ESP32 ADC (0–3.3 V range, and note the ESP32 ADC is nonlinear near the rails, so aim for a 0.2–3.0 V signal window):

  1. Sensor: the probe is two electrodes whose resistance varies roughly 10 kΩ (wet) to 100 kΩ+ (dry). Put it in a divider with a 47 kΩ fixed resistor to 3.3 V. Wet: tap ≈ 3.3 × 47/(47+10) ≈ 2.7 V. Dry: tap ≈ 3.3 × 47/(47+100) ≈ 1.05 V. The raw swing is 1.05–2.7 V — usable, but it loads the ADC and inverts the sense (dry = low voltage).
  2. Buffer: LM358 half A as a voltage follower on the divider tap. Input impedance now megaohms; the ADC sees a stiff source.
  3. Comparator: LM358 half B compares the buffered signal against a threshold from a 10 kΩ pot, with hysteresis (1 MΩ feedback to the + input). Output: digital "needs water" signal — dry soil pulls the tap below threshold, output swings high, straight to a digital pin or a transistor driving a pump relay.
  4. Power: LM358 pin 8 to 3.3 V, pin 4 to ground, 100 nF across them. Output swing on 3.3 V is 0 to ~1.8 V — fine for the ESP32's digital input threshold, but note the analog buffered signal (up to 2.7 V) exceeds the output swing. Fix: run the LM358 from 5 V instead — then the follower output can reach 3.5 V, so divide the buffered signal down with a resistor pair (e.g. 10 kΩ / 20 kΩ) before the ADC to keep it under 3.3 V. This is exactly the rail bookkeeping the limitations section warned about — do it on paper before soldering.

This is the analog front end behind projects like the soil EC mapper: resistive sensing, buffered, thresholded, digitised. Get the four steps right and the "sensor" half of the project stops being mysterious.

The debugging table: why is the output railed?

An op-amp output pinned at ~0 V or ~3.5 V (on 5 V supply) with no response to the input is the most common symptom. Read it like this:

Symptom Likely cause Check
Output stuck near 3.5 V, input changes do nothing Input beyond the common-mode range (near VCC); or feedback resistor open/missing Measure both inputs — are they inside 0 to (VCC−1.5 V)? Check Rf continuity
Output stuck at ~0 V Inverting amp on single supply with no virtual ground; or input below ground Bias the + pin to mid-supply for AC signals; check input polarity
Output oscillates (fuzzy on scope / unstable ADC) Missing supply decoupling; capacitive load; excessive gain with stray feedback Add 100 nF directly across pins 8–4; keep gain per stage under ~200; shorten wiring
Gain is wrong (e.g. 3× instead of 6×) Wrong resistor value read (color codes); Rf and R1 swapped Measure the actual resistors with a meter — see the project testing and debugging guide
Comparator output chatters at threshold No hysteresis; noisy sensor supply Add hysteresis resistor; decouple the sensor supply
Works on the bench, drifts in the enclosure Input offset + high gain; temperature drift of thermistor divider Reduce stage gain, split into two stages; verify offset contribution
Second op-amp in the package misbehaves Unused op-amp left with floating inputs — it oscillates and couples into the used half Tie unused amp as a follower: − input to its output, + input to ground (or mid-supply)

The unused-op-amp rule: never leave half of an LM358 with floating inputs. An open-loop op-amp with floating inputs picks up noise, oscillates, and injects it into the supply rails shared with the half you are using. Wire it as a voltage follower to a quiet DC level. One minute of wiring, hours of debugging saved.

Design checklist

  • Supply: single 5 V (or 3.3 V) with 100 nF decoupling at the chip.
  • Signal range fits the output window: 0 to (VCC − 1.5 V). On 5 V, keep amplified peaks under ~3.5 V.
  • Gain set by resistor ratio; resistor values in the 1 kΩ–100 kΩ band.
  • Non-inverting chosen for high-impedance sources; inverting only where the source can drive Rin.
  • Single-supply AC signals: + pin biased to mid-supply with a quiet divider, input AC-coupled.
  • Comparators: hysteresis resistor fitted, threshold calibrated at the real operating point.
  • Unused second op-amp wired as a follower, not left floating.
  • Gain per stage under ~200 so input offset does not rail the output.
  • ADC reference and op-amp output range matched (3.5 V max output vs 3.3 V ADC — usually fine, verify).

Putting it together

The LM358 rewards a specific discipline: know your supply rails, keep every signal inside the 0 to (VCC − 1.5 V) window, set gain with resistor ratios in the 10 kΩ band, and give every comparator hysteresis. With just the non-inverting amplifier, the inverting amplifier, the follower, and the comparator-with-hysteresis, you can condition nearly every sensor a final-year project uses — thermistors, LDRs, current shunts, microphones, and photodiodes. The soil EC mapper project is a good example of analog sensor conditioning feeding a digital system, and when the conditioned signal still misbehaves, the project testing and debugging guide walks through finding the fault stage by stage.

More project guides

More in Electronics / E&TC