Digital-to-Analog Converter

The questions below are due on Friday February 23, 2024; 05:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Getting Help

We have a help queue active for lab! Go through the lab, answer the questions on this page, and ask for checkoffs on the queue! When you're all done with everything, you're done! If you get stuck on the way, let us know; we're happy to help!

Remember that labs are intended to be a learning experience, not a test, so take your time and understand things, and please ask us questions as you're working through!

Although we do want everyone to work individually and build their own circuits, it is also more than OK to ask friends/neighbors for help, too.

1) Introduction

One way that circuits demonstrate their usefulness is as transducers, converting signals from one domain (light, temperature, pressure, electrical, etc.) to another. We've seen examples of this in just about every lab so far; in lab 1, we were converting information about light and sound into electrical sigals, and in lab 2, we were using the joysticks and buttons to measure information about the world as well.

Last week, we took an additional step of including a microcontroller (in our case, a Teensy) and used it as an interface to take measurements about the real world and digitize them so that we could make use of them inside of a computer (to determine the position of the joystick as well as the buttons that were being pressed. This illustrates another amazingly-useful application of circuits: as interfaces between the real world and computation.

The Teensy microcontroller makes a comeback today, where we'll use it to demonstrate conversion in the other direction: rather than using the Teensy to measure analog voltages from the world, we'll use it to generate them.

The Teensy is a digital electronic device, meaning that it operates using only two types of voltages: On and Off, with "On" being a high voltage and "Off" being a low voltage. In the Teensy, the "On" voltage is around 3.3 Volts and the "Off" voltage is near 0V; and, as a digital device, the Teensy does not utilize voltages in between those values internally. However, if we're thinking about interfacing with the physical world, it is often useful to be able to work with analog voltages like the ones we've mostly seen so far in lab (which can take any value, not just "On" or "Off"). This lab will be an opportunity to explore that idea a little bit by building a 6-bit digital-to-analog converter, which will allow us to use the Teensy to generate a more diverse set of voltages.

2) Analysis

Here's the circuit we'll be using today. Parts of it should look familiar from the last problem set and the prelab, but it is a little bit different. We'll start by analyzing this circuit, and then we'll build it and play with it a little bit to round out today's lab.

We're going to be re-using this circuit for the next several labs, so it's worth making sure that you understand how it works, in-depth; and when we get to building it, it's also worth trying to lay it out nicely on the board. Feel free to ask for help on either or both of those fronts!

In this configuration, what is the voltage v_\text{out} in terms of V_1, V_2, V_3, V_4, V_5, V_6, and/or R? Use superposition. You may find it easier to start by finding V_1's contribution, rather than the other way around).
Enter your answer as a Python expression, using values V_1, V_2, V_3, V_4, V_5, V_6, and R, respectively, to refer to these values.
v_\text{out} =~

3) Teensy and the Digital Abstraction

As mentioned above, this circuit is useful as a digital-to-analog converter (DAC, often pronounced like "dack" instead of "dee-eh-see"). We're going to hook it up to a Teensy microcontroller, which is only capable of producing two voltages: a "high" voltage of 3.3 Volts and a "low" voltage of 0 Volts relative to its internal ground. programmatically, we can set any pin to "high" or "low", making the corresponding pin look like this:

By hooking various pins of the Teensy up to a circuit like the one at the very top of the page, we'll be able to get a much wider variety of voltages from the Teensy.

Imagine hooking up the Teensy's pins so that they provide V_1, V_2, V_3, V_4, V_5, and V_6 in the circuit at the top of the page. How many distinct values of v_\text{out} will we be able to make?

In that same configuration, what is the maximum voltage we'll be able to make by turning pins "on" and "off", in units of Volts? Enter your answer accurate to within 10^{-2} Volts.

In that same configuration, what is the smallest change we'll be able to make by turning a pin on/off, in units of Volts? Enter your answer accurate to within 10^{-4} Volts.

4) Grab Your Equipment

Now that we know what to expect, we're going to build the circuit from up above, using several of the Teensy's pins to provide the voltage sources indicated there.

Since it takes a while to boot up, turn on your scope before going to grab your parts so that it can be warming up while you're grabbing things. Then grab a Teensy (make sure it says Teensy 4.0 on the bottom, not Teensy 3.2), a breadboard, and a speaker. We'll also need to cut some wires, so grab yourself some clippers as well.

For this lab, grab a full-sized breadboard since the circuit we're going to be building is pretty big (especially if you decide to try some of the optional pieces at the end).

not the little one
with 30 rows.
the BIG one
with 60 rows.

When using the Teensy with a breadboard, it's best to plug it in on the very end of the board so that it's spanning the gap in the board, with its USB jack facing outward, like so:

It can take some work to get it into the breadboard. Be careful not to break things, but it's OK to use a little bit of force to get the pins to go in.

Once you've done that, construct the circuit from the very top of the page, using pins 14, 15, 16, 17, 18, and 19 to provide V_1, V_2, V_3, V_4, V_5, and V_6, respectively. Use a value for of 1{\rm k}\Omega for R when building your circuit.

As a layout tip, it is good to try to make the structure of this circuit on your breadboard somewhat match the structure of the schematic, if possible. Specifically, we recommend having the 2R resistors cross the center gap in the breadboard, with two open holes between each one (which makes connecting them via R resistors easy); and having the v_{\rm out} side of the circuit be on the side of the Teensy (i.e., having V_1 close to the Teensy and V_6 farther away).

We're happy to consult on board layout as well; just ask!

Also note that the pin labeled "G" in the very corner of the board is the Teensy's internal ground. The "-" sides of the voltage sources in the circuit above are all connected to that spot internally by the Teensy (as shown below), so there are some other components we'll need to connect there as well to complete the circuit.


 
A partial view of a model of the Teensy's internals

Check Yourself 1:
In our original schematic, we the two 2R resistors on the sides connected to a single node shared by the - sides of all of the sources. Here, though, we only have a single wire connecting to the Teensy's ground (pin G). How can we adjust the schematic from up above to account for this fact? Where and how should we wire up the Teensy's pin G? What components should it connect to?

We're going to use a couple of different programs to test things today. You can download them all as a ZIP file: dac_code.zip.

To start, we're going to use the program called "stepper" (stepper/stepper.ino inside of that ZIP file), which you should flash to your Teensy using a similar process to what we did last week:

  • Open Arduino (Teensyduino on Mac) and open stepper/stepper.ino.
  • From the menu, under Tools\toBoard: ...,\toTeensyduino, select Teensy 4.0. This tells Arduino what kind of board we're programming.
  • Plug your Teensy into the computer via USB cable (we have extras if you need one) and double-tap the button within 0.5 seconds of plugging it in.
  • From the menu, under Tools\toUSB Type: ..., make sure Serial is selected.
  • Click the second button from the left, which looks like a rightward-pointing arrow (it should say "Upload" when you hover over it). This should compile things and send the result to the Teensy with no orange error messages.

If you use channel 1 on the scope to measure v_\text{out}, you should see what looks like a little ramp repeating itself every so often. Make sure to turn on "BW Limit" so things aren't too fuzzy. If you zoom in closely, you can see that it's not actually a smooth ramp, though; it's got lots of little sements in it if you zoom in enough. You may also wish (for now) to set a trigger in the middle of the ramp or something like that, and switch triggered to Normal mode (which should help hold things in place). Or you can use the Run/Stop button manually to freeze things on the screen.

Check Yourself 2:
Use the cursors on the scope to measure various properties of the wave to see whether they agree with the theory from above:
  • What is the maximum voltage in that waveform?
  • What is the spacing (in volts) between the voltage levels?

Given those measurements, how many distinct steps are there in the ramp?

Be prepared to talk about these measurements and how you made them during your checkoff.

Check Yourself 3:
Given the pattern you see, how is the Teensy toggling the pins? Can you predict the pattern? Try using your scope to measure the individual pins after you have a hypotheses. Does it make sense that changing them in this way causes the pattern we see in v_\text{out}?

Checkoff 1:
Discuss your results so far with a staff member. How did you solve for the values in the circuit at the top of the page? Do your experimental results match the theory?

5) A Second Program

Now load the "song" program (song/song.ino in the ZIP file) and continue measuring the output. Hit "Default Setup" and then use the controls on the scope to get a nice view of the output wave (we want the wave to be taking up most of the screen). If you had your trigger in Normal mode for measuring the ramp, switch it back to Auto.

Now, let's listen to it by hooking up a speaker across the port labeled v_{\rm out} in the diagram. You should hear a song. But oh, no! It's really quiet. And oh, no! Look at the scope!!! What changed about your signal?

Check Yourself 4:
Try plugging and unplugging the speaker while looking at your scope; what changes about the signal when the speaker is connected?

Let's see if we can understand this change using circuit theory. But oh, no! The DAC is a complicated circuit with six independently-controllable voltage sources and thirteen resistors, so solving that seems like kind of a pain. nervous emoji. It wasn't too bad to analyze using series/parallel combinations and divider relationships, but still...it sure would be nice if we could model it as a simpler circuit instead...

...wouldn't it?

...

Thankfully, as we saw in lecture, we do have a way to to this! We can model our whole DAC circuit (including the Teensy) with a Thévenin equivalent. Thanks, Helmholtz!

By thinking about the Thévenin equivalent of our DAC, we can model our circuit, with and without the speaker connected, as shown below; much nicer to analyze with way fewer components! party_ball emoji partying_face emoji celebration emoji

without speaker with speaker

Check Yourself 5:
Given this model, can you see why the voltage output changed when we added the speaker?

Let's analyze this in a little bit more detail. To start, let's model the speaker as a resistor. What is its resistance, in Ohms? (Note that you can measure this directly using your multimeter).

Now, let's make some measurements and see if we can empirically determine the Thévenin resistance of our DAC circuit. Firstly, let's switch back to the stepper.ino program on our Teensy. Then, use the scope to answer the following two questions:

  • What is the peak voltage of the stepper program without the speaker connected?
  • What is the peak voltage of the stepper program with the speaker connected?

Note that you may need to zoom way in to make this second measurement, and if the ramp looks really fuzzy, you'll want to measure in the middle of that fuzz, not on top of it (and make sure that "BW Limit" it on to reduce the fuzz in the first place!).

Given these measurements and the Thévenin model of the DAC up above, estimate the Thévenin resistance of your DAC, and enter your answer below, in units of Ohms:
R_{\rm TH}\approx~

Check Yourself 6:
Do these results match (approximately) what the theory predicted? Be prepared to discuss the result and your method(s) during your checkoff.

This last part of the lab hopefully demonstrates a couple of neat things:

  1. By introducing computation, we can work with arbitrarily-complicated signals and perform arbitrarily-complicated operations easily.

  2. Using our circuit, we can take those results and turn them into something that the scope/speaker/world can use!

  3. Thévenin equivalents are useful for more than just solving p-set problems; they helped us understand the issue with the sound being so quiet!

Next week we'll see what we can do about the issue we noticed today with the really quiet output, but we'll have to leave you on the edge of your seat for now...

Checkoff 2:
Discuss your results with a staff member. How did you calculate R_\text{TH} using the scope measurements? Demonstrate the song playing as well.

When your checkoff is done, clean up (instructions below)!

DO NOT DESTROY YOUR DAC; WE'LL NEED IT LATER!!!

DON'T THROW YOUR DAC AWAY!!!

We're going to use the DAC in a couple of labs in the future. If you want to leave it in the lab, put some masking tape on the back and write your Kerberos username or something on it so you can find it (you can leave the speaker connected), then put it on the top shelf of your lab station. You can also take it with you, but if you do, make sure to bring it back; if you lose it, that's on you, and it might hamper your progress in some of the future labs.

PLEASE RETURN USB CABLES, THOUGH. Otherwise your friends in the other lab section might get upset when we don't have enough to go around.

Have a good weekend!

6) (Optional) Extra Things To Do If You Want To

None of the stuff here has a checkoff or affects your grade or anything like that, but if you're having fun, there are some other things you might explore:

  • Try changing the rate at which we move through the samples in the song.ino file. What happens if you speed it up by a factor of two? What if you slow it down by a factor of 2?

  • Right now, there is a fair amount of distortion in the audio output from the fact that we only have 64 distinct voltage levels. Try adjusting things so that your DAC uses 8 bits instead. How can you extend your circuit to make this happen? If you do this, change the DAC_RESOLUTION variable to be your new number of bits in all of the programs, and use pins above 19 for the new bits. The stepper demo should now produce a much smoother results, and if you change #include "song_data_6bit.h" to #include "song_data_8bit.h", you're hear a cleaner audio signal as well.

  • Try using the Teensy's input pins to detect button presses and use those to change the frequency of a sine wave (the sine/sine.ino program can be a good starting point), making yourself a little musical instrument. Or, measure the (continuous) voltage from a potentiometer hooked up as a voltage divider and using that voltage to control the frequency of a sine wave so that you can smoothly go between different frequencies.

If you do try these things (or other experiments of your own), we'd love to see what you come up with! :)