Operational Amplifiers Worked Example

Non-inverting amplifier demo

Build an adjustable-gain amplifier, then measure its output with an Arduino.

Getting started: what you'll need

This section lists everything you need for the worked examples below. Equivalent parts will work.

Equipment list

Part numberDescriptionQty
ABX00087Arduino UNO R4 WiFi (needs a DAC and ADC, as the UNO R4 has)1
USB-C data cableUSB-C to USB-A/C data cable (not charge-only)1
BB830 (or equivalent)830-point solderless breadboard1
Jumper wire kitMale-to-male breadboard jumper wires, assorted1
MCP6021-I/PSingle rail-to-rail op-amp: 1x MCP6021 DIP Style Package (plugs directly into breadboard), or the lower-cost 5x MCP6021 SOP Style Devices with a SOP to DIP Adapter board (requires soldering skills)1
1k resistor1 kΩ resistor (R1). Included in the Elegoo starter kit below1
10k potentiometer10 kΩ slider potentiometer (Rf). A 10k pot is also included in the Elegoo starter kit below1

New to electronics? This Elegoo electronics starter kit includes the Arduino-compatible board, breadboard, jumper wires, 1 kΩ resistor and 10 kΩ potentiometer, and much more. It doesn't include an op-amp, which you'll need to buy separately.

New to Arduino? Start with Arduino's UNO R4 getting-started guide for setting up your board and uploading your first sketch.

Arduino Code

This sketch outputs about 1 V on A0 using the UNO R4's digital-to-analogue converter, then reads the op-amp output on A1 with the analogue-to-digital converter and shows it on the built-in LED matrix. Use the Copy button, paste it into the Arduino IDE and upload.

#include "Arduino_LED_Matrix.h"
// Hosted on https://LittleEngineeringBooks.com for educational purposes.

// Create an object to control the built-in LED matrix
ArduinoLEDMatrix matrix;

// 8 rows x 12 columns display buffer
uint8_t frame[8][12];

// 3x5 font for digits 0-9
const uint8_t digits[10][5] = {
  {0b111, 0b101, 0b101, 0b101, 0b111}, // 0
  {0b010, 0b110, 0b010, 0b010, 0b111}, // 1
  {0b111, 0b001, 0b111, 0b100, 0b111}, // 2
  {0b111, 0b001, 0b111, 0b001, 0b111}, // 3
  {0b101, 0b101, 0b111, 0b001, 0b001}, // 4
  {0b111, 0b100, 0b111, 0b001, 0b111}, // 5
  {0b111, 0b100, 0b111, 0b101, 0b111}, // 6
  {0b111, 0b001, 0b010, 0b010, 0b010}, // 7
  {0b111, 0b101, 0b111, 0b101, 0b111}, // 8
  {0b111, 0b101, 0b111, 0b001, 0b111}  // 9
};

// --------------------------------------------------
// Clear display buffer
// --------------------------------------------------
void clearFrame()
{
  memset(frame, 0, sizeof(frame));
}

// --------------------------------------------------
// Draw a digit at a specified X position
// --------------------------------------------------
void drawDigit(int digit, int xOffset)
{
  if (digit < 0 || digit > 9) return;

  for (int row = 0; row < 5; row++)
  {
    for (int col = 0; col < 3; col++)
    {
      if (digits[digit][row] & (1 << (2 - col)))
      {
        frame[row + 1][xOffset + col] = 1;
      }
    }
  }
}

// --------------------------------------------------
// Draw decimal point
// --------------------------------------------------
void drawDecimalPoint(int x, int y)
{
  frame[y][x] = 1;
}

// --------------------------------------------------
// Setup
// --------------------------------------------------
void setup()
{
  matrix.begin();

  // ADC: 10-bit (0-1023)
  analogReadResolution(10);

  // DAC: 12-bit (0-4095)
  analogWriteResolution(12);

  // Output approximately 1.0V on A0
  //
  // DAC Value = (1.0 / 5.0) * 4095
  //           ≈ 819
  analogWrite(A0, 840);

  Serial.begin(115200);
}

// --------------------------------------------------
// Main Loop
// --------------------------------------------------
void loop()
{
  // Take 16 ADC readings and average them
  long total = 0;

  for (int i = 0; i < 16; i++)
  {
    total += analogRead(A1);
  }

  int adc = total / 16;

  // Convert ADC reading to voltage
  float voltage = adc * (5.0f / 1023.0f);

  // Extract digits for X.X display
  int whole = (int)voltage;
  int decimal = ((int)(voltage * 10)) % 10;

  // Clear the display
  clearFrame();

  // Display voltage as X.X
  drawDigit(whole, 0);
  drawDecimalPoint(4, 5);
  drawDigit(decimal, 6);

  // Update LED matrix
  matrix.renderBitmap(frame, 8, 12);

  // Debug output
  Serial.print("ADC = ");
  Serial.print(adc);
  Serial.print("   Voltage = ");
  Serial.println(voltage, 2);

  delay(100);
}

// Hosted on https://LittleEngineeringBooks.com for educational purposes.
Non-inverting amplifier demo

Breadboard schematic

Breadboard and schematic for the non-inverting amplifier with a potentiometer setting the gain
Click the image to view it full size.

Pinout wiring

MCP6021 pins are numbered 1 to 8 anticlockwise from the notch or dot. The 10k pot pins are 1 (one end), 2 (wiper) and 3 (unused).

FromToPurpose
5VMCP6021 pin 7Positive supply
0V (GND)MCP6021 pin 4Negative supply / ground
Arduino A0MCP6021 pin 31 V from the Arduino DAC to the non-inverting input
MCP6021 pin 6Arduino A1Op-amp output, read by the ADC
MCP6021 pin 610k pot pin 2 (wiper)Feedback: gives adjustable resistance of 0 to 10k (Rf)
10k pot pin 1MCP6021 pin 2Inverting input
10k pot pin 11k resistor, other side to GNDR1 to ground

What we're doing

The Arduino uses its digital-to-analogue converter (DAC) to output 1 V on pin A0. (Curious how DACs work? See the converters book!) That 1 V goes into the non-inverting (+) input of the op-amp.

The gain is set by the ratio of the resistors: Gain = 1 + Rf/R1, where R1 is 1 kΩ and Rf is the 10 kΩ potentiometer. With the pot at 0 Ω the gain is 1, and with the pot at 10 kΩ the gain is 11 (1 + 10k/1k). Rounding for simplicity, you'd expect to see roughly 1 V up to about 10 V on the output.

There's a catch: the op-amp only has the Arduino's 5 V rail, and it can't quite reach the rail. Any output that should be above just under 5 V will clip at just under 5 V. The Arduino's A1 input also can't read above 5 V.

Vout = Vin × (1 + Rf/R1)

Testing and results

Photos of the LED matrix display for each pot setting (images coming soon):

  • 1 V displayed (gain of 1): pot at 0. Output equals the input.
  • 3.3 V expected (gain of 3.3): pot at about 2.3 kΩ. The amplifier is still in its linear range.
  • 5 V expected (gain of 5): pot at about 4 kΩ. The output is right at the limit and starts to clip just below 5 V.
  • 10 V expected (gain of 10): pot at about 9 kΩ. The output clips just below 5 V, since the op-amp can't go higher than its supply.