Home / Lab 2

Lab 2

The questions below are due on Friday September 13, 2019; 05:15:00 PM.
You are not logged in.

If you are a current student, please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.csail.mit.edu) to authenticate, and then you will be redirected back to this page.

Music for this Lab

Goals:In this lab we'll use our nascent analytical tools to design and build a digital-to-analog converter circuit, and use it to play music on a speaker.

1) The Challenge

Interfacing digital systems with the analog world often requires converting signals from digital values (as they are stored in computers) to analog values (as many signals in the world are). More precisely, on the computer signals are discretized in value. For example, a 4-bit digital register can store discrete decimal values from 0..15 (2^4-1). An audio signal traveling through the air, however, is continuously valued (it can take on a continuum of values).

To convert between these two representations we use circuits called digital-to-analog converters (DACs) and analog-to-digital converters (ADCs). Today we'll focus on creating a DAC. The Teensy has two built-in ways of creating analog voltages. It has a true DAC (we used it in Lab 1), and can also create analog voltages using pulse-width modulation (PWM), which we'll learn about in a few weeks.

What if you don't have a DAC or PWM? We can still create a DAC from digital outputs using only resistors and leveraging superposition, which we learned about in Lecture yesterday.


A lot of modern microelectronics are what we call digital, meaning they primarily work in terms of "High" and "Low" voltages. In the case of our Teensy microcontroller, it works with voltages that are 3.3V (a voltage often called V_{CC}) to represent the High voltage and 0.0V for the Low voltage (There's actually a range of voltages that will be treated as "High" and another range treated as "Low", but that is detail best left for 6.004 or 6.111. For today, it is simpler to assume it either 3.3V or 0.0V). The reasons for this High/Low system are covered in 6.004/6.111, but a brief explanation is it allows us to design circuits that are far more robust to noise (you no longer need to worry about if a voltage is 2.5V or 2.55V...you just worry if it is High or Low.). This digital abstraction is one of the main reasons why we can make extraordinarily complex digital systems. We can thus think about the outputs of microcontrollers as computationally controllable voltage sources. When a Digital I/O pin is set to be High it acts like a V_{CC} source (3.3V in the case of our Teensy) between that pin and ground. When it is set Low, it acts like there is a 0.0V source (short) between the pin and ground.

We use these High/Low voltages to convey information, and a common way in which we describe how much information we're dealing with is with a **bit**, which is the amount of information contained in one yes/no decision. The bit lends itself well to digital electronics since, as we've said like six times already, digital electronics only speak in two values (High Voltage /Low Voltage). So four wires carrying four digital voltages (either high or low) convey four bits of information. Microcontrollers perform their calculations in terms of this High/Low paradigm and never generate in-between voltages, but there's still a need to produce actual analog voltages (voltages that can be a wide range of numbers). What if you want to drive a speaker or other actuator that uses analog voltages? Or perhaps, more specifically, how can we create an in-between voltage such as 1.65V if the only voltages we can generate are 0.0 or 3.3V? The solution is a **D**igitial-to-**A**nalog-**C**onverter. What we'll eventually make today is a 4-bit DAC, meaning it is controlled by four separate High/Low Digital Pins.

2) Analyze a simple circuit

Let's build up the DAC circuit one step at a time. As a first step, examine the circuit below and determine the output voltage V_O when V_1 = V_{CC}.

For the questions below, use VCC for V_{CC} and R for R. Do not use V_1 in your solution, since V_1 = V_{CC}.

Enter a symbolic equation for V_O in terms of V_{CC} and R. V_O=

3) Superposition

Now, what happens if we have the following circuit instead:

We now have 5 nodes (including the reference node) and could set up a large system of equations. Don't do it!

Instead, we use superposition to solve this circuit, taking advantage of the fact that the circuit is comprised of constant resistors and independent sources, and is thus linear. Thus, the output voltage V_O is the sum of the output voltages produced when each voltage source is individually activated.

As a reminder of how to implement superposition, we turn off all sources except one. By turn off, we mean set the value of the source to zero (0 V in the case of a voltage source, as we have here).

Let's first turn off V_2 and leave V_1 on. When turning off a voltage source, we set the voltage across its terminals to zero, which is equivalent to replacing the source with a wire (short circuit). Remember that a current source turns into an open circuit when it's value is set to zero. So we're left with the following circuit:

Now, it looks like we still have 4 unknown node voltages, but you should be able to use series/parallel relations and voltage/current divider to solve this circuit without writing a set of equations.

Determine the output voltage V_O when V_1 = V_{CC} and V_2 = 0.

Don't get lost in the math here. If you are writing simultaneous equations and trying to solve them at once, ask for help to get on the right track.

For the questions below, use VCC for V_{CC} and R for R. Do not use V_1, V_2, etc., in your solution, since V_n are set to either 0 or V_{CC}$.

Enter a symbolic equation for V_O in terms of V_{CC} and R. V_O=

Similarly, determine the output voltage V_O when V_2 = V_{CC} and V_1 = 0.

Enter a symbolic equation for V_O in terms of V_{CC} and R. V_O=

Now, using superposition, we see that the output voltage when both sources are ON is the sum of the output voltages due to each source. Determine the output voltage V_O when V_1 = V_2 = V_{CC}.

Enter a symbolic equation for V_O in terms of V_{CC} and R. Enter V_{CC} as VCC. V_O=

Stepping back, we see that by independently turning on V_1, V_2, or both, we can create 4 distinct analog voltages even if V_1 and V_2 each take on only two values (0 and V_{CC}). We have made a 2-bit DAC! We also see that when V_2 is ON (=V_{CC}) it changes the voltage by twice as much as when V_1 is ON (=V_{CC}). We call V_1 the least significant bit (LSB) and V_2 the most significant bit (MSB).

4) Paper design

Now, let's make a paper design. Think about extending this resistor topology to create a 4-bit DAC using four voltage sources. Draw it out and analyze the four cases where each individual voltage source is ON to convince yourself that this topology will work.

Feel free to think about this for 10 or 15 minutes to try to come up with a topology. Talk to a neighbor if you wish. If you're stuck after 15 minutes or so, ask staff for help.

For your four-bit DAC design where V_{CC} = 3.3 V (the supply voltage of the Teensy), what is the smallest voltage change that you can create [in V]?

For your four-bit DAC design where V_{CC} = 3.3 V (the supply voltage of the Teensy), what is the largest voltage that the circuit can create (when all bits are 1) [in V]?

Checkoff 1:
Explain your paper design to a staff member. Show your calculations of the output voltage.

5) Implement

Now let's build your design. We're going to use Teensy digital pins 3-6 to implement voltage sources V_1..V_4, respectively. In this case, V_1, which is pin 3, corresponds to LSB, while V_4, which is pin 6, corresponds to the MSB.

What should you use for resistors? Well, in theory it shouldn't matter, but in practice it does. If the resistor values are too small (<1 k\Omega), they will draw too much current from the Teensy pins (max current provided by the Teensy on a digital out is 9 mA) and dissipate alot of power when used, which is bad design. If the resistor values are too large (>50 k\Omega), then when you connect the DAC to a load (a piezo speaker in this case) the circuit will get loaded down (which we can analyze exactly using Thevenin equivalents). So pick some value in the middle. It's also good to pick a value R for which 2R is also available.

Never put resistors back into the cabinet. If you take out a resistor and don't want it, throw it away. And if any resistor value is missing, ask a staff member. There are plenty more resistors in the stockroom.
When creating integrated circuits, it is often difficult to create precise values of resistors but easy to control resistor ratios. This DAC topology is thus ideally suited to integrated circuit implementation.

Once you have hooked up the circuit, get today's code from HERE. In the code towards the top, set the flag STEP_THROUGH to 1 in the code, then compile and download onto the Teensy. This code sequentially runs through the 16 values of the 4-bit DAC, pausing on each for 50 milliseconds. Use your oscilloscope to observe this waveform (of course ask staff for help if needed!) You should see a plot similar to what is shown below. If your step sizes are not uniform or if it isn't a continuous staircase, there's a problem with your circuit.

Once you're good, change STEP_THROUGH to 0, and recompile and download. Obtain a piezo buzzer from the staff table. Connect the output of the circuit to the piezo buzzer as shown below:

The piezo buzzer is a simple electrical-to-sound transducer (acts as a speaker) that has a high resistance (~30k\Omega at 1 kHz), and thus is easy to drive with the DAC you've created. The Teensy will play an awesome song, although if your resistor values are too large it might be somewhat quiet. Observe the output signal on your oscilloscope. Next, adjust the VOLUME constant in the code to 0.5. Reupload. What happens to the signal? There are several other songs (riffs) you can play (which you can select by commenting/uncommenting the apprpriate variable declarations towards the top of the code) if you'd like. Depending on your construction, you should see a signal that looks like the following while playing music:

Checkoff 2:
Show your working circuit to a staff member. Demonstrate the output waveform on the oscilloscope with and without the piezo attached. Discuss the pros and cons of this type of DAC, the signals on the scope, and any other questions you have.

6) Cleanup

Before you leave, it's time to clean up again! Steps for cleanup:

  • Return your piezo buzzer to the staff table so we have enough for the other lab sections.
  • Carefully pick up your system and place into its plastic case.
  • Throw away loose wires on your desk.
  • Throw away paper, food, etc. on your desk.

Checkoff 3:
Show your cleaned-up lab space to a staff member.