{"id":741,"date":"2017-06-01T22:38:07","date_gmt":"2017-06-02T02:38:07","guid":{"rendered":"http:\/\/www.circuitcrush.com\/?p=741"},"modified":"2021-06-30T21:07:05","modified_gmt":"2021-07-01T01:07:05","slug":"digital-analog-converters-tutorial","status":"publish","type":"post","link":"https:\/\/www.circuitcrush.com\/digital-analog-converters-tutorial\/","title":{"rendered":"Digital to Analog Converters – An Introductory Tutorial"},"content":{"rendered":"<p>At first, I was going to write about the analog to digital converter (ADC). I decided to write about the digital to analog converter instead.<\/p>\n<p>Why?<\/p>\n<p>I started gathering information about ADCs. Then it hit me: digital to analog converters (or DACs) are often part of analog to digital converters.<\/p>\n<p>So, the topic this time will be the types of digital to analog converters and a bit (sorry for the pun) on how they work.<!--more--><\/p>\n<p>This post is an introductory post, so we won\u2019t necessarily cover every single DAC out there, nor will we go into immense detail about how the ones we do discuss work, though we will touch on the basics.<\/p>\n<p>DACs are simpler than ADCs, and unlike ADCs, there are only a few practical ones in use today.<\/p>\n<p>Now, let\u2019s delve into digital to analog converters and their types.<\/p>\n<h1><strong>Digital to Analog Conversion 101<br \/>\n<\/strong><\/h1>\n<p>The function of a digital to analog converter is to convert a sequence of digital bits (usually stored in some sort of register) into an analog signal. That is, a DAC takes a binary number and converts it an analog voltage that is proportional to the binary number. If we feed a DAC with different binary numbers in quick succession a complete analog waveform is created.<\/p>\n<p>If you\u2019re not 100% sure what the difference between digital and analog is, my post <a href=\"http:\/\/www.circuitcrush.com\/analog-vs-digital\/\">Analog vs Digital: What\u2019s the Difference?<\/a> can help.<\/p>\n<p>A typical DAC is an active circuit in the form of an IC and may consist of a data register, solid state switches, resistors, and op-amps powered by an external supply.<\/p>\n<p>They can be either unipolar or bipolar. A unipolar DAC has a single polarity. A bipolar unit can have positive and negative output signals. These often use either offset binary or 2\u2019s complement code rather than straight binary like DACs in unipolar mode do.<\/p>\n<p>Sometimes, you\u2019ll see a particular type of digital to analog converter listed as a multiplying DAC. These produce an output that is proportional to the product of a varying input reference (like a voltage) times a digital code. These commonly find a place in systems that use ratiometric transducers like position potentiometers, strain gauges, and pressure sensors to help correct errors that may occur.<\/p>\n<p>The full-scale voltage (V<sub>FS<\/sub>) of a DAC is the maximum analog level that it can reach when applying the highest binary code (which would be 1111 for a 4-bit DAC). In general, at full scale the analog output of an n-bit DAC is given by the equation below.<\/p>\n<p>Eq 1: max output = ((2<sup>n<\/sup>-1) \/ 2<sup>n<\/sup>) V<sub>ref<\/sub><\/p>\n<p>So, assuming a V<sub>ref <\/sub>of 5 V, the maximum analog output of an 8-bit DAC would be about 4.98 V.<\/p>\n<p>DACs find a home in op-amp gain control circuits, waveform generators, process control and autocalibration circuits, and consumer electronics such as MP3 players (often as part of a microcontroller).<\/p>\n<p>A few examples of DACs you may use are the 8-bit DAC0808 and the 12-bit DAC8083A. Of course, there are others.<\/p>\n<p>There are two main types of digital to analog converters that we\u2019ll talk about in this post.<\/p>\n<h1><strong>Types of Digital to Analog Converters (DACs)<br \/>\n<\/strong><\/h1>\n<h2><strong>Binary-Weighted Digital to Analog Converter<br \/>\n<\/strong><\/h2>\n<p>Below we have a simple, 4-bit binary-weighted digital to analog converter (a.k.a. weighted resistor DAC).<\/p>\n<p>The picture is a bit simplified as it lacks the digital switches that switch the 4 bits on and off, but the basic gist remains the same.<\/p>\n<p> <\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-742 alignnone\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/Digital-to-Analog-Converter.jpg\" alt=\"Weighted resistor digital to analog converter\" width=\"719\" height=\"333\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Digital-to-Analog-Converter.jpg 719w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Digital-to-Analog-Converter-600x278.jpg 600w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Digital-to-Analog-Converter-150x69.jpg 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Digital-to-Analog-Converter-300x139.jpg 300w\" sizes=\"(max-width: 719px) 100vw, 719px\" \/><\/p>\n<p><em>Figure 1: a weighted DAC. From Digital Fundamentals, 7<sup>th<\/sup> Edition.<\/em><\/p>\n<p>The resistor values represent the binary weights of the input bits. I<sub>f <\/sub>is the sum of currents I<sub>0 <\/sub>through I<sub>3<\/sub>. Due to the properties of the op-amp, almost no current flows into it, rather, current flows through the feedback resistor R<sub>f<\/sub>. Another property of op-amps is that they try to keep both the inverting and non-inverting inputs at the same potential. Because the non-inverting input is grounded, the inverting input is also at 0 volts. This is known as a virtual ground.<\/p>\n<p>If the input voltage is zero (binary 0000), the current is also obviously zero. If the input voltage is some other value (from a binary number greater than 0), then the amount of current depends on the input resistor value and is different for each resistor.<\/p>\n<p>The values of the resistors are inversely proportional to the binary weights of the corresponding bits. For example, the lowest value resistor R corresponds to the MSB, 2<sup>3<\/sup> in this case, which is eight (binary 1000). The other resistors are multiples of R.<\/p>\n<p>This design makes the output voltage V<sub>out<\/sub> proportional to the binary input value.<\/p>\n<p>In practice, weighted resistor DACs are seldom used.<\/p>\n<p>For example, if we want an 8-bit weighted resistor DAC, not only would we need 8 resistors, but the values of the resistors would need to be extremely precise (a tolerance less than 0.5% in this case) or conversion errors will result. We\u2019ll touch on some conversion errors later.<\/p>\n<p>These values would range from 1k\u2126 to 128k\u2126 (assuming R = 1k\u2126).<\/p>\n<p>As far as I know, there is no standard 128k\u2126 resistor in production, not to mention one with a tolerance of less than 0.5%! This is circuit is obviously difficult to manufacture, and as we\u2019ll see in a minute, there is a better solution.<\/p>\n<h2><strong>The R\/2R Ladder Digital to Analog Converter<br \/>\n<\/strong><\/h2>\n<p>The R\/2R ladder DAC overcomes the problems inherent with the weighted resistor DAC in that it requires only 2 resistor values. These values are standard values that are in production and usually range from 2k\u2126 to 10k\u2126.<\/p>\n<p>The circuit is expandable to any number of bits simply by adding one resistor of each value for each bit. This is another advantage of the R\/2R DAC.<\/p>\n<p>A simplified representation of an R\/2R ladder digital to analog converter is shown below.<\/p>\n<p> <\/p>\n<p><img decoding=\"async\" class=\" wp-image-743 alignnone\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-DAC.jpg\" alt=\"R\/2R Ladder DAC\" width=\"745\" height=\"354\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-DAC.jpg 1112w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-DAC-600x285.jpg 600w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-DAC-150x71.jpg 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-DAC-300x142.jpg 300w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-DAC-768x365.jpg 768w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-DAC-1024x486.jpg 1024w\" sizes=\"(max-width: 745px) 100vw, 745px\" \/><\/p>\n<p><em>Figure 2: an R\/2R ladder DAC.<\/em> <em>From Digital Fundamentals, 7<sup>th<\/sup> Edition.<\/em><\/p>\n<p>The circuit above requires an op-amp with a high slew rate (the rate at which the output changes after a step change in input). If you grab a standard 741 variety op-amp, the circuit will not accurately reproduce changes introduced by large changes in the digital input.<\/p>\n<h3 style=\"text-align: center;\">Become the Maker you were born to be. Try <a href=\"https:\/\/learnarduinonow.com\">Arduino Academy<\/a> for FREE!<\/h3>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-4238\" src=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png\" alt=\"\" width=\"344\" height=\"131\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png 828w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-300x114.png 300w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-150x57.png 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-768x292.png 768w\" sizes=\"(max-width: 344px) 100vw, 344px\" \/><\/p>\n<p>One way to analyze and understand how this circuit works is to replace the R\/2R ladder with its Th\u00e9venin equivalent circuit, then treat it as an inverting amplifier.<\/p>\n<p>The topic of Th\u00e9venin circuit equivalents is a subject that could at least take up another whole post, so I won\u2019t go into any great detail about how or why it works here. Just know that it\u2019s a way to simplify (or complicate, if you ask the average EE student) the analysis of electric circuits.<\/p>\n<p>Looking in on the circuit from its left end, we see two 2R resistors in parallel. This simplifies to R. Now, the parallel combination of R<sub>1<\/sub> and R<sub>2<\/sub> are in series with R<sub>4<\/sub>, giving us yet again 2R. Now this 2R combination is in parallel with R<sub>3<\/sub>.<\/p>\n<p>You get the idea.<\/p>\n<p>The process repeats until we reach the op-amp. At this point, the total resistance of the ladder is just R. This is true regardless of how many inputs or bits we have.<\/p>\n<p>In case you still don\u2019t quite understand how this works, the picture below can help. R<sub>TH<\/sub> is the Th\u00e9venin equivalent resistance.<\/p>\n<p> <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-744 alignnone\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-Digital-to-Analog-Converter.jpg\" alt=\"R\/2R DAC Thevenin equivalent\" width=\"665\" height=\"262\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-Digital-to-Analog-Converter.jpg 665w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-Digital-to-Analog-Converter-600x236.jpg 600w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-Digital-to-Analog-Converter-150x59.jpg 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/R-2R-Ladder-Digital-to-Analog-Converter-300x118.jpg 300w\" sizes=\"(max-width: 665px) 100vw, 665px\" \/><\/p>\n<p><em>Figure 3: derivation of the Th\u00e9<\/em><em>venin equivalent of the R\/2R circuit. Analysis starts on the left end with 2R ||2R.<\/em><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p>Like the weighted resistor DAC, very little current flows into the op-amp and both terminals are at 0V. Thus, almost all the current flows through the feedback resistor and V<sub>out<\/sub> is equal to -IR<sub>f<\/sub>.<\/p>\n<p>Because of this, the value of V<sub>out <\/sub>DOES depend on the digital code, though the ladder\u2019s total resistance does not. If we make R<sub>f<\/sub> = R, then we can calculate the analog output using the equation below. This of course can be expanded for any number of bits.<\/p>\n<p>Eq 2: V<sub>out<\/sub> = -[d<sub>3<\/sub>\/2 + d<sub>2<\/sub>\/4 + d<sub>1<\/sub>\/8 + d<sub>0<\/sub>\/16] V<sub>ref<\/sub><\/p>\n<p>Where V<sub>ref <\/sub>is a logic HIGH and d<sub>x<\/sub> is either a 1 or a 0.<\/p>\n<p>The R\/2R ladder and op-amp are at the heart of most types of digital to analog converters you\u2019ll see today.<\/p>\n<p>In fact, making a digital to analog converter from scratch isn\u2019t worth it. You\u2019re better off buying a DAC IC or using the one built into your microcontroller or ecosystem board.<\/p>\n<h2><strong>Digital to Analog Converter Performance Specs<br \/>\n<\/strong><\/h2>\n<p>Several factors affect the performance of a DAC. Here are a few of the major ones to watch out for. Of course, I can\u2019t cover every single possible spec a DAC could have in one blog post.<\/p>\n<p><strong>Monotonicity<\/strong>: Math geeks call a mathematical function monotonic if it is either increasing or decreasing on its entire domain. For example, X<sup>3<\/sup> is monotonic (it always gets bigger whether X is negative or positive) and X<sup>2<\/sup> is not (it decreases in the left quadrant and increases in the right quadrant). Such is similar with DACs. A DAC is monotonic if the output voltage increases every time the input code increases.<\/p>\n<p><strong>Resolution<\/strong>: The resolution of a DAC is the smallest voltage change the device can output for the smallest input change, such as when the LSB of a binary number changes. In other words, it is the measure of how finely its output may change between discrete, binary steps. This metric often results in confusion, for reasons we\u2019ll tackle in a minute.<\/p>\n<p>The resolution of a DAC can also refer to number of bits it can handle.<\/p>\n<p>Resolution is often expressed as either a percentage or a voltage.<\/p>\n<p>When expressing resolution as a percentage, we calculate it with the formula below.<\/p>\n<p>Eq. 3: Resolution = [1 \/ (2<sup>n<\/sup> \u2013 1)] x 100<\/p>\n<p>Where n is the number of bits.<\/p>\n<p>When we want to express resolution as a voltage we can use the formula below.<\/p>\n<p>Eq. 4: Resolution = V<sub>FS<\/sub> \/ 2<sup>n<\/sup><\/p>\n<p>Where n is again the number of bits and V<sub>FS<\/sub> is the DAC’s full scale output voltage for the maximum binary input.<\/p>\n<p>Note that sometimes you may see the formula for DAC resolution written as equation 5 below.<\/p>\n<p>Eq.5: Resolution = V<sub>FS<\/sub> \/(2<sup>n<\/sup>-1)<\/p>\n<p>After poring over dozens of websites and books, I could not find any explanation as to why one would pick equation 5 over 4 or vice versa. Regardless, the difference in the answers you get is very small.<\/p>\n<p>Below is my best educated guess as to why this is so. If I\u2019m wrong and you know which formula is correct, please comment and let us know why.<\/p>\n<p>My educated guess: People who use this formula are counting \u201csteps\u201d instead of voltage levels.<\/p>\n<p>Remember that zero is a number and that the highest 8-bit number we can write is actually 255 (binary 1111 1111), not 256. This is because the \u201cmissing count\u201d is used by the number zero (binary 0000 0000), so though we can represent 256 different numbers in 8 bits, our range is from 0-255, not 1-256.<\/p>\n<p>Since zero is not a \u201cstep\u201d from one level to another, we subtract 1 from the total number of possible values (2<sup>n<\/sup>) in equation 5.<\/p>\n<p>Since zero volts is technically a voltage level, many use the formula in equation 4. It really depends on the perspective you use. As always, be consistent with which ever you pick!<\/p>\n<p><strong>Update: It seems I’m not the only one who’s noticed that there are two different formulas for resolution floating around the ‘Net. In his article <em>Rectifying an A\/D Conversion Error<\/em> (Circuit Cellar, February 2017), David Ludington, a retired electrical engineer, writes about this. David and I seem to agree that what he calls the “Standard Convention” (or equation 4) is indeed the correct one. Though his article focuses on A\/D converters, the thinking is the same for DACs. <\/strong><\/p>\n<p><strong>According to David, even big sites like AdaFruit, SparkFun and the Arduino site have an equation similar to equation 5 listed. These are all wonderful resources. In no way am I trying to discredit them, but accuracy — especially in technical writing — is important. Depending on what your goal is, using eq. 5 instead of eq. 4 may not be a big deal. However, if you need high precision small errors can create big problems and the source may be hard to find. This can lead not only to faulty projects which don’t always perform as expected, but also frustration and wasted time.<\/strong><\/p>\n<p>Anyway, onward we march\u2026<\/p>\n<p><strong>Settling time<\/strong>: This is the time the output needs to switch and settle to within +\/- \u00bd LSB when the input code switches from all 0s to all 1s. As a general rule of thumb, the settling time should be about half of the data arrival time, maybe less.<\/p>\n<p><strong>Linearity<\/strong>: A linear error is a deviation from the ideal straight-line output of the DAC. A linearity error of more than +\/- \u00bd LSB can result in a nonmonotonic output. An offset error is a special case of a linear error. It is the amount of output voltage when all input bits are zero. A graph of the ideal output of a DAC looks like a straight line starting at the origin and increasing. Offset errors shift that line up or down so it does not start at the origin.<\/p>\n<p><strong>Relative accuracy<\/strong>: This is more frequently used than absolute accuracy (measure of output voltage with respect to its expected value). It measures the deviation of the actual from the ideal output voltage as a fraction of the full-scale voltage. Shoot for an accuracy no worse than +\/- \u00bd LSB when picking a DAC.<\/p>\n<p>You\u2019ve now read over 2000 words, so that\u2019s it for this post on the types of digital to analog converters.<\/p>\n<p>There\u2019s a lot more to say about these devices, so I’m sure we’ll see DACs appear again in a future post. Until next time, comment and tell us what you\u2019re favorite DAC is. Is it a dedicated IC? Does it come as part of the package for a microcontroller or eco board? Let us know.<\/p>\n<h2 style=\"text-align: center;\">Become the Maker you were born to be. Try <a href=\"https:\/\/learnarduinonow.com\">Arduino Academy<\/a> for FREE!<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4238\" src=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png\" alt=\"\" width=\"828\" height=\"315\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png 828w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-300x114.png 300w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-150x57.png 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-768x292.png 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/p>\n<a target=\"_blank\" href=\"https:\/\/www.drpeterscode.com\/index.php\"><img src=\"https:\/\/www.circuitcrush.com\/wp-content\/plugins\/dpabottomofpostpage\/apixel1x1.jpg\" ><\/a><table><\/table>","protected":false},"excerpt":{"rendered":"<p>At first, I was going to write about the analog to digital converter (ADC). I decided to write about the digital to analog converter instead. Why? I started gathering information about ADCs. Then it hit me: digital to analog converters (or DACs) are often part of analog to digital converters. So, the topic this time […]<\/p>\n","protected":false},"author":1,"featured_media":751,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[88],"tags":[90,89],"class_list":{"0":"post-741","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-digital-to-analog-conversion","8":"tag-dacs","9":"tag-digital-to-analog-converters","10":"entry"},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Digital-to-analog-converter.png","_links":{"self":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/comments?post=741"}],"version-history":[{"count":4,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/741\/revisions"}],"predecessor-version":[{"id":4334,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/741\/revisions\/4334"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/media\/751"}],"wp:attachment":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/media?parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/categories?post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/tags?post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}