Memo: One-shot timer and op-amp filters

Integrators and Differentiators are basic building blocks of analog computers. They enable summation and subtraction operations (hence, the core component of them is called “operational” amplifier). For multiplication, multiple integrators are used in parallel, along with exponential and logarithmic elements (non-linear op-amp circuits), to achieve the effect via the following transformation:

ln(ab) = ln(a) + ln(b) = c

e^(c) = ab 

This is why analog multiplicators are expensive, costing up to $20 a pop.

All the circuits below require very specific values for their components. Changing the value of the components will do more than changing the properties of the output, it can cause instability and disrupt the circuit’s operation. As an oscilloscope is required to diagnose these circuits, some preset values that have been tested in the lab are also included in this memo for DIY projects at home.

Monostable multivibrator (one-shot timer)

Monostable multivibrator has one stable state and it will change to the unstable state for a period of time when a trigger pulse (negative edge) is introduced as input.

The circuit can be derived from astable multivibrator circuit; the only new addition is the grounded diode in parallel to the output capacitor. If all values are appropriate, connecting the diode will dampen all output and (negative) feedback generated by the powered astable multivibrator without any input. After this dampening characteristic has been achieved, negative edge trigger input can be introduced to complete the circuit.

The basic circuit is as seen below:

basic op amp monostable
Basic monostable multivibrator: R1 = 10K, R2 = 2.2K, R = 1K, C = 1uF
op amp monostable waveforms
Measuring the voltage behavior across the capacitor C yields a shark-fin waveform

The timing period T is the amount of time it takes for the circuit to return switch from unstable back to stable state. The timing period is given by

T = RC ln[1 / (1-B)]

where B is the regenerative feedback as described in Memo: Schmitt trigger. The units of the remaining variables are as follow:

T: seconds (s)

R: ohms (Ω)

C: farad (F).

Cheat sheet: when R1 = R2, the timing period T = 0.693 RC

Similarly, the charging period is the amount of time the circuit must wait before it can be triggered again. This is given by

T(charging) = RC ln[(1+B) / B]

In some circuits, an additional RC differentiator circuit can be connected to the input (sometimes, only a single 0.01uF capacitator is sufficient). The purpose of this extra circuit is to transform rectangular signal into trigger pulse signal as seen below

rc differentiator circuit
RC differentiator

The complete monostable circuit is as follow:

op amp monostable circuit
Final monostable multivibrator with RC differentiator

Integrator functions like an average filter, it’s often used as a low-pass filter.

Inverting integrator: C = 0.01uF, R = 1K

The output of the integrator is given by

V_{{{\text{out}}}}(t_{1})=V_{{{\text{out}}}}(t_{0})-{\frac  {1}{RC}}\int _{{t_{0}}}^{{t_{1}}}V_{{{\text{in}}}}(t)\,\operatorname {d}t

or in Laplace domain, it is

Vout = -Vin/(sRC)

If the integrator starts from zero (no charge in the capacitor), the output is simply given by

-{\frac  {1}{RC}}\int _{{t_{0}}}^{{t_{1}}}V_{{{\text{in}}}}(t)\,\operatorname {d}t

where Vout(t0) represents the output voltage of the circuit at time t = t0.

Op-amp integrator suffers from the same frequency response limitation as other closed-loop op-amp circuits. It has a cut-off frequency at -3 dB and a decreased output at high frequencies. In addition to this, the integrator also has run-away output issue where it can drift to either power rail due to constant noises and it must be reset periodically to prevent this problem.

The drift is caused by any of the three conditions:

The input Vin has a non-zero DC component,

Input bias current is non-zero,

Input offset voltage is non-zero.

A more complex, grounded integrator circuit prevents this drift

Grounded integrator circuit

A simple switch in parallel to the negative feedback capacitor allows resetting the integrator to zero.

For the grounded integrator circuit, the output is given by

V_{{{\text{out}}}}(t_{1})=V_{{{\text{out}}}}(t_{0})-{\frac  {1}{R_{{i}}C_{{f}}}}\int _{{t_{0}}}^{{t_{1}}}V_{{{\text{in}}}}(t)\,\operatorname {d}t


Differentiator, in contrast, is a high-pass filter. It has poor high frequency response and any sudden disturbance at the input will cause it to ring at natural frequency

Op-Amp Differentiating Amplifier.svg
Inverting differentiator: C = 1F, R = 1K

The transfer function of the above circuit is as follow:

V_{{{\text{out}}}}=-RC\,{\frac  {\operatorname {d}V_{{{\text{in}}}}}{\operatorname {d}t}}\,\qquad {\text{where }}V_{{{\text{in}}}}{\text{ and }}V_{{{\text{out}}}}{\text{ are functions of time.}}

or in Laplace domain:

Vout = -sVinRC

Read more


Memo: Schmitt trigger

Hysteresis definition is as follow:

the phenomenon in which the value of a physical property lags behind changes in the effect causing it, as for instance when magnetic induction lags behind the magnetizing force.

The horizontal and vertical axes are input voltage and output voltage, respectively. T and −T are the switching thresholds, and and −M are the output voltage levels.

In asymmetric bipolar power supply (such as those created by elevating a virtual ground from unipolar power source), the hysteresis can be skewed along the horizontal axis of the above transfer function.

Schmitt triggers vs. Closed-loop amplifiers

It is easy to convert a closed-loop amplifier into Schmitt trigger by swapping the input terminals. Standard closed-loop amplifiers have negative feedbacks while Schmitt triggers have positive feedbacks as defined by Wikipedia:

In electronics, a Schmitt trigger is a comparator circuit with hysteresis implemented by applying positive feedback to the noninverting input of a comparator or differential amplifier.

One important note when converting negative feedback comparators into Schmitt triggers is the direction of the output. As seen below, non-inverting amplifiers will yield inverting Schmitt triggers when the input terminals are swapped, and vice versa.

Non-inverting amplifier (negative feedback)
Inverting Schmitt trigger (positive feedback)

Fortunately, changing the output direction of a comparator circuit (any feedback) is quite simple. Swapping Vin and GND terminals inverses the output direction of the circuit.

Non-inverting Schmitt trigger, practically identical layout with the inverting counterpart except Vin and GND positions
Schmitt trigger’s characteristics

Schmitt triggers are commonly used for switch debouncing and noise filtering for digital signals. In digital circuits, noisy signals are often fed to a low-pass filter to create a smoother signal before passing through a Schmitt trigger to recreate the sharp digital signal. Despite its importance in digital circuitry, the trigger itself is an analog component and is sometimes omitted in digital circuit simulation libraries.

Due to open-loop nature of the circuit, the gain of Schmitt trigger is infinity. Impedance values follow normal op-amp characteristics.

The regenerative feedback refers to the portion coming out of the voltage divider and into the non-inverting input. It’s denoted with beta symbol and defined as follow:

B = R1 / (R1 + R2)

The switching thresholds are calculated as the regenerative feedback times the positive or negative supply rail voltage Vs as seen below

V_\mathrm{+} = \frac{R_1}{R_1+R_2} \cdot V_\mathrm{s}

Please note that despite the formula, in reality, the Vs value in this calculation experiences some voltage drops (around 15%) due to internal impedance; hence the switching thresholds might be lower than calculated.

Astable multivibrator (Relaxation oscillator)

With a few extra components (a capacitor and a resistor), a Schmitt trigger can be adapted into an astable multivibrator. From inverting Schmitt trigger, add an additional negative feedback using a resistor, a capacitor and ground to create the astable multivibrator.

Astable multivibrator adapted from inverting Schmitt trigger circuit

The multivibrator does not take in any input signal. When powered up, it creates a full-range square waveform across the op-amp’s output and a half-range waveform across the non-inverting input (some documents denote this waveform as reference voltage). At the same time, it creates a half-range ramp waveform across the capacitor.

Transient analysis of a comparator-based relaxation oscillator.

The time period of the multivibrator is given by

T = 2 RC ln[(1+B)/(1-B)]

with B is the regenerative feedback, R and C are the value of the resistor and the value of the capacitor across the negative feedback respectively.

The amplitudes of half-range voltages are calculated using the same formula as the switching thresholds of normal Schmitt triggers.

When using an asymmetric bipolar supply, the switching thresholds of the multivibrator will be skewed and it will produce waveform of adjustable duty cycles. The duty cycle is dependent on the offset; left offset produces shorter duty cycle and right offset produces longer duty cycle. However, the device can only accept so much power supply offsetting before it cannot operate (under load) or overloaded.

Read more


Memo: Op-amp’s gain factor and noise problems

First, here’s a quick cheatsheet on closed-loop gain factor for inverting and non-inverting amplifiers. The gain of inverting amplifier is given by the resistance R2 across the feedback loop divided by the resistance R1 across the forward input.

A = R2/R1

Meanwhile, the gain of non-inverting amplifier is given by

A = 1 + R2/R1

as calculated in the previous note. Kirchhoff’s current law is applied.

Gain factor and cut-off frequency relationship

Cut-off frequency is not only affected by the properties of a given component but it is also dependent on the gain factor of the amplifier circuit. Higher gain amplifiers have lower cut-off frequencies and this can be charted as an exponential decay function.

To easier express this function in linear terms, the relationship is converted to logarithmic terms:

20 log(0.5) = -6.02 [dB]

with 0.5 is the approximated V/Vref value from experimentation.

On a side note, below are two commonly seen decibel functions in electronics for power and voltage respectively.

{\displaystyle G_{\mathrm {dB} }=10\log _{10}\left({\frac {1000~\mathrm {W} }{1~\mathrm {W} }}\right)=30.}

{\displaystyle G_{\mathrm {dB} }=20\log _{10}\left({\frac {31.62~\mathrm {V} }{1~\mathrm {V} }}\right)=30.}

Naturally, the gain factor for op-amps is expressed in terms of voltages (the second formula).

Differential amplifier

A useful circuit using op-amps to amplify and de-noise a weak signal is differential amplifier. The basic circuit uses one op-amp in the arrangement below:

V1 and V2 inputs are the same signal, one of which is inverted. This can be done using an inverting amplifier with gain factor of 1 (inverting unity gain amplifier) but this arrangement is subjected to high-frequency quirks as stated in the previous memo, especially phase shifting property.

If the inputs are identical and not phase-inverted, the output will be zero.

The output of this particular circuit is an amplified signal with transmission line noise suppressed. Note that the transmission line noise being suppressed is the one common on both inputs after the inversion. Noises occurred before inversion cannot be suppressed this way.

Instrumental amplifier (three op-amps)

Normally, to ensure proper signal flow, the inputs are tunneled through two additional voltage followers (two more op-amps) in order to reduce the impedance. The full circuit (called “instrumentation amplifier) would then look like the following:

with all R values are the same except for Rgain.

Rgain can be of any value. It serves as a “common ground” connector and it also tweaks the voltage gain of the full circuit. Increasing the value of Rgain decreases the gain of the differential amplifier.

In reality, the gain of the above circuit can be tweaked further, following this function

{\frac {V_{\mathrm {out} }}{V_{2}-V_{1}}}=\left(1+{2R_{1} \over R_{\mathrm {gain} }}\right){R_{3} \over R_{2}}

Instrumental amplifier (two op-amps)

An alternative instrumental amplifier design using two op-amps can be seen below:

Though this setup saves on component costs, it does have a few disadvantages, notably, the lack of support for unity gain (not a problem for most scenarios but can be if the instrumental amplifier is used solely for noise canceling).

Furthermore, the circuit is unbalanced. The leftmost amplifier increases the input slightly and introduces some signal delay. This unbalance leads to reduced noise canceling capability. The output can saturate if the common-mode noise of the input signal here is too high and race condition can lead to a much lower cut-off frequency (compared to three op-amps version).

The gain factor here is controlled by RG in the same manner as Rgain in the other circuit.

G = 1 + R2/R1 + (2*R2)/RG

Bonus: Choosing the base resistance

When calculating the resistance of an analog system, the first resistor (base resistor) is chosen as a compromise between power consumption and noise tolerance. Low resistance (or impedance for AC systems) allows more current draw. This is welcome when loads are concerned but it is a waste of energy when sensors are concerned. The higher the base resistance, the lower the power consumption will be.

On the other hand, high resistance circuits are more susceptible to noise. The relationship between resistance and noise is given by Johnson-Nyquist noise voltage function

{\overline {v_{n}^{2}}}=4k_{\text{B}}TR

where kB is Boltzmann constant 1.38 x 10^-23 [J/K], T is the absolute temperature in Kelvin [K], and R is the system’s resistance. In other words, a small increase in resistance increases in Johnson noise voltage by the power of two.


Increase R, increase noise, decrease current consumption

Decrease R, decrease noise, increase current consumption

Choose the right compromise for the application.

Bonus: Using op-amp with a unipolar power source

Unipolar or single-rail power sources that only have GND and VCC terminals need to offset the ground terminal to create a -VCC source. This offset can be done using a voltage divider circuit in conjunction with a voltage follower. Be mindful that the with only half of the voltage range, the op-amp might experience unexpected floating values.

The voltage follower (or unity gain amplifier) must be connected in series to the middle point of the voltage divider in order to create a new, offset virtual ground. This eliminates the added impedance from the voltage divider, ensuring sufficient current draw power devices using the virtual ground.

For example, such a system of voltage divider and voltage follower can be employed to create +2.5V and -2.5V bi-polar supply from Arduino’s 5V and 0V unipolar supply. In this case, the op-amp will produce 5V (relative to the Arduino’s true ground) as HIGH signal and 0V as LOW signal. Inversely, it will see 0V from the Arduino as -2.5V (relative to the virtual ground) and 5V as +2.5V.

The previous section on choosing base resistance also applies here, and even more so with the halved voltage range doubling susceptibility to signal distortion.

Read more

Memo: Op-amp’s analog characteristics

Operational amplifier (op-amp) possesses an open-loop gain factor A0 and a close-circuit gain factor A.

The negative terminal of the op-amp is called “inverting” terminal and the positive terminal is called “non-inverting”. In addition to two input terminals, there are two power terminal V+vcc and V-vcc.

The output Vout is calculated as

Vout = A * (V+in – V-in).

Ideal op-amp has the following characteristics:

Input impedance rin approaches infinity.

Meanwhile, output rout impedance approaches zero.

Unity gain amplifier

These characteristics see applications in voltage follower (also known as unity gain amplifier), which produces an output voltage equal to input voltage but with much lower impedance. Low impedance circuits allows more current draw than high impedance ones.

Voltage follower setup: a simple negative feedback loop without any resistance

This unity gain is possible because the gain factor used here is open loop

Vout / Vin = A/ (1+A0)

as Aapproaches infinity

Vout / Vin = 1

Gain of closed-loop amplifiers

In other cases, the gain is determined by the values of the voltage divider overlaying the feedback loop. For example

A non-inverting amplifier: negative feedback with voltage divider setup

In this case, the gain is determined using Kirchhoff’s law. Since Kirchhoff’s current law states that the same current must leave a node as enter it, and since the impedance into the (−) pin is near infinity, we can assume practically all of the same current i flows through Rf, creating an output voltage

{\displaystyle V_{\text{out}}=V_{\text{in}}+i\times R_{f}=V_{\text{in}}+\left({\frac {V_{\text{in}}}{R_{g}}}\times R_{f}\right)=V_{\text{in}}+{\frac {V_{\text{in}}\times R_{f}}{R_{g}}}=V_{\text{in}}\left(1+{\frac {R_{f}}{R_{g}}}\right).}

Finally, we have the following closed-loop gain (applicable only to the above circuit)

{\displaystyle A_{\text{CL}}={\frac {V_{\text{out}}}{V_{\text{in}}}}=1+{\frac {R_{f}}{R_{g}}}.}

High frequency characteristics

The gains and formulas above are only applicable for low frequency circuits. In high frequencies, the following will happen:

Above certain frequencies, voltage gain and current gain factors become diminished in inverse proportion to frequency increase.

The output signal becomes lagged behind the input signal (phase shifting) as the op-amp cannot react fast enough to the frequency changes.

Very high input amplitude (peak-to-peak voltage swing of electrical signal) causes distortions and the higher the frequency, the lower this upper amplitude threshold is (distortion appears at lower amplitudes at high frequencies).

Cut-off frequency is the frequency at which the voltage gain is 1/sqrt(2) of the low frequency gain, meaning, the power at the output is effectively halved in voltage follower setup. The cut-off frequency is unique to each op-amp component. It can be easier determined using an oscilloscope as the frequency at which the phase shift is 180o.

Textbook definition:

The frequency where the voltage falls to 0.707 of its intended value is the cutoff or -3 dB frequency, fc. (Gain in decibels = 20∙log(0.707) = -3dB.)

As a result of these special high frequency characteristics, op-amps can be unsuitable for high frequency applications.

Read more

DIY Memo: ESP8266 v7 hookup

Same story as the last time, but with ESP8266 v7 this time.

This is the hook-up instruction for ESP-07 and how to capitalize on its GPIO pins.


Things to know:

– 3.3V power source

– Default baud rate: 115200 (programmable at 9600)

– R1, R2, R3, R4 are all 10K

– C1 is 0.1 uF (ceramic capacitor 104)

– The solder pads are slightly smaller than the standard Arduino pin’s dimension. Use a small drill, purchase specialized pins or solder wires directly.

– GPIO2 -> HIGH during startup

– GPIO0 -> HIGH (standard) or GPIO0 -> LOW (flash) during startup

– GPIO15 -> LOW (standard) or GPIO15 -> HIGH (boot from SD card) during startup

– RXD is GPIO3 and can only be used as input. Similarly, TXD is GPIO1 and can only be used as output

– Deep sleep requires connecting GPIO16 -> RST to wake up.

– A diode from RST -> GPIO16 is necessary to use wake up and reset switch at the same time. The cause is GPIO16 producing HIGH current in deep sleep and it prevents reset switch from working. A diode blocking this current solves the issue.

– ADC is the only analog pin available. It is GPIO6 or A0 (alias) in Arduino language.

ESP8266 pin I/O support
GPIO0 INPUT with pull-up, jumper and delayed hook-up via a sacrificial GPIO pin which is only grounded after boot
OUTPUT with external device between VCC and GPIO0 (GPIO0 -> HIGH, device -> LOW)
GPIO2 INPUT with pull-up and delayed hook-up via the same sacrificial GPIO pin as GPIO0
OUTPUT with external device between VCC and GPIO2 (GPIO2 -> HIGH, device -> LOW)
GPIO15 OUTPUT with external device between GND and GPIO15 (GPIO0 -> HIGH, device -> HIGH)
RST or DTR to wake up from Deep Sleep mode. Include a diode from RST -> GPIO16 if reset switch is used
GPIO6 (ADC, A0) Analog INPUT

Read more:

DIY Memo: ESP8266 v1 hookup

Tonight marks the third time I forgot how to hook up this wifi module already, I might as well leave a memo here for my forgetful future self.

This is the hookup instruction for ESP8266 v1 using CP2012 and Tera Term software


Things to know:

  • 3.3 VDC source
  • RX and TX can accept 5V logic level without problems (so far).
  • 5V output of an Arduino Uno (externally sourced) through AMS1117 provides enough current for ESP8266 boot up.
  • Default: 115200 baud rate
  • Tera Term > Setup > Terminal > New-line transmission mode: CR+RF
  • Flash mode: GPIO 0 + RESET -> GND then RESET -> float and finally GPIO 0 -> float
ESP8266 CP2012 / Source
GND GND (common to CP2012)

Technical aspects of computer visual system

Here’s a few technical aspects of computer visual system I wrote two months ago in response to a complaint regarding scroll bars in wiki articles on wide screens.

First, we have the sampled image dimension in pixel resolution. Then, we have the physical dimension of the monitor in inches. Finally, we have the all-so-important parameter no one gives a damn about called “dpi” or “dots per inch”.


Each inch of the screen consists of a number of dots. Each pixel of the digital image is physically displayed using a cluster of dots. Dpi value links the virtual dimension of an image to the physical dimension of a screen.

For example, 1024×768 pixels can be displayed using 1024×768 discrete dots and if we have the dpi to be 64, it will translate into a 16″ x 12″ (or a 20″ screen, diagonally). Increase the dpi to 128, it will become a 10″ screen.

In this manner, I can ramp up the dpi to 256 or higher and get things displayed in a 5″ monitor. However, I won’t be able to see a damn without a magnifier as the image’s details will be really small. Hence, to the user, the physical dimension (measured in inch) of the screen matters the most and is the determining factor whether I want to view the web page at 100%, 120% or 150% zoom ratio.

Zoom ratio:

Because physical devices don’t just shrink or expand, we invented the scroll bars to emulate expansions in physical dimension. Instead of having two 5″ screen side-by-side to double the area, we have one 5″ screen and a 50% scroll bar. In principle, the zoom ratio ties the emulated dimension to the actual dimension.

A screen at 200% zoom ratio will measure an image in inches the same as a 100% zoom screen of twice the physical size will. Arguably the resolution will seem lower if the screen didn’t have enough (or small enough) dots to meet dpi requirement. The effect is that we have lower resolution at a certain dpi above a certain zoom ratio.

Try this one exercise:

Adjust the browser’s zoom ratio and see the scroll bars:

1) Appears or widens when zoom out (smaller image, less scrolling)

2) Shrinks when zoom in (larger image, more scrolling).

Human perception has its limits, plus, some people are more impaired than others, they have their own preferences when it comes down to zoom ratio of the browser, which affects the interpretation resolution of the displayed image.

That’s why people who have 20″ screens will likely to view at 100% zoom (since the image resolution won’t be so small) and people who have 10″ screens will likely to zoom out to 200%  in order to emulate the effect of viewing a 1024×768 px image in 512×384 px (by design) monitor.

A common misconception I have seen over and over again is the rated p value of a monitor. When a monitor is rated at 1080p, it means we have 1920×1080 dots (or any multiple of the resolution) across your screen and it is fully capable of producing full HD images. It says nothing about the physical size of the hardware (or the actual size of the dots for the matter).

For this reason, it’s incorrect to assume a $100 monitor won’t get scroll bars and weird stuffs. It does, just as others do with $20 tiny monitor, at a certain zoom ratio. And since zoom ratio is dictated by the viewer’s eyesight, two same screens might not see the same layout the same. Saying the layout is uniformed across devices, even among those with the same dimension, is naive.