This example shows how to use the PmodDA4 DAC and the PmodAD2 ADC on the board, using the board's two Pmod interfaces. The notebook then compares the DAC output to the ADC input and tracks the errors.
The errors are plotted using Matplotlib and an XKCD version of the plot is produced (for fun). Finally a slider widget is introduced to control the number of samples diaplayed in the error plot.
Note: The output of the DAC (pin A) must be connected with a wire to the input of the ADC (V1 input).
In [1]:
from pynq.overlays.base import BaseOverlay
from pynq.lib import Pmod_ADC, Pmod_DAC
In [2]:
ol = BaseOverlay("base.bit")
In [3]:
dac = Pmod_DAC(ol.PMODB)
adc = Pmod_ADC(ol.PMODA)
In [4]:
dac.write(0.35)
sample = adc.read()
print(sample)
In [5]:
from math import ceil
from time import sleep
import numpy as np
import matplotlib.pyplot as plt
from pynq.lib import Pmod_ADC, Pmod_DAC
from pynq.overlays.base import BaseOverlay
ol = BaseOverlay("base.bit")
dac = Pmod_DAC(ol.PMODB)
adc = Pmod_ADC(ol.PMODA)
delay = 0.0
values = np.linspace(0, 2, 20)
samples = []
for value in values:
dac.write(value)
sleep(delay)
sample = adc.read()
samples.append(sample[0])
print('Value written: {:4.2f}\tSample read: {:4.2f}\tError: {:+4.4f}'.
format(value, sample[0], sample[0]-value))
In [6]:
%matplotlib inline
X = np.arange(len(values))
plt.bar(X + 0.0, values, facecolor='blue',
edgecolor='white', width=0.5, label="Written_to_DAC")
plt.bar(X + 0.25, samples, facecolor='red',
edgecolor='white', width=0.5, label="Read_from_ADC")
plt.title('DAC-ADC Linearity')
plt.xlabel('Sample_number')
plt.ylabel('Volts')
plt.legend(loc='upper left', frameon=False)
plt.show()
In this example, we extend the IO plot with a slider widget to control the number of samples appearing in the output plot.
We use the ipwidgets
library and the simple interact()
method to launch a slider bar.
The interact function (ipywidgets.interact) automatically creates user interface (UI) controls for exploring code and data interactively. It is the easiest way to get started using IPython’s widgets.
For more details see Using ipwidgets interact()
In [7]:
from math import ceil
from time import sleep
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from ipywidgets import interact
import ipywidgets as widgets
ol = BaseOverlay("base.bit")
dac = Pmod_DAC(ol.PMODB)
adc = Pmod_ADC(ol.PMODA)
def capture_samples(nmbr_of_samples):
delay = 0.0
values = np.linspace(0, 2, nmbr_of_samples)
samples = []
for value in values:
dac.write(value)
sleep(delay)
sample = adc.read()
samples.append(sample[0])
X = np.arange(nmbr_of_samples)
plt.bar(X + 0.0, values[:nmbr_of_samples+1],
facecolor='blue', edgecolor='white',
width=0.5, label="Written_to_DAC")
plt.bar(X + 0.25, samples[:nmbr_of_samples+1],
facecolor='red', edgecolor='white',
width=0.5, label="Read_from_ADC")
plt.title('DAC-ADC Linearity')
plt.xlabel('Sample_number')
plt.ylabel('Volts')
plt.legend(loc='upper left', frameon=False)
plt.show()
_ = interact(capture_samples,
nmbr_of_samples=widgets.IntSlider(
min=5, max=30, step=5,
value=10, continuous_update=False))