Using Operational Amplifiers in your Arduino project

[u]Introduction: Using Op Amps to measure signals with an Arduino[/u]

Who is this tutorial for? Well, not "newbies" or anyone happy to just connect together modules. If you have a LITTLE knowledge of electronics, have perhaps used a transistor or a FET to extend the output capability of an arduino, and want to learn about ways to handle different Analog inputs, than read on!

What will you need? All you will need to carry out a few simple experiments is a breadboard, a couple of (very inexpensive) op amps - I'd recommend the MPC6002; a few resistors, maybe an LED, phototransistor or photodiode, and any other odd bits you have lying around.

What is a signal?

In our Arduino world its a value - usually a voltage or current - that carries information. Your Arduino will have signals going in - Analog or digital values; and signals out, to control motors, lights etc. This tutorial focusses mainly on issues in dealing with Analog input signals.

Measuring signals

The ANALOG inputs found on most Arduinos is well suited to measuring signals of a few volts from a low resistance source, but there ARE signals that dont match well to those analog inputs. For example many high quality sensors produce only very small currents or voltages, and have a very high resistance, so they arent suited to direct measurement with a conventional Analog to Digital Converter (ADC). Examples include Photodiodes, Piezoelectric Sensors, Accelerometers, Hydrophones, Humidity Monitors, pH Monitors, Chemical Sensors, and Smoke Detectors. Even common sensors like audio pickups and microphones will have their performance impaired if connected to a low resistance input.

Operational amplifiers ("Op Amps") offer an easy way to prepare these signals for measurement.

This tutorial aims to provide an introduction to the use of Op Amps, and offer help in choosing the right one for your particular application.

Topics: 1: Measuring voltages with an Arduino 2: What is an "Op Amp" 3: Basic circuits using Op Amps 4: Example application 1: Photodiode amplifier, including sketch 5: Choosing the right Op Amp for your application 6: Powering your Op Amp circuit - single or split supplies 7: Important characteristics explained 8: Example 2: Small signal with dc offset - using a difference amplifier

I'd like to acknowledge the massive help given to me in developing this tutorial by: Perry Bebbington, Idahowalker, Robin2, MarkT, Raschemmel and Southpark.

Comments feedback or questions on this tutorial to this thread please:

https://forum.arduino.cc/index.php?topic=722227.0

[u]1: Measuring voltages with an Arduino[/u]

Operational amplifiers are simply devices that amplify the difference between two inputs. However these simple devices can be used in combination to create many useful circuits. If you want to measure a signal voltage with an Arduino, you may face some of the issues described here; often a simple circuit using an operational amplifier can help.

Lets look at these problem issues.

The Arduino Uno has ADC inputs that can measure voltages in the range 0 to 5V.

(other ranges are available on different versions)

If the signal is outside this range - as the blue line on this diagram - it can't be measured directly. An inverting amplifier will produce a positive voltage of the same value, which can then be measured.

Also the signal may be too small to measure; (green line) in which case a non-inverting amplifier as described below can help. If the signal is changing between 0.1 and 0.2V an amplifier with a gain of 20 will give a signal in the range 2 - 4v which matches the measurement range of the Arduino.

The cyan line is a bit different. Suppose the signal is changing between 3.4 and 3.6V - a change of 0.2V; we could amplify it by ten - but then it would be changing between 34V and 36V - still no good. However using a difference amplifier we can take the 3.4 - 3.6, SUBTRACT 3.2, and multiply by ten - giving a nicely measureable signal in the range 2 - 4 Volts.

The operational amplifier circuits described in [u]Reply 3[/u] will allow you to perform this "signal conditioning".

opamp signals1.png|400x392

[u]2: What is an "Op Amp"?[/u]

An Operational Amplifier is .. an amplifier; however it has special characteristics that make it very easy to design circuits for particular applications. Fundamentally it takes two inputs, shown here as Va, Vb, and gives an output voltage Vo which is bigger than the DIFFERENCE between Va and Vb. The amount by which it is bigger is the "open loop voltage gain (Avol)" of the amplifier.

Vo = Avol (Va - Vb)

An "ideal" operational amplifier is a differential amplifier with the following characteristics: it has

  • infinite open loop voltage gain
  • infinite input resistance (no current into input terminals)
  • zero output resistance (current drawn from the output does not affect the output voltage)
  • infinite bandwidth (no limit on speed of response)

Modern "real world" op amps are in most cases a good match to these characteristics.

There are a few more minor "tweaks" we will introduce later but .. these four, with an understanding of Ohms Law, allow us to design and understand op amp circuits very easily - as you will see in the basic circuits that are described below (Reply 3).

NB: For simplicity the power supply connections to op amps may sometimes be omitted from schematic diagrams - or be shown seperately. But of course they do need power supplies!

opamp.png|300x265

[u]3: BASIC CIRCUITS USING OP AMPS:[/u]

Op amps can be used in many different configurations, but the simple circuits described here cover most applications.

Unity gain buffer, inverting and non-inverting amplifier, difference amplifier

If you feel some of this is getting too technical you may wish to skip any bits in italics.

[u]Unity gain buffer.[/u]

A "unity gain buffer" gives an output voltage that is the same as the input voltage

See also here

The important feature of this circuit is that it takes almost no current from the input. It can be used to measure voltages without placing a load on the circuit being measured. Lets look at how it works.

The unity gain buffer is very easy to analyse, based just on the characteristics of an ideal op amp. As the amplifier has infinite gain from #1: - the difference voltage between the + and - inputs must be zero. R1 and R2 just provide protection to the amplifier inputs. "No" current flows through them. so Vo = Va

[u]Non-inverting amplifier[/u]

Like the unity gain buffer, this circuit does not load the input - but it does provide a precise amount of voltage gain, set by resistors R1 & R2.

Vout = Vin (R2 + R1 / R1) or Vout / Vin = 1 + (R2 / R1)

The input resistance is VERY HIGH (ideally "infinite")

Remember, for any finite voltage at the output the difference voltage at the input must be zero. Lets analyse this circuit just using the ideal op amp characteristics and you will see how easy it is. We'll use real numbers. Suppose Vo = 10V and R1=2k, R2 = 18k R1 + R2 = 20k so a current of 10V / 20k = 0.5mA flows through the resistor chain. NONE of that current flows into the inverting input. (2:) ( I2 = I1 ) so the voltage across R1 is 0.5mA * 2k = 1V From 1: Vb = Va .. therefore the voltage gain Vo / Va = 10V / 1V = 10.

[u]Inverting amplifier[/u]

This circuit produces an output voltage that is the INVERSE of the input voltage. For example a negative input voltage will result in a positive voltage at the output. This type of amplifier circuit is used in our first example (Reply 4).

Vout = - Vin R2 / R1 or Vout / Vin = - R2/R1 ( Note: this formula is not the same as for the non-inverting amplifier)

The input resistance Rin is just R1. However with modern op amps we can use large value resistors.

Once again, Remember (1:) the difference voltage between the + and - inputs must be zero: so the junction of R1 and R2 is at 0V. (we call this a "virtual earth") Vo = I2 R2 Also (2:) no current flows into the input terminals - so I1 = I2 Va = - I1 R1 = - I2 R1. Vo / Va = I2 R2 / - I2 R1 = - R2 / R1

Important note: Unlike the circuits of Fig 1 and Fig 2 this circuit involves a current flowing through the input circuit, so the gain WILL depend on the source resistance. The usual way to avoid this is to use a unity gain buffer at the input.

[u]Difference amplifier[/u]

This simply measures the DIFFERENCE between the two inputs and multiplies it by a gain factor. A difference amplifier is used in our second example (Reply 8)

Vo = (Vb - Va) (R2/R1) ;

If R2 = R1 the gain is set to 1 and the output voltage is just the difference between the two input voltages.

By choosing different values we can add a bit of gain;

If R2 = 10M and R1 = 1M gives a gain of ten.

Now the input resistance for this circuit is just R1 + R2, so if you dont want to load the circuit you are measuring R1 and R2 need to be large - and you will need to choose an op amp with a * low input bias current. (more on this later - reply 7): basic circuits

Diagrams on this page produced using Diagram Designer

opampfig1.png|260x260

opampfig2.png|260x245

opampfig3.png|260x258

opampfig4.png|260x258

[u]4: Using Op Amps: A Practical example[/u] Measuring light level with a photodiode

Photodiodes are used in many every day electronic devices such as cameras, smoke detectors, burglar alarms, safety equipment, medical applications, CD players and surveying instruments. They can be used to detect a wide range of electromagnetic radiation, from infra-red through the visible to ultraviolet and even x-rays. They aren't just for visible light.

How they work When a photon (a bit of light or other EM wave) hits the semiconductor junction of a photodiode it causes a current to flow - in the OPPOSITE direction to the normal forward conduction. There are different ways of detecting this, and you CAN measure the voltage generated; but this circuit uses the "short circuit current" - so the voltage across the device needs to be kept at zero.

This circuit uses an "Op Amp" to do just that.

Components: I used an MCP6022 but any CMOS Op Amp capable of "rail to rail" single supply operation at 5V will do; see my chart above. Also any silicon photodiode will work, and if you just want to test the circuit and dont have a photodiode you can use an LED. The circuit IS polarity sensitive so if it does not work just turn the diode connections around.

This circuit is very like the "Inverting amplifier" described in reply 3.

The above diagram was produced using "Circuit Diagram"

/*
 * Sketch written for Arduino Uno to read current generated by photodiode
 * J. L. Errington skillbank.co.uk Jan 2021
 * Uses an Operational Amplifer powered from the Arduino to measure the current
 * which is then read on Analog input A0 and displayed on the Serial monitor
*/

//Analog input pin assignments
const byte lightPin = A0;  //


//reading ADC and converting to current measurement in microamps
const int vRef = 1100; //value of reference voltage in mV
const int rFeedback = 220; //enter value of feedback resistor in kOhms
long  temp;  //value read from ADC
long mVolts; // mV on analog input 
int uAmps; // Diode current in microAmps  I(uA) = V(mV) * 1000 / R

void setup() {
  Serial.begin(57600);
  analogReference(INTERNAL); //using a 1100mV reference - so a reading of 1 = 1100/1024 millivolts
}

void loop() {
  temp = analogRead(lightPin);
  mVolts = (temp * vRef)/1024; // the multiplication is done first to keep precision - so temp needs to be a long
  uAmps = (mVolts * 1000 ) / rFeedback;  // the multiplication is done first to keep precision - so mVolts needs to be a long
  Serial.print("ADC reads: ");
  Serial.print(temp);
  Serial.print("   Voltage is: ");
  Serial.print(mVolts);
  Serial.print(" milliVolts");
  Serial.print("   Current is: ");
  Serial.print(uAmps);
  Serial.print(" microAmps");
  Serial.println("");
  delay(500); 
}

How the circuit works: When a current I1 is generated by the photodiode the op amp "self-adjusts" to keep the input voltage at zero by producing an output voltage that will cause the exact same current (I2) to flow through the feedback resistor. If the voltage is measured and the value of the feedback resistor is known (here 220 kOhms) the current can be calculated using Ohms law.

Note the op amp is powered from the +5 and 0V from the arduino. This is called "single supply" operation and (as for "rail to rail" operation) is discussed further in reply #6.

The above diagram was produced using "Circuit Diagram"

photodetector.png|380x300

[u]5: Choosing the right Op Amp for your application[/u]

For simple applications you may wish to skip to the bottom of this section where you will find recommendations that will suit most purposes.

Modern op amps can be a very good approximation to an "ideal" op amp provided you choose the right one for your application. Real world operational amplifiers do however have limitations; for example

  • the output voltage cannot exceed the supply voltages; and
  • the output stage imposes a limit on the current they can supply.

We will add some "tweaks" to our ideal op amp specification to reflect their real world limitations: In no particular order: (these terms are explained below, reply #7)

  • Zero input offset voltage
  • Zero noise
  • Zero input bias current

These can introduce (usually slight) offsets to the output voltage from the amplifier.

In the table that follows you will find values of the most important characteristics for some "real world" op amps, to guide you in choosing one for your project. To "short-cut" this for simple applications, just go straight to the recommendations below.

Different "flavours" of op amps

To provide performance characteristics to suit a wide renge of applications op amps are made using different transistor technologies (Bipolar, JFET or MOSFET). Bipolar op amps may have higher gain, lower input offset voltage, faster response, and be more robust. MOSFET op amps can provide "rail to rail" operation (but see below) with very low input bias currents and high (almost infinite) input resistance (but some input capacitance). Generally they include static protection. Some op amps combine FET and bipolar sections for the "best of both worlds".

A "short list" of popular op amps

There are so many op amps you could never choose the "right" one. So below is a list I've compiled that will cover most common applications. I have tested them all. The criteria I've used in making this selection is they must be:

  • Readily available from different sources (eg Mouser, RS Components )
  • Available in a DIP package suitable for breadboards or sockets ( for when you blow them up ).
  • In common use and inexpensive (mostly less than $1)
  • Mostly two op amps in a single 8 pin package - although usually there will be other options.

|500x346

NOTES::

If the table is too small to read conveniently you can view or download it full size using the link below.

The upper half of the table lists op amps with a bipolar input circuit - so they generally do NOT accept rail - rail inputs or outputs. You will see they have generally lower input offset voltage and MUCH higher input bias current than the FET input op amps in the lower half.

MOS input stages allow extremely high input impedances, while MOS output stages allow ALMOST rail-rail output voltages.

I've highlighted the LM747 - a dual 741. A popular choice, but it really has little to recommend it as compared with more modern op amps.

The devices in the green sections are suitable for single-supply operation. However few op amps will work with supplies much below 5V.

The MCP6042 is a micropower op amp intended for very low frequency applications - hence the GBw of only 14kHz - its NOT a misprint!

[u]Recommendations[/u]

For a single supply driven from your Arduino's 5V or 3.3V supply,

the MPC6002 is usually a good choice; if you need a faster response use the MPC6022.

For dual supply running from ±5V to ±15V

The TL072 is usually suitable. For more demanding applications the AD823 may be a good choice.

opamp-table.png|790x547

[u]6: Powering an op amp: Single or split supply, and "rail to rail"?[/u]

The most important criterion is that

the inputs to your circuit must not exceed the bounds of the power supplies.

For example in Fig 5a if you are using +15V - 15V for the supply then you can reasonably apply a 10 volt peak to peak sinewave and you will see the same at the output.

If the SAME op amp is used with a single supply (Fig 5b) +30V - 0V it wont work - and you will likely damage the IC - because the negative excursion of the AC input is outside the range of the supplies.

Bipolar op amps generally need a bit of "headroom" so for example in Fig 5a with ±15V supplies the input should be restricted to say 12V peak - peak.

A split supply doesnt need to be symmetrical as long as the inputs and expected outputs stay within the bounds of the supplies.

Generally CMOS op amps can happily accept "rail to rail" inputs (but check the data sheet). However when you read it offers rail-rail outputs you should be careful. The circuit will give outputs a few mV above the negative supply or below the positive supply - but only with a very high impedance load.

opampfig5.png|387x215

[u]7: Important characteristics explained: Input Bias current, Input Offset current, Input Offset voltage, and "headroom"[/u]

Fig 6 represents the input stage of a conventional bipolar op amp. You can see that the inputs Vin+, Vin- need to provide base current Ib1, Ib2 for the transistors. This is the input bias current.

If the gains of the transistors are not identical they will not take the same base current. The difference between them is the input offset current.

Suppose Vin+ = Vin- ; then Vout SHOULD be zero. However if the transistors are not perfectly matched there will be a difference in their Vbe - that is the input offset voltage.

Looking at this circuit you will see that if Vin+ or Vin- was at -Vcc the transistors would be turned off and the circuit could not work; most op amps require the input voltages to be well within the supply voltages;- and in particular, op amps with bipolar input stages - like this - generally need Vin to be about 3V clear of -Vcc or +Vcc. The amount by which the supply voltage must exceed the input voltage range is called "headroom".

[u]Gain Bandwidth product and Slew Rate[/u]

Both of these relate to the frequency response of the amplifier. Few op amps will have very high gain-bandwidth product - mostly around 5 - 10MHz.

Suppose you build an amplifier using a NE5532, which has a gain-bandwidth product of 10MHz and set it for a gain of 100 the amplifier bandwidth will be 10MHz / 100 = 100kHz.

The slew rate is the speed at which the output voltage can change in response to a step voltage at its input. Looking at the table (Reply 5) you will see that to get a fast slew rate you need an amp with a good GBw product.

Faster is not always better

Very fast op amps like the AD797 above are useful in specific applications, but may prove less stable and prone to oscillate, sometimes needing careful decoupling and pc layout. Op amps with a GBW of 1 - 10 MHz are easier to work with, and for signals that are only changing slowly consider op amps with a GBw less than 1.

Bandwidth: the highest frequency that can be handled by the amplifier.

opampfig6.png|240x286

[u]8: Example 2. Amplifying a signal from a sensor when it has an offset voltage[/u] Many common sensors will produce a voltage that does not "sit" on the 0V rail, as shown in the blue line in this example. The green signal is too small (say 0.04V) to measure with any accuracy; however by amplifying it by 100 we get a signal of around 0.04*100 = 4V that can be measured.

However the signal on the blue line is small, but has a dc offset of about 2.5V; multipling that by 100 gives a 25V signal that still can't be measured. If the DIFFERENCE between the signal and a fixed offset (the orange line) is amplified it gives a nicely measurable signal. The amplification (or gain) and offset can be adjusted to suit your particular sensor.

I used a hall effect sensor A1302 for my own experiment, to look for small changes in magnetic field. It gives a bidirectional voltage output biased to Vcc/2. Load cells used for weighing give similar outputs. The sensor is shown in the diagram below as a pair of thermistors (you could use those, or light dependent resistors)

|500x241

The above diagram was produced using "Circuit Diagram"

How it works

The sensor gives an output near 2.5V (V1); A divider chain gives an adjustable voltage V2 so the offset can be "nulled out". Then the difference amplifier, consisting of the op amp, resistors r1 and r2 amplifies the voltage difference between points A and B, and passes it to the analog input of the arduino.

The gain of the amplifier is R2 / R1 so in the diagram its 10M / 100k = 100 times. You can change the value of the r2 resistors to give the gain you require for your own project. 1M for a gain of ten would perhaps be a good start.

If the sensor gives a negative voltage difference the connections to points A and B can be reversed to provide a positive voltage at the output.

opamp signals3.png|392x400

ex2diffamp.png|620x300

Comments feedback or questions on this tutorial to this thread please:

https://forum.arduino.cc/index.php?topic=722227.0