In this lab, we will be measuring the time-response of the voltage across a capacitor in an RC circuit. We will apply an input voltage and measure the \(v_o\) response with an NI myRIO single-board computer, export it to a data file, import that data file into Python, plot the data, derive an analytic model, and compare the analytic model to the data.
The objectives of this lab exercise are for students:
The following materials are required for each lab station:
Use the following procedure to build the circuit.
Build the RC circuit (sans source) on a breadboard. The capacitor is polarized; that is, it has a positive and a negative terminal. We must take care to connect such capacitors such that the voltage always drops from the positive terminal to the negative terminal. Sometimes this is indicated by a shorter lead and/or a "\(-\)" symbol on the negative side. (In our case "\(-\)" should connect to the ground node.)
Connect the myRIO to power and to your workstation computer via USB. It will be both the voltage source \(V_s\) and measurement of \(v_o\).
We will be using the MSP connector named C on the side of the myRIO shown in the figure below.
CWith jumper wires, connect the analog output ground channel AGND (3) to the ground of your circuit (ensure this is connected to the "\(-\)" capacitor terminal).
Connect the analog output channel AO0 (4) to the resistor such that the analog voltage supplied via AO0 and AGND is applied across both the resistor and capacitor. You now have \(V_s\) supplied by analog output AO0.
Connect AI0- (8) to the circuit's ground.
Connect AI0+ (7) to the node shared by the resistor and capacitor. You now have a measurement of \(v_o\) on analog input AI0.
The LabVIEW project for this lab is available for download here:
Download the zip file and extract it (you have to actually extract it, not just open it up or a later step won't work). Open the LabVIEW project (not the VI) lab03_RCCircuit.lvproj in LabVIEW. Expand the myRIO device and open the revealed Main.vi program. The front panel of the VI looks as shown in figure 3.
The block diagram of the VI might look as shown in figure 4.
The Main.vi program has the following functionality:
AO0 when the user toggles a button named executeAI0+, continuously.lvm datafile on the myRIO via the array_save_newfile.vi and array_save_append.vi functionsThe code that precedes the Main Loop resets the controls on the front panel to their default values. The block labeled This VI is the VI Server Reference VI and is found in the Functions > Application Control palette. The block labeled Defaults Vals.Reinit All is the Invoke Node VI and is found in the Functions > Application Control palette. After wiring the VI Server Reference to the input of Invoke Node, right-click on the word Method on the Invoke Node VI and select Method for VI Class>Default Values > Reinitialize All to Default. Finally, the block labeled C is the Close Reference VI and is found in the Functions > Application Control palette.
The Case Structure labeled append data to arrays has a False panel that simply passes through arrays instead of appending to them.
The following procedure can be used to capture the charging of the capacitor.
/c/data/datafile001.lvm.Main.vi. You should see a zero voltage \(V_s\) and \(v_0\) reading on the chart./c/data.Write a report on your laboratory activities using the template given.
Include the following plots (use Python to make the plots) for both the charging portion of your data and the discharging portion:
The measured \(\widetilde{V}_s\) and \(\tilde{v}_o\) as functions of time and
The measured \(\tilde{v}_o\) as a function of time overlayed with your theoretical prediction for \(v_o\).
Include all measurements made by hand (e.g. resistance).
Include an analysis of a voltage RC circuit that can be used to produce the theoretical predictions in the plots.
It may be helpful to take pictures during the laboratory procedure. These can be included in your report.
Loading the LVM data file into Python is straightforward. You will need to install the lvm_read package using pip:
pip install git+https://github.com/ricopicone/lvm_readFirst, load the lvm_read package:
import lvm_read
import numpy as npPlace your data file, say datafile001.lvm, in the same directory as your Python script. Load the data using the lvm_read.read() function:
data = lvm_read.read('datafile001.lvm')Now you can access the data using the data variable. It’s pretty messy, so we’ll parse it into a more usable format, an matrix of shape (n_samples, n_channels), where n_channels is 3 for us (time, source voltage, capacitor voltage):
n_samples = len(data[0]['data'])
n_channels = 3
data_array = np.zeros((n_samples, n_channels)) # Initialize data array
for i in range(n_channels):
data_array[:, i] = [p[0] for p in data[i]['data']]
print(f"Data head:\n{data_array[:5]}")Data head:
[[0. 1. 0.009766]
[0.01 1. 0.019531]
[0.02 1. 0.029297]
[0.03 1. 0.043945]
[0.04 1. 0.053711]]
Here’s an example of how to plot the data:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(data_array[:, 0], data_array[:, 1], label='Source Voltage')
ax.plot(data_array[:, 0], data_array[:, 2], label='Capacitor Voltage')
ax.set_xlabel('Time (s)')
ax.set_ylabel('Voltage (V)')
ax.legend()
ax.grid(True)
plt.draw()To plot a theoretical function on the same plot, we could define a function such as the following:
def theretical(t):
return 1 - np.exp(-t / 1)Now plot both the theoretical capacitor voltage and the measured voltage together:
fig, ax = plt.subplots()
ax.plot(data_array[:, 0], data_array[:, 1], label='$V_S$')
ax.plot(data_array[:, 0], data_array[:, 2], '.', label='$v_C$ Measured', alpha=0.5, markersize=4, marker='o', markerfacecolor='orange', markeredgecolor='none')
ax.plot(data_array[:, 0], theretical(data_array[:, 0]), label='$v_C$ Theoretical')
ax.set_xlabel('Time (s)')
ax.set_ylabel('Voltage (V)')
ax.legend()
ax.grid(True)
plt.show()