{"id":4429,"date":"2016-02-29T15:05:25","date_gmt":"2016-02-29T07:05:25","guid":{"rendered":"http:\/\/www.rocketscream.com\/blog\/?post_type=portfolio&#038;p=4429"},"modified":"2016-02-29T16:40:52","modified_gmt":"2016-02-29T08:40:52","slug":"using-digital-analog-converter-dac-on-mini-ultra-pro","status":"publish","type":"portfolio","link":"https:\/\/www.rocketscream.com\/blog\/docs-item\/using-digital-analog-converter-dac-on-mini-ultra-pro\/","title":{"rendered":"Using Digital Analog Converter (DAC) on Mini Ultra Pro"},"content":{"rendered":"<div  class='tabcontainer av-av_tab_container-cdfb33d025ccce82e3bec7780d128c1e sidebar_tab sidebar_tab_left  avia-builder-el-0  avia-builder-el-no-sibling  border_tabs'>\n<section class='av_tab_section av_tab_section av-av_tab-21127c225c921997827e1c80d5a962e2'  itemscope=\"itemscope\" itemtype=\"https:\/\/schema.org\/CreativeWork\" ><div id='tab-id-1-tab' class='tab active_tab' role='tab' aria-selected=\"true\" tabindex=\"0\" data-fake-id='#tab-id-1' aria-controls='tab-id-1-content'  itemprop=\"headline\" ><span class='tab_icon avia-iconfont avia-font-entypo-fontello' data-av_icon='\ue8a5' data-av_iconfont='entypo-fontello' ><\/span>Overview<\/div><div id='tab-id-1-content' class='tab_content active_tab_content' role='tabpanel' aria-labelledby='tab-id-1-tab' aria-hidden=\"false\"><div class='tab_inner_content invers-color'  itemprop=\"text\" ><p>The ATSAMD21G18A-AU microcontroller on the Mini Ultra Pro comes with a built-in Digital Analog Converter (DAC) that allow it to provide analog output. Usually, when such peripheral is not available on a microcontroller, it is very common to use a resistor-capacitor filter network on a Pulse Width Modulation (PWM) output to obtain the desired analog voltage.<\/p>\n<p>The analog voltage output is provided by DAC0 located on pin A0. Both 8-bit and 10-bit resolution is available for the analog output voltage. At 10-bit resolution setting, you would be able to output 0 to 3.3 V voltage in 1024 steps (about 3.22 mV of resolution). We found this feature to be very useful when you need to drive a sensor with a voltage reference other than your microcontroller operating voltage.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4435\" src=\"http:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProDAC0.jpg\" alt=\"Mini Ultra Pro DAC0\" width=\"600\" height=\"600\" srcset=\"https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProDAC0.jpg 600w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProDAC0-100x100.jpg 100w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProDAC0-80x80.jpg 80w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProDAC0-300x300.jpg 300w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProDAC0-36x36.jpg 36w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProDAC0-180x180.jpg 180w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<\/div><\/div><\/section>\n<section class='av_tab_section av_tab_section av-av_tab-661d3d52ba97baa606861794a9cfa0b4'  itemscope=\"itemscope\" itemtype=\"https:\/\/schema.org\/CreativeWork\" ><div id='tab-id-2-tab' class='tab' role='tab' aria-selected=\"false\" tabindex=\"0\" data-fake-id='#tab-id-2' aria-controls='tab-id-2-content'  itemprop=\"headline\" ><span class='tab_icon avia-iconfont avia-font-entypo-fontello' data-av_icon='\ue856' data-av_iconfont='entypo-fontello' ><\/span>Generating Analog Voltage<\/div><div id='tab-id-2-content' class='tab_content' role='tabpanel' aria-labelledby='tab-id-2-tab' aria-hidden=\"true\"><div class='tab_inner_content invers-color'  itemprop=\"text\" ><p>To demonstrate the functionality of the DAC peripheral on the Mini Ultra Pro, we will be providing a sinusoidal output voltage on pin A0 using the DAC peripheral. Use the following code and upload it onto the Mini Ultra Pro.<\/p>\n<pre>\/\/ ***** CONSTANTS *****\r\n\/\/ Angle increment to sweep from -pi to +pi\r\nconst float angleDelta = 0.05;\r\n\/\/ Midpoint of DAC value (10-bit resolution span)\r\nconst float midPoint = 1023 \/ 2;\r\nconst float pi = 3.142;\r\n\r\n\/\/ ***** PIN DEFINITION *****\r\n#define PIN_DAC0 A0\r\n\r\n\/\/ ***** VARIABLES *****\r\n\/\/ Argument of sin() function\r\nfloat angle = 0;\r\n\/\/ DAC value in analogWrite() function\r\nint dacValue = 0;\r\n\r\nvoid setup()\r\n{\r\n  \/\/ Change analog output resolution to 10-bit from default 8-bit\r\n  analogWriteResolution(10);\r\n}\r\n\r\nvoid loop()\r\n{\r\n  \/\/ The sin() function returns a value between -1 &amp; 1\r\n  \/\/ Map -1=0; 0=1023\/2; &amp; 1:1023\r\n  dacValue = (int)(midPoint + (midPoint * sin(angle)));\r\n\r\n  \/\/ Increment the angle\r\n  angle += angleDelta;\r\n\r\n  \/\/ Wrap around the angle between -pi &amp; +pi\r\n  if (angle &gt; pi)\r\n  {\r\n    angle = -pi;\r\n  }\r\n\r\n  \/\/ Generate a voltage between 0 and 3.3V.\r\n  analogWrite(PIN_DAC0, dacValue);\r\n}<\/pre>\n<p>Use an oscilloscope to probe the signal on pin A0 (DAC0).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4433\" src=\"http:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProOscilloscopeOnDAC0.jpg\" alt=\"Oscilloscope on DAC0\" width=\"600\" height=\"600\" srcset=\"https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProOscilloscopeOnDAC0.jpg 600w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProOscilloscopeOnDAC0-100x100.jpg 100w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProOscilloscopeOnDAC0-80x80.jpg 80w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProOscilloscopeOnDAC0-300x300.jpg 300w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProOscilloscopeOnDAC0-36x36.jpg 36w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProOscilloscopeOnDAC0-180x180.jpg 180w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>You should be able to get a similar waveform as below. Take note that the output swing between 0 and 3.3 V.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4431\" src=\"http:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SinusoidalOutputDAC.jpg\" alt=\"Sinusoidal Output DAC\" width=\"800\" height=\"480\" srcset=\"https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SinusoidalOutputDAC.jpg 800w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SinusoidalOutputDAC-600x360.jpg 600w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SinusoidalOutputDAC-300x180.jpg 300w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SinusoidalOutputDAC-768x461.jpg 768w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SinusoidalOutputDAC-705x423.jpg 705w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>However, if you do not have an access to an oscilloscope, we would be able to plot the DAC0 output voltage on your Arduino IDE. Starting from v1.6.6 of the Arduino IDE, you can visualize your data in real time graphs using the Serial Plotter feature on the IDE. In order to use the Serial Plotter, remove the oscilloscope probe and replace them with a jumper wire shorting pin A0 and pin A1. We will use the ADC on\u00a0pin A1 to read the output voltage of pin A0 (DAC0).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4434\" src=\"http:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProA0ShortedToA1.jpg\" alt=\"Pin A0 Shorted to Pin A1\" width=\"600\" height=\"600\" srcset=\"https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProA0ShortedToA1.jpg 600w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProA0ShortedToA1-100x100.jpg 100w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProA0ShortedToA1-80x80.jpg 80w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProA0ShortedToA1-300x300.jpg 300w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProA0ShortedToA1-36x36.jpg 36w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/MiniUltraProA0ShortedToA1-180x180.jpg 180w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>On top of that, we need to modify our code a little bit.<\/p>\n<pre>\/\/ ***** CONSTANTS *****\r\n\/\/ Angle increment to sweep from -pi to +pi\r\nconst float angleDelta = 0.05;\r\n\/\/ Midpoint of DAC value (10-bit resolution span)\r\nconst float midPoint = 1023 \/ 2;\r\nconst float pi = 3.142;\r\n\r\n\/\/ ***** PIN DEFINITION *****\r\n#define PIN_DAC0 A0\r\n#define PIN_ADC A1\r\n\r\n\/\/ ***** VARIABLES *****\r\n\/\/ Argument of sin() function\r\nfloat angle = 0;\r\n\/\/ DAC value in analogWrite() function\r\nint dacValue = 0;\r\n\/\/ Voltage output of DAC0 in V\r\nfloat outputVoltage = 0;\r\n\r\nvoid setup()\r\n{\r\n  \/\/ Change analog input resolution to 12-bit from default 10-bit\r\n  analogReadResolution(12);\r\n  \/\/ Change analog output resolution to 10-bit from default 8-bit\r\n  analogWriteResolution(10);\r\n  \/\/ Wait until serial USB connection is ready\r\n  while(!SerialUSB);\r\n  \/\/ Set the baud rate\r\n  SerialUSB.begin(115200);\r\n}\r\n\r\nvoid loop()\r\n{\r\n  \/\/ The sin() function returns a value between -1 &amp; 1\r\n  \/\/ Map -1=0; 0=1023\/2; &amp; 1:1023\r\n  dacValue = (int)(midPoint + (midPoint * sin(angle)));\r\n\r\n  \/\/ Increment the angle\r\n  angle += angleDelta;\r\n\r\n  \/\/ Wrap around the angle between -pi &amp; +pi\r\n  if (angle &gt; pi)\r\n  {\r\n    angle = -pi;\r\n  }\r\n\r\n  \/\/ Generate a voltage between 0 and 3.3V.\r\n  analogWrite(PIN_DAC0, dacValue);\r\n  \/\/ Read DAC0 output and convert to 0 to 3.3 V\r\n  outputVoltage = (analogRead(PIN_ADC) * 3.3) \/ 4096;\r\n  SerialUSB.println(outputVoltage);\r\n}<\/pre>\n<p>Upload the updated code to your Mini Ultra Pro and run the Serial Plotter (Tools &gt;&gt; Serial Plotter). You should be able to see your sinusoidal voltage output from your DAC0 of the Mini Ultra Pro!<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-4432 size-full\" src=\"http:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SerialPlotter.jpg\" alt=\"Serial Plotter\" width=\"1057\" height=\"576\" srcset=\"https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SerialPlotter.jpg 1057w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SerialPlotter-600x327.jpg 600w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SerialPlotter-300x163.jpg 300w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SerialPlotter-768x419.jpg 768w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SerialPlotter-1030x561.jpg 1030w, https:\/\/www.rocketscream.com\/blog\/wp-content\/uploads\/2016\/02\/SerialPlotter-705x384.jpg 705w\" sizes=\"auto, (max-width: 1057px) 100vw, 1057px\" \/><\/p>\n<\/div><\/div><\/section>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":4449,"comment_status":"open","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"tags":[],"portfolio_entries":[183,155],"class_list":["post-4429","portfolio","type-portfolio","status-publish","has-post-thumbnail","hentry","portfolio_entries-mini-ultra-pro","portfolio_entries-products"],"_links":{"self":[{"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/portfolio\/4429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/portfolio"}],"about":[{"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/types\/portfolio"}],"author":[{"embeddable":true,"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/comments?post=4429"}],"version-history":[{"count":1,"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/portfolio\/4429\/revisions"}],"predecessor-version":[{"id":4430,"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/portfolio\/4429\/revisions\/4430"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/media\/4449"}],"wp:attachment":[{"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/media?parent=4429"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/tags?post=4429"},{"taxonomy":"portfolio_entries","embeddable":true,"href":"https:\/\/www.rocketscream.com\/blog\/wp-json\/wp\/v2\/portfolio_entries?post=4429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}