13C: Integrator & differentiator#

Learning goal: Explore uses of opamps like integrator and differentiator circuits. Know when they are used and how to make them.

Structure of an experiment:

  • Background + Predict + Stimulate (35 minn): per person. This is homework and should be finished before you start your 4 hours practicum session

  • Build + Measure (40 min): with your partner (group of 2)

  • Evaluate (10 min): with a group of 4 (per table)

BACKGROUND#

⏳ Estimated time: 10 min

The opamp circuits we have used up till now use solely resistors in the feedback path. However, other passive components (capacitors, inductors) could be used in the feedback path as well. In this situation we can use the same formulas for the transfer function as we use with resistors (inverting amplifier configuration and non-inverting amplifier configuration). Instead of the resistors-values we will use the (complex) impedance in the standard opamp formulas for the non-resistive components.

In this lab we will focus on circuits were a resistor is replaced by a capacitor:

  • When we replace the resistor connected between the signal source and the opamps input with a capacitor we create a circuit which we call a differentiator. The behavior of this circuit corresponds to a (mathematical) differentiator when viewed in the time-domain, or to a high-pass filter in the frequency domain.

  • When (only!) the resistor between the output and the inverting input is replaced by a capacitor we will create a circuit called an integrator. This means, it will integrate signals in the time-domain in a mathematical way, in the frequency domain this behavior translates to a low-pass filter.

For both the integrator and the differentiator the inverting input as a virtual ground point. Have a look at the following circuit.

From the virtual ground point, since the noninverting (+) input is connected to ground, can be found that \(V_\mathrm{in} + V_\mathrm{C1} = 0 \Rightarrow V_\mathrm{in} = -V_\mathrm{C1}\). The current \(I_{\mathrm{C}1}\) through C1 is time dependent (and also frequency dependent), and is equal to \(V_\mathrm{out}/R_1\). Using the two equations above, plus the following equation for the current through a capacitor. $\( \begin{align} \mathrm{d}Q = C \cdot \mathrm{d}V = I \cdot \mathrm{d}t \tag{1} \end{align} \)$

In which \(Q\) is the charge on the capacitor in Coulomb, \(C\) is the capacitance in Farad, \(V\) is the voltage over the capacitor, I is the current through the capacitor, and \(t\) for time. You can derive that: $\( \begin{align} I_\mathrm{C1} &= C_1 \frac{\mathrm{d} V_\mathrm{C1}}{\mathrm{d}t} = -C_1 \frac{\mathrm{d} V_\mathrm{in}}{\mathrm{d}t} = \frac{V_\mathrm{out}}{R_1} \tag{2} \end{align} \)$

Rearranging terms:

\[\begin{split} \begin{align} \Rightarrow \frac{\mathrm{d} V_\mathrm{in}}{\mathrm{d}t} &= -\frac{V_\mathrm{out}}{R_1 C_1} \tag{3a}\\ V_\mathrm{out} &= -R_1 C_1 \frac{\mathrm{d} V_\mathrm{in}}{\mathrm{d}t} \tag{3b} \end{align} \end{split}\]

This means that this circuit behaves as an differentiator! Notice that if the \(C_1\) and \(R_1\) get exchanged and you solve for \(V_\mathrm{out}\) you find the relation: \(V_\mathrm{out} = -\frac{1}{R_1 C_1} \int V_\mathrm{in} \mathrm{d}t \) which defines an integrator.

Thinking about this, it is hopefully logical that a square-wave on the input of the integrator, will show up as a triangle wave at the output of the integrator.

ℹ️ Please note: Both the integrator and the differentiator are derived from the inverting amplifier. Hence the differentiator and the integrator have an inverting function, as well.

ANTICIPATE: predict behaviour of differentiator circuit#

⏳ Estimated time: 10 min

As the name already says, the circuit above is a differentiator.

  • If a circuit acts as a differentiator, what will be the shape and phase of \(V_\mathrm{out}\) when \(V_\mathrm{in}\) is a sine wave? Explain why.

  • Explain what happens to the amplitude of \(V_\mathrm{out}\) when the frequency of the sine input doubles.

### TO DO ="your answer: Vout from diffrentiator, with Vin=sine"
 

### TO DO ="your answer: if frequency doubles, what happens to the amplitude of Vout"
 

SIMULATE 1: simulate an differentiator circuit#

⏳ Estimated time: 10 min

In LTSpice, build the given differentiator circuit or download a file from brightspace named ‘week13C’ and fill in missing values.

Run a transient simulation with a stop time of 15ms and start saving data after 5ms. Plot Vin and Vout.
Explain the look of the waveforms?

ℹ️ Hint
Consider the phase of the signal. Which phase difference would you expect based on the differentiation? Don’t forget to take the phase shift caused by the inverting opamp into account.

  • Explain the similarities and differences between the signal at \(V_\mathrm{in}\) and \(V_\mathrm{out}\).

### TO DO='describe Vin and Vout, plus explain'
 

SIMULATE 2: an integrator circuit#

⏳ Estimated time: 15 min

Since you already have the differentiator circuit in LTSpice, you can pretty quickly simulate an integrator as well. Use the same file, copy paste the circuit, and swap some elements (remember to change the labels such as Vout).
Run the simulation twice:

  • With the same sine input (as in the anticipate), plot \(V_\mathrm{in}\) and both the \(V_\mathrm{out}\) from the differentiator and integrator. Add a screenshot of the \(V_\mathrm{in}\) and both \(V_\mathrm{out}\).

  • What differences do you see?

%python
from ipywidgets import FileUpload
from IPython.display import Image
import os
upload=FileUpload()
upload
%python
file_name="13C_1_int.jpg"
if upload.value:
    with open(file_name,"wb") as f:
        try: f.write(upload.data[-1]) # python 3.7 Kernel code, not working on Vocareum
        except: f.write(upload.value[-1]["content"])  # python 3.8 Kernel code, not working on Vocareum

Image(filename=file_name, width="50%")
### TO DO ="description of the differences between Vin and Vout (differentiator and integrator)" 
 

In the introduction, you briefly saw that for the integrator we have the relation: $\( V_\mathrm{out} = -\frac{1}{R_1 C_1} \int V_\mathrm{in} \; \mathrm{d}t \tag{4}\)$

You can also express this relation as a derivative (simply take the derivative of both sides):

\[\begin{equation} \frac{\mathrm{d} V_\mathrm{out}}{\mathrm{d}t} = \frac{1}{-R_1 C_1} V_\mathrm{in} \tag{5} \end{equation}\]
  • Adapt the voltage source to create a square wave.

  • Using equation (5), compare the calculated slope with your simulation.

  • Investigate which effect the values of the resistor and capacitor have (by changing the value one order of magnitude up and down at the time) on the output signal in LTSpice. Using the theory you have learned, explain how these components affect the output signal.

%python
upload
%python
file_name="13C_2_diffV.jpg"
if upload.value:
    with open(file_name,"wb") as f:
        try: f.write(upload.data[-1]) # python 3.7 Kernel code, not working on Vocareum
        except: f.write(upload.value[-1]["content"])  # python 3.8 Kernel code, not working on Vocareum

Image(filename=file_name, width="50%")
### TO DO=" your answers on the slope value, and dependency on R&C" 
 

Change the simulation type to AC-analysis. Select the octave sweep with 50 points per octave, start at 1 Hz and stop at 10kHz. Give the AC-source a value of 100mV for the AC-analysis.

  • Plot V(Vout)/V(Vin) (which you can access by right-clicking the label on top of the simulation). Upload a screenshot.

  • Describe what you observe in this plot.

  • What is the gain change per octave?

  • And per decade?

ℹ️ Hint

  • When looking at the graph, you can right click the trace and in the text box input an mathematical expression.

  • Remember: an octave concerns a doubling of the frequency, while a decade involves multiplication by 10.

%python
upload
%python
file_name="13C_3_diff_AC.jpg"
if upload.value!={}:
    with open(file_name,"wb") as f:
        try: f.write(upload.data[-1]) # python 3.7 Kernel code, not working on Vocareum
        except: f.write(upload.value[-1]["content"])  # python 3.8 Kernel code, not working on Vocareum

Image(filename=file_name, width="50%")
### TO DO=" write your answer here, incl. the gain change per octave and decade" 

IMPLEMENT & INVESTIGATE 1: build an integrator circuit#

⏳ Estimated time: 30 min

To make a succesful integrator circuit on the ALPACA we need three parts:

  1. The actual integrator circuit

  2. An input signal which alternates between positive and negative values (like the pulse used in the second simulation).

  3. Measure via one of the onboard amplifiers to prevent any negative amplitudes on the analog in pins.

A picture of opamp pins as a reminder:

The 1st and 3rd step you should already be confident doing, but for the 2nd step you have to think for yourselves how to generate negative voltages.

ℹ️ Hints (dropdown)

On the right hand side of the ALPACA, you find the DAC ASSISTANT. Using this DAC assistant you can turn the purely positive signals from +IN into signals that both positive and negative value at OUT.

To find the exact voltage conversion of the DAC assistent read the alpaca intro manual: https://mwdocter.github.io/TeachBook/main/EI_manuals/intro_ALPACA.html

Building the integrator#

Can you design the connection of the integrator yourself? Practice this, as you will need to be able to do this for the final assignment as well. Feel free to look back at the connections given in earlier assignments.

ℹ️ Hints For some guidance, consult the example below. There is also a video put in this notebook which you can watch to see how this can be built step by step.

Detailed fritzing (dropdown)

Using the amplifers#

The integrator can give any output between -12V and +12 V. Always measure first with the Voltmeter!

By now you know how to measure safely with the ALPACA, think for yourself what attenuations you want to use for \(V_\mathrm{in}\) and \(V_\mathrm{out}\). Have a look in the manual to find the corresponding settings: https://mwdocter.github.io/TeachBook/main/EI_manuals/intro_ALPACA.html

  • Use the following code to acquire and display your measurement.

%serialconnect to --port="COM7" 

import time
import numpy as np
import matplotlib.pyplot as plt

from machine import ADC # type: ignore
from functiongenerator import FuncGen, DC, Sine, Square # type: ignore
def convert_DAC_voltage(assistant_v_out):
    """Converts OUT to +IN at DAC ASSISTANT

    Finds the correct DAC voltage to get a specific output at the DAC ASSISTANT
    """
    assistant_v_in = assistant_v_out / 5 + 2.048
    return assistant_v_in

def convert_samples_to_volts(samples, gain=1):
    """Converts integer values to volts

    Converts the measurements from the analog input of the ALPACA
    into volts. Has an optional parameter `gain` which when set
    to the gain of the AMP0/1 will automatically correct for 
    the applied gain.
    """
    return samples / 65536 * 3.0 / gain

# Instantiate a measurement pin Ain0 for the output signal
adc0 = ADC(26) 
adc1 = ADC(27) 
# Settings
V_MAX = 2
V_MIN = -2
MAX_TIME = 1
DELAY_MS = 5 #20

num_samples = int(MAX_TIME / DELAY_MS * 1000)
output_signal = np.zeros(num_samples)
input_signal = np.zeros(num_samples)

assert num_samples < 3000, "Error: can't take that many samples!"

with FuncGen(Square(
    Vmin=convert_DAC_voltage(V_MIN), 
    Vmax=convert_DAC_voltage(V_MAX), 
    freq=10)):
    
    time.sleep_ms(100)
    
    for i in range(num_samples):
      
        v_out = adc0.read_u16()
        output_signal[i] = v_out
        
        v_in = adc1.read_u16()
        input_signal[i] = v_in

        time.sleep_ms(DELAY_MS)

print('Acquisiton done!')

plt.plot(convert_samples_to_volts(output_signal, gain=0.1), color='r', label='Vout')
plt.plot(convert_samples_to_volts(input_signal), color='b', label='Vin')
plt.ylabel("Voltage [V]")
plt.xlabel("Sample number")
plt.grid()
plt.legend()

IMPLEMENT & INVESTIGATE 2: build a differentiator circuit#

⏳ Estimated time: 10 min

  • Change around the resistor and capacitor to build a differentiator.

  • Run the code below. Note it is almost identical to the code above, but uses a sine wave rather than a square wave.

  • Explain why the output signal looks the way it does.

with FuncGen(Sine(
    Vmin=convert_DAC_voltage(V_MIN), 
    Vmax=convert_DAC_voltage(V_MAX), 
    freq=10)):
    
    time.sleep_ms(100)
    
    for i in range(num_samples):
      
        v_out = adc0.read_u16()
        output_signal[i] = v_out
        
        v_in = adc1.read_u16()
        input_signal[i] = v_in

        time.sleep_ms(DELAY_MS)

print('Acquisiton done!')

plt.plot(convert_samples_to_volts(output_signal, gain=0.1), color='r', label='Vout')
plt.plot(convert_samples_to_volts(input_signal), color='b', label='Vin')
plt.ylabel("Voltage [V]")
plt.xlabel("Sample number")
plt.grid()
plt.legend()

ℹ️ Hint

  • Think about how the sine is generated. How does such a signal look when zoomed in in voltage and time?

### TO DO=" your explanation for why the measured output does not look like a (co)sine" 
 

COMPARE & CONCLUDE#

⏳ Estimated time: 10 min

  • Wait till all (4) group members finish their observation

  • Compare your results with your other group members.

  • If your results agree, and are in line with all predictions, then talk to a TA and get checked off

  • Otherwise, so if your results do not agree, or your results are not in line with your predictions, then first discuss amongst your group before getting a TA.

to be checked off by a TA:

  1. Show the signal with the square wave input plus (not clipped) sawtooth

  2. You built and used a differentiator circuit with a sine input, but were (probably) unsuccesfull. The resulting data showed many spikes (short peaks). Discuss and explain what might have gone wrong

  3. exit card:

    1. Write a brief abstract on what you learned (conclusion, useful graph),

    2. Which troubleshooting skills do you want to remember for next sessions,

    3. Which code do you copy for use in next sessions,

  4. How do you think this notebook could be improved

#13C integrator-differentiator
### TO DO ="2. explain why we can't build a differentiator with sine wave input"

### TO DO="3a. abstract"

### TO DO="3b. troubleshooting"

### TO DO="3c. code"

### TO DO="4. what changes would you suggest?"