Using the Built-In Operational Amplifier (OPAMP) on the Arduino UNO R4

by dr_lobo in Circuits > Arduino

1487 Views, 11 Favorites, 0 Comments

Using the Built-In Operational Amplifier (OPAMP) on the Arduino UNO R4

Arduino R4.jpg
Labeled OPAMP.png

Released in June of 2023, the Arduino UNO R4 series added a built-in operational amplifier (OPAMP) which significantly enhances that microcontroller's capabilities to work with analog signals. However, to date, the documentation and examples of how to use the R4's onboard OPAMP have been limited to the basics of getting up and running. While there are many great resources for learning about OPAMPs in general, the amount of information can be daunting and difficult to connect back the the specifics of the R4. Therefore, the goal of this Instructable is to provide a more technical look at the R4's OPAMP and to illustrate its application to a few analog signal scenarios that are commonly encountered by Arduino users.

If you are not already familiar with them, operational amplifiers (OPAMPs) are active electronic components that are exceptionally useful for a wide range of analog signal processing applications such as amplification, filtering, and mathematical operations like differentiation and integration. OPAMPs have three terminals: an "inverting" input (labeled "-"), a "non-inverting" input (labeled "+"), and an output. OPAMPs also have two power connections, sometimes referred to as "rails", that can be bipolar (±Vsupply) or single-sided (+Vsupply and ground).

While the internal design of OPAMPs can be very sophisticated, they are typically built to have certain idealized characteristics that allow them to be used as "black boxes" with little concern for detailed specifications. In particular, OPAMPs are designed to satisfy the two "golden rules" that make them so useful for analog signal processing: (1) in the presence of feedback from the output to the inverting ("negative") input, the OPAMP will set its output so that the voltage difference between its inputs is zero, and (2) the inputs draw no current. While the virtues of these idealized OPAMP characteristics may not be immediately obvious, this Instructable will show how they can be used to provide buffering, amplification, and voltage offsets to our analog signals.

Supplies

Signal Generator & Oscilloscope.jpg
Breadboard.jpg
Resistors.jpg
CircuitLab.png

In addition to an Arduino UNO R4 (with its built-in OPAMP), you will need a signal generator (to drive the OPAMP), an oscilloscope (to observe the OPAMP's output), a breadboard (for putting together some external circuits), and an assortment of resistors. For the differential amplifier circuit, you will also need a bench power supply (not shown) to provide a reference voltage. Finally, if you would like to simulate any of the circuits in this Instructable, it would be useful to have a CircuitLab account or something equivalent.

Please note that, while you could conceivably use the R4's onboard Digital-to-Analog Converter (DAC) as a signal source and its onboard Analog-to-Digital Converter (ADC) to capture the OPAMP's output, using a separate signal generator and oscilloscope will make it easier to observe the limitations of the OPAMP's behavior that will be important to understand for your own projects. That said, in real projects, the R4's onboard DAC may be more than adequate for your signal generation needs (if any) and, likewise, the onboard ADC may also be well suited to measuring the OPAMP's output. In fact, the most common way that I use the R4's OPAMP is to "condition" analog signals (typically from sensors) so that the OPAMP's output is perfectly matched to the 0-5V input range that I can connect to the onboard ADC for measurements.

Setup Signal Generator & Oscilloscope

Bench Setup.jpg
Scope Setup.png

While the details of how to set up your signal generator and oscilloscope will depend on your specific equipment, there are a few general items to consider before starting to build OPAMP circuits. Regarding signal generation, you should be familiar with how to select waveforms (sine, sawtooth, etc.) and specify waveform parameters such as amplitude, frequency, and offset. Regarding measurements, you should be familiar with how to connect a signal to an oscilloscope input channel and how to configure channel settings such as vertical (voltage) resolution, horizontal (time) resolution, and trigger level.

For this Instructable, I set up my bench as shown in the first figure. On the far left is my bench power supply which is only used in the differential amplifier circuit. In the center is my oscilloscope with its built-in signal generator (Rigol DS1104Z-S Plus). I have attached a 1X passive probe for measuring the output of the OPAMP on channel 1 of the oscilloscope. I have split the output of the signal generator at the back of the scope and I have connected one leg to the large alligator clips shown in the left center of the bench's work area and the other leg to channel 2 of the scope (so it can be shown as a reference signal alongside the output of the OPAMP on channel 1). I have configured the signal generator to output a sine wave with an amplitude of 1V peak-to-peak (±0.5V), a frequency of 1 kHz, and an offset of +0.5V (so that the signal never goes negative). A scope capture for this configuration is provided in the second figure. Finally, the power lead shown on the far right of the bench's work area is a 9V supply for the Arduino.

UNO R4 OPAMP Specifications

RA4M1 OPAMP Specs.jpg
R4 Cheat Sheet.png
R4 Schematic.jpg

From the R4 Cheat Sheet, we learn that the built-in OPAMP is exposed on pins A1 ("+" non-inverting input), A2 ("-" inverting input), and A3 (output) of the R4's headers and, from the R4 OPAMP Tutorial, we learn that the OPAMP is activated by calling the OPAMP.begin(speed) function from the OPAMP.h library as in this minimal sketch (where speed can be HIGH or LOW with different performance characteristics and power consumptions) ...

#include <OPAMP.h>

void setup () {
 OPAMP.begin(OPAMP_SPEED_HIGHSPEED);
}

void loop() {}


From the R4 Cheat Sheet, we also learn that the onboard OPAMP is provided by that board's Renesas RA4M1 processor. While, in general, the RA4M1's OPAMP can be treated as an ideal OPAMP, there is at least one implementation-specific detail that we need to know – the OPAMP's output voltage range. This information is best gleaned from the Renesas RA4M1 Hardware Manual – specifically, from the OPAMP Electrical Characteristics in Section 48.14 on page 1394 (it's a long manual!). From there, we learn that the RA4M1's OPAMP output voltage range is 0.1 to (AVCC0-0.1) where AVCC0 is the chip's analog power supply voltage which, from the R4's schematic, is +5V on the UNO R4. We can model this output range in CircuitLab by treating the OPAMP as single-sided with AVCC0 = +5V on the high rail and the low rail connected to ground (as opposed to -5V in a bipolar configuration). Because the OPAMP cannot output voltages beyond its rails, it will be important to ensure that we never configure the OPAMP to output more than +5V or less than 0V for the range of analog inputs we anticipate. Exactly how we design our OPAMP circuits to stay in this output range, and ideally use as much of it as possible, will constitute the majority of this Instructable.

Voltage Follower

3.1 Voltage Follower Circuit.png
3.2 Voltage Follower Simulation.png
3.3 Voltage Follower Photo.jpg
3.4 Voltage Follower Scope.png

The simplest OPAMP circuit of note is the humble voltage follower. In this circuit, the signal (Vin) is connected to the non-inverting (+) input and the output is connected to the inverting (-) input as shown in the first figure. The "negative feedback" connection between the output and the inverting input is what provides the OPAMP the ability to control the voltage on that input. Remembering that the first golden rule of OPAMPs says that, in the presence of negative feedback, the OPAMP will drive the output so that the voltage difference between the inputs is zero, means that the output will be equal to (or "follow") the non-inverting input so that the two inputs are the same. For a 1V peak-to-peak (±0.5V) sine wave input, offset by +0.5V to avoid going negative, a CircuitLab simulation of the voltage follower output is provided in the second figure (along with the input signal which the output signal is right on top of). It is important to note that, since the R4's OPAMP output range is single-sided (0-5V), we need to ensure that the input signal does not try to drive an output outside that range – for example, if we did not provide the +0.5V offset to our ±0.5V sine wave, the voltage follower would "clip" the negative half of the sine wave because the OPAMP cannot drive its output below the ground rail.

An implementation of this circuit on a breadboard connected to an UNO R4 is shown in the third figure. Note the connections to the R4's OPAMP pins, the alligator clips connected to the signal generator, and the passive probe connected to the oscilloscope. Since this circuit does not require any additional components, all the connections can be made with jumper wires on the breadboard. A scope capture of the OPAMP's output is shown in the fourth figure where I have slightly offset the original signal (blue) above the OPAMP's output (yellow) so that they can be seen at the same time. While the real output matches the simulation well, you can see that it is slightly "clipped" at the low end as indicated by the flattened bottoms of the output sinusoid. This is caused by the OPAMP trying to drive too close to its lower rail, which is at ground. We will observe the same effect when the OPAMP is trying to drive too close to its upper rail in a subsequent example. For this reason, it is good practice to design your amplifier circuits so that the output never gets too close to either of the OPAMP's rails.

At first glance, this circuit may appear to be trivial to the point of being useless. However, remembering that the second golden rule of OPAMPs says that the inputs draw no current means that the voltage follower is a great way to isolate your signal source from any other downstream electronics. Since additional electronics can act like a "load" (drawing current) on your signal source, they can cause your signal to "sag" (reduced voltage), which can spoil any measurements of the input signal that you may want to make. By inserting a voltage follower with its high impedance inputs between the signal source and the rest of your electronics, downstream loads will draw their current from the OPAMP's power supply instead of from the signal source and, consequently, the input signal will remain pristine. This kind of electrical isolation is called "buffering" and, since the voltage follower has a "gain" or amplification ratio (Vout / Vin) equal to 1 ("unity"), this is why the voltage follower is sometimes also referred to as the much fancier sounding "unity gain buffer".

Non-Inverting Amplifier

4.1 Non-Inverting Amplifier Circuit.png
4.2 Non-Inverting Amplifier Simulation.png
4.3 Non-Inverting Amplifier Photo.jpg
4.4 Non-Inverting Amplifier Scope.png

The voltage follower is a special case of a more general OPAMP circuit known as a "non-inverting amplifier". In this circuit, the signal is again connected to the non-inverting (+) input but now the inverting input (-) is connected to ground through an input resistor, R-, and to the output through a feedback resistor, Rf, as shown in the first figure. As for the voltage follower, since the first golden rule of OPAMPs tells us that the output will be driven such that the voltage on the inputs is the same, we know that the voltage on the inverting input must be equal to the signal on the non-inverting input, Vin. Given that, Ohm's law tells us that the current through the input resistor, I-, must be equal to the voltage across the input resistor, V- = Vin, divided by the resistor's value, R-. Furthermore, the second golden rule of OPAMPs tells us that the inputs draw no current. Therefore, all the current running through the input resistor must be coming from the OPAMP's output – which means that it must all be running through the feedback resistor as well. Using Ohm's law again tells us that the voltage drop across the feedback resistor, Vf = Vout - Vin, must be equal to this current, I- = Vin / R-, times the value of the feedback resistor, Rf. Solving this expression for the ratio of the output to the input yields Vout / Vin = 1 + (Rf / R-). This is the "gain" equation for a non-inverting amplifier and it is worth noting that it is always greater than or equal to one (in other words, it cannot be used to attenuate the input signal). Also note that the case where G = 1 is realized when Rf = 0 and R- = ∞ which is equivalent to the voltage follower examined in the previous section.

A common Arduino application of this circuit is to amplify signals that live in the range from zero to (significantly) less than five volts. While we can already read such signals with the R4's onboard ADC (which has an input range of 0-5V), we are giving up sensitivity if we don't use the full input range because the number of digital output levels is fixed by the bit depth of the encoder (8 bits = 256 output levels by default, up to 12 bits = 4096 output levels on the R4). Since G = 1+(Rf/R-), if we would like to amplify a 1V signal by five times (5x) to fill the 0-5V input range of the R4's ADC, we need (Rf/R-) = G-1 = 4. Since standard resistors only come in certain values, this calculator can help select the right values for any particular ratio. While we can't get (Rf/R-) = 4 exactly with only two standard resistors, we can get pretty close (0.61%) with any pair of resistors with the ratio 33 / 8.2. When working with OPAMPs, it is good practice to use larger resistors to make sure that you never inadvertently reduce the impedance of either of the inputs to a degree that impairs the OPAMP's function. For this application, I selected Rf = 3.3MΩ and R- = 820kΩ, as shown in the first figure, to achieve the desired gain of 5. For the same ±0.5V sine wave, with a +0.5V bias, used in the previous section, a CircuitLab simulation of the non-inverting amplifier output is provided in the second figure. As expected, the 0-1V input signal is amplified by 5 to fill the 0-5V range that we would like to be able to measure with the onboard ADC. An implementation of this circuit on a breadboard connected to the UNO R4 is shown in the third figure. Finally, a scope capture of the OPAMP's output is shown in the fourth figure and, as you can see, it again agrees well with the simulation result.

Differential Amplifier

5.1 Differential Amplifier Circuit.png
5.2 Differential Amplifier Simulation.png
5.3 Differential Amplifier Photo.jpg
5.4 Differential Amplifier Scope.png

Unfortunately, signals in the wild are often not only smaller than we would like but also offset from ground. While we could simply amplify these signals so that their highest values are still below the upper limit of the input to our ADC, the offset of their lowest values would similarly be amplified – leaving a significant fraction of the ADC's input range unused. For example, if the ±0.5V sine wave used in the previous section was offset by +2V, it's highest value would now be +2.5V which can only be amplified by 2 before hitting the +5V upper limit of the ADC's input. However, the lowest value of the sine wave would now be +1.5V which, when similarly amplified by 2, would leave the bottom 3V of the ADC's input range empty of signal. By providing a reference signal that is effectively subtracted from the input signal, the differential amplifier provides the capability to remove unwanted offsets prior to amplification in order to fully use the output range of the OPAMP to best match the input requirements of any downstream electronics (including, but not limited to, ADCs).

Consider the circuit in the first figure. Since we know that the inputs draw no current (second golden rule), the two resistors connected to the non-inverting (+) input form a simple voltage divider to ground such that V+ = Vin • Rg / (Rg + R+). For the same reason (inputs draw no current), we also know that the current through the feedback resistor, If = (Vout - V-) / R-, must be equal to the current drawn from the voltage reference, Iref = -(Vref - V-) / R- (be careful with the signs). Finally, since we also know that the inputs are equal (first golden rule), we can replace V- in the equality between these currents with our expression for V+ from the input voltage divider and solve for Vout. While the general form of Vout is complicated, if Rg = Rf and R+ = R- = Rin, it simplifies to Vout = Rf / Rin • (Vin - Vref). This is exactly what we want from a differential amplifier – the amplification is simply defined by the ratio of the feedback to the input resistor and Vref is subtracted from Vin prior to amplification.

Note that the differential amplifier gain, G = Rf / Rin, is missing the "+ 1" term from the gain equation for the non-inverting amplifier. Therefore, if we still want a gain of 5, so that the 1Vpp sine wave fills the 5V input range of the ADC, we now only need a 680kΩ input resistor. Furthermore, if we want to fit into the range 0-5V, we need to provide a +1.5V reference which can be removed from the +2V input signal offset (leaving the +0.5V offset needed to keep the signal from going negative). A simulation of this circuit is illustrated in the second figure, a breadboard implementation is shown in the third figure, and a scope capture is provided in the fourth figure.

As you can see, the differential amplifier cleanly maps the 2V±0.5V input to the full 0-5V output of the OPAMP which conveniently, but not coincidentally, matches the 0-5V input range of our ADC. Normally, I would take off a little less offset and drive a little less gain to make sure that I didn't push the OPAMP's output too close to either of its rails. From the Renesas RA4M1 Hardware Manual, this should be at least 100mV – both below AVCC0 = +5V as well as above ground. However, in this example, you can see how clean the output is even though it is very close to both the upper and lower rails. Given how common it is to encounter small (<1Vpp) signals with significant non-zero offsets, the differential amplifier is a great circuit for conditioning your signals for readout, through the ADC, and / or additional processing.

Inverting Amplifier

6.1 Inverting Amplifier Circuit.png
6.2 Inverting Amplifier Simulation.png
6.3 Inverting Amplifier Photo.jpg
6.4 Inverting Amplifier Scope.png

While all the previous circuits have connected the incoming signal to the non-inverting input, it is also possible to construct useful amplifiers using the inverting input as shown in the first figure. As always, the feedback loop and its associated resistor must be connected to the inverting input, but now the signal is connected through the input resistor to the inverting input and the non-inverting input is connected to ground. Since we know that the inputs are equal (first golden rule), we know that the "summing point" that connects the two resistors to the inverting input must also be at ground potential – not through a true connection to ground but by a balance of currents through the two resistors that creates a "virtual ground" at the summing point. Since the inputs draw no current (second golden rule), the two resistors form a simple voltage divider with Vin at the "top", Vout at the "bottom", and the virtual ground summing point in the middle. Even before solving for the gain equation, we can see that Vout must be on the opposite side of ground with respect to Vin – in other words, the output will be inverted in sign with respect to the input (negative when the input is positive and vice versa). By equating the current through the feedback resistor, If = -Vout / Rf, with the current through the input resistor, I- = Vin / R- (again, be careful of the signs), we can quickly see that the gain of this "inverting amplifier" is G = -Rf / Rin and the negative sign indicates, as expected, that the output is inverted with respect to the input. It is interesting to note that, like the differential amplifier, the gain of this circuit can be less than one – meaning that the OPAMP is acting as an attenuator rather than an amplifier. While this can be useful, especially considering the buffering provided by the OPAMP's high impedance inputs, it can be tricky in practice.

For the resistor values shown in the first figure, this inverting amplifier should have a gain of G = -3.3MΩ / 330kΩ = -10. However, when we simulate this circuit with a single-sided OPAMP for an ±0.5Vpp input sine wave, this time with no offset (so that it has both positive and negative components), as illustrated in the second figure, we see that the output is clipped to ground whenever the input is positive. This is because the OPAMP cannot drive its output below the ground rail when the inverting amplifier is receiving a positive input signal. However, at least in simulation, the OPAMP happily drives a positive output when receiving a negative input and, for this part of the signal, exhibits the expected 10x amplification gain.

If we refer back to the Renesas RA4M1 Hardware Manual, we see that the OPAMP's input range is also specified to be single-sided (approximately 0-5V). For this reason, I was unsure whether applying a negative signal to an inverting amplifier using the R4's OPAMP would produce any output as it should be zero when the input is positive, since the OPAMP can't drive an output below ground, and might possibly be zero when the input is negative since that is outside the OPAMP's specified input range. However, when I wired up the circuit shown in the third photo, I observed the output shown in the following scope capture – in good agreement with the simulation (albeit with a little clipping when the output gets too close to the upper rail). While I would not recommend using the OPAMP in this fashion, since operating outside of the manufacturer's specifications can result in damaging the part, it clearly illustrates the limitations on the OPAMP's output. Since the R4's OPAMP is not specified to receive negative inputs and not able to drive negative outputs, I don't generally recommend using it in an inverting amplifier configuration.

Resources for Additional Study

In addition to the sources linked to within the body of this Instructable, there are a lot a great resources online to learn more about OPAMPs and their applications.

The Electronics Tutorials referenced in the official R4 OPAMP Tutorial are particularly good. The materials at ElectronicsHub and All About Circuits are also good. They all have OPAMP circuits beyond the basic amplifier examples in this Instructable.

All of the major component manufacturers have their own overviews which tend to be more technical but highlight the advantages and disadvantages of real devices. Here are some materials from Texas Instruments, Analog Devices, and Monolithic Power Systems to get you started.

Once you start having specific questions, electronics.stackexchange.com can be a great resource – but it helps to know a little bit about what you are doing first.

Finally, if you like to curl up with huge technical books, then The Art of Electronics by Paul Horowitz and Winfield Hill may be right up your alley.