{"id":3064,"date":"2014-11-02T10:27:59","date_gmt":"2014-11-02T15:27:59","guid":{"rendered":"https:\/\/itp.nyu.edu\/physcomp\/?page_id=3064"},"modified":"2022-11-06T18:08:18","modified_gmt":"2022-11-06T23:08:18","slug":"lab-spi-communication-aith-a-digital-potentiometer","status":"publish","type":"page","link":"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-serial-communication\/lab-spi-communication-aith-a-digital-potentiometer\/","title":{"rendered":"Lab: SPI Communication With A Digital Potentiometer"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this lab, you&#8217;ll see synchronous serial communication in action using the Serial Peripheral Interface (SPI) protocol. You&#8217;ll communicate with a digital potentiometer chip from a microcontroller. You&#8217;ll use the digital pot to control the loudness of a speaker playing a tone from the microcontroller.<\/p>\n\n\n\n<p>Related videos: <a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380370070\" target=\"_blank\">Intro to Synchronous Serial<\/a>, <a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380370524\" target=\"_blank\">SPI<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Youll_Need_to_Know\"><\/span>What You\u2019ll Need to Know<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To get the most out of this Lab, you should be familiar with the basics of programming an Arduino microcontroller. If you\u2019re not, review the <a rel=\"noopener noreferrer\" href=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/digital-input-and-output-with-an-arduino\/\" target=\"_blank\">Digital Input and Output Lab<\/a>, and perhaps the <a rel=\"noopener noreferrer\" href=\"http:\/\/arduino.cc\/en\/Guide\/HomePage\" target=\"_blank\">Getting Started with Arduino guide<\/a>. You should also understand <a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/serial-communication__trashed\/serial-communication-the-basics\/\" target=\"_blank\" rel=\"noreferrer noopener\">asynchronous serial communication<\/a> and how it differs from <a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/serial-communication__trashed\/synchronous-serial-communication-the-basics\/\" target=\"_blank\" rel=\"noreferrer noopener\">synchronous serial communication<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Things_Youll_Need\"><\/span>Things You\u2019ll Need<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>From Figure 1-5 are the parts that you need for this lab.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/hookup_wire2.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/hookup_wire2-150x150.jpg\" alt=\"Three 22AWG solid core hookup wires. Each is about 6cm long. The top one is black; the middle one is red; the bottom one is blue. All three have stripped ends, approximately 4 to 5mm on each end.\" class=\"wp-image-1203\"\/><\/a><figcaption>Figure 1. 22AWG solid core hookup<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image size-thumbnail wp-image-5921\"><figure class=\"alignleft\"><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/pcomp-kit-f2019-arduino-nano-33-iot-150x150.jpg\" alt=\"Photo of an Arduino Nano 33 IoT module. The USB connector is at the top of the image, and the physical pins are numbered in a U-shape from top left to bottom left, then from bottom right to top right.\" class=\"wp-image-5921\"\/><figcaption>Figure 2. Arduino Nano 33 IoT<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image wp-image-3098 size-thumbnail\"><figure class=\"alignleft\"><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/AD5206_img-150x150.png\" alt=\"This component is a small rectangle with several pins on each of the two long sides. There is a small round depression on the upper left corner of the rectangle, and a semicircular dip in the middle of the top.\" class=\"wp-image-3098\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/AD5206_img-150x150.png 150w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/AD5206_img-300x300.png 300w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/AD5206_img.png 643w\" sizes=\"(max-width: 150px) 85vw, 150px\" \/><figcaption>Figure 3. Digital Potentiometer<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/speaker.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/speaker-150x150.jpg\" alt=\"An 8 ohm speaker with 2 wires solder to the speakers leads\" class=\"wp-image-2501\"\/><\/a><figcaption>Figure 4. An 8 ohm speaker<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistors1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistors1-150x150.jpg\" alt=\"Resistors. Shown here are 220-ohm resistors. You can tell this because they have two red and one brown band, followed by a gold band.\" class=\"wp-image-798\"\/><\/a><figcaption>Figure 5. Resistors. Shown here are 220-ohm resistors. You can tell this because they have two red and one brown band, followed by a gold band.<\/figcaption><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Controlling_the_Loudness_of_a_Speaker\"><\/span>Controlling the Loudness of a Speaker<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you did the <a rel=\"noreferrer noopener\" href=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-arduino-digital-and-analog\/tone-output-using-an-arduino\/\" target=\"_blank\">tone lab<\/a> on this site, you know that it&#8217;s not possible to vary the loudness of a tone generated from a microcontroller. In order to change the loudness, you&#8217;d need to change the voltage of the tone pin. If you put a potentiometer in series with the speaker as shown below, you can modify the tone&#8217;s loudness, as is shown in Figure 6.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"291\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/tone_output_loudness_schem.png\" alt=\"Electrical diagram of speaker. The speaker's negative pin is connected to ground and the positive pin is connected to a 10k potentiometer\" class=\"wp-image-3071\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/tone_output_loudness_schem.png 479w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/tone_output_loudness_schem-300x182.png 300w\" sizes=\"(max-width: 479px) 85vw, 479px\" \/><figcaption><em>Figure 6. Electrical diagram of speaker. The speaker&#8217;s negative pin is connected to ground and the positive pin is connected to a 10k potentiometer<\/em><\/figcaption><\/figure>\n\n\n\n<p>This circuit requires that you modify the loudness manually, by turning the pot. But with a digital potentiometer, you can modify the loudness from your program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Connect_the_digital_potentiometer\"><\/span>Connect the&nbsp;digital potentiometer<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The digital potentiometer used in this lab, &nbsp;an <a rel=\"noopener noreferrer\" href=\"http:\/\/www.analog.com\/static\/imported-files\/data_sheets\/AD5204_5206.pdf\" target=\"_blank\">Analog Devices AD5206<\/a>, is an integrated circuit (IC) that can perform one specific function: it has six potentiometers that you can control, as is shown in Figure 7. Each of its pins has a specific function, as shown below. It&#8217;s in a Dual Inline Package, or DIP. DIP package ICs typically have their pins enumerated in a U-shape, starting with pin 1 on the top left, and the highest number pin on the top right.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/digital_pot_schem.png\"><img loading=\"lazy\" decoding=\"async\" width=\"614\" height=\"445\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/digital_pot_schem.png\" alt=\"AD5206 Digital Potentiometer. The pins on the left side of the chip are pins for potentiometer 6, followed by pins for power and SPI connections, followed by pins for potentiometer 5. The potentiometer pins are labeled A, W, and B. The power and SPI connection pins are labeled Ground, CS, Vdd, SDI, CLK, and Vss. The pins on the right side of the chip are for potentiometers 4, 2, 1, and 3. Each of the potentiometer pins are labeled A, W, and B.\" class=\"wp-image-3070\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/digital_pot_schem.png 614w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/digital_pot_schem-300x217.png 300w\" sizes=\"(max-width: 614px) 85vw, 614px\" \/><\/a><figcaption>Figure 7. AD5206 Digital Potentiometer. The pins on the left side of the chip are pins for potentiometer 6, followed by pins for power and SPI connections, followed by pins for potentiometer 5. The potentiometer pins are labeled A, W, and B. The power and SPI connection pins are labeled Ground, CS, Vdd, SDI, CLK, and Vss. The pins on the right side of the chip are for potentiometers 4, 2, 1, and 3. Each of the potentiometer pins are labeled A, W, and B.<\/figcaption><\/figure><\/div>\n\n\n\n<p>The potentiometers in this chip are labeled A1, B1, W1 through A6, B6, W6. The A and B pins are the &nbsp;fixed end&nbsp;pins of the potentiometer, and if you measure resistance across them with a multimeter, you&#8217;ll measure 10 kilohms. The W pins are the wipers, and the resistance is programmable via the SPI connection. You can use any of these potentiometers in a circuit just like you would a regular potentiometer.<\/p>\n\n\n\n<p>First, connect the digital potentiometer&#8217;s power and ground connections, and the connections for clock, chip select, and serial data in, as shown in Figure 8-10:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_schem_no_spkr.png\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"737\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_schem_no_spkr.png\" alt=\"Schematic of an Arduino attached to a AD5206 Potentiometer. The Arduino's ground is attached the the potentiometer's A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino's D10, D11, and D13 pins are attached to the potentiometer's CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer's Vdd pin, number 6, is connected to 5 volts.\" class=\"wp-image-3072\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_schem_no_spkr.png 865w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_schem_no_spkr-300x255.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption>Figure 8. Schematic of an Arduino attached to a AD5206 Potentiometer. The Arduino&#8217;s ground is attached the the potentiometer&#8217;s A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino&#8217;s D10, D11, and D13 pins are attached to the potentiometer&#8217;s CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer&#8217;s Vdd pin, number 6, is connected to 5 volts. If using the Arduino Nano the potentiometer&#8217;s Vdd pin, number 6, should connect to 3.3 volts.<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_bb_no_spkr.png\"><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"535\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_bb_no_spkr.png\" alt=\"Schematic of an Arduino attached to a AD5206 Potentiometer. The Arduino's ground is attached the the potentiometer's A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino's D10, D11, and D13 pins are attached to the potentiometer's CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer's Vdd pin, number 6, is connected to 5 volts.\" class=\"wp-image-3073\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_bb_no_spkr.png 820w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_bb_no_spkr-300x195.png 300w\" sizes=\"(max-width: 820px) 85vw, 820px\" \/><\/a><figcaption>Figure 9. Schematic of an Arduino attached to a AD5206 Potentiometer. The Arduino&#8217;s ground is attached the the potentiometer&#8217;s A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino&#8217;s D10, D11, and D13 pins are attached to the potentiometer&#8217;s CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer&#8217;s Vdd pin, number 6, is connected to 5 volts.<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image wp-image-6191\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/spiDigitalPotLabTemplate_bb2-e1565975304202.png\" alt=\"Figure 10. Schematic of an Arduino attached to a AD5206 Potentiometer. The Arduino's ground is attached the the potentiometer's A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino's D10, D11, and D13 pins are attached to the potentiometer's CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer's Vdd pin, number 6, is connected to 3.3 volts.\" class=\"wp-image-6191\" width=\"284\" height=\"893\"\/><figcaption>Figure 10. Schematic of an Arduino attached to a AD5206 Potentiometer.<\/figcaption><\/figure><\/div>\n\n\n\n<p>Figure 10. Schematic of an Arduino attached to a AD5206 Potentiometer. The Arduino&#8217;s ground is attached the the potentiometer&#8217;s A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino&#8217;s D10, D11, and D13 pins are attached to the potentiometer&#8217;s CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer&#8217;s Vdd pin, number 6, is connected to 3.3 volts.<\/p>\n\n\n\n<p>Next, add a speaker to&nbsp;the wiper of the fifth channel of the digital pot. Include a 100-ohm resistor in series with the speaker. Then connect the A5 pin to digital pin 9 of the Arduino, which you&#8217;ll use to generate a tone. You can use any channel of the digital pot if you want, but the code below uses the fifth channel.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_schem.png\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"737\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_schem.png\" alt=\"Schematic of an Arduino attached to a AD5206 Potentiometer and a speaker. The Arduino's ground is attached the the potentiometer's A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino's D10, D11, and D13 pins are attached to the potentiometer's CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer's Vdd pin, number 6, is connected to 5 volts. The Arduino's D9 pin is connected to the negative terminal of a speaker. The speaker's positive terminal is connected to a 100 Ohm resistor, which is connected to the potentiometer's W5 pin, number 11.\" class=\"wp-image-3075\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_schem.png 865w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_schem-300x255.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption>Figure 11. Schematic of an Arduino attached to a AD5206 Potentiometer and a speaker. The Arduino&#8217;s ground is attached the the potentiometer&#8217;s A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino&#8217;s D10, D11, and D13 pins are attached to the potentiometer&#8217;s CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer&#8217;s Vdd pin, number 6, is connected to 5 volts. If using the Arduino Nano, the potentiometer&#8217;s Vdd pin, number 6, should connect to 3.3 volts. The Arduino&#8217;s D9 pin is connected to the negative terminal of a speaker. The speaker&#8217;s positive terminal is connected to a 100 Ohm resistor, which is connected to the potentiometer&#8217;s W5 pin, number 11.<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_bb.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1011\" height=\"535\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_bb.png\" alt=\"Schematic of an Arduino attached to a AD5206 Potentiometer and a speaker. The Arduino's ground is attached the the potentiometer's A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino's D10, D11, and D13 pins are attached to the potentiometer's CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer's Vdd pin, number 6, is connected to 5 volts. The Arduino's D9 pin is connected to the negative terminal of a speaker. The speaker's positive terminal is connected to a 100 Ohm resistor, which is connected to the potentiometer's W5 pin, number 11.\" class=\"wp-image-3076\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_bb.png 1011w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabSPI_bb-300x158.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption>Figure 12. Schematic of an Arduino attached to a AD5206 Potentiometer and a speaker. The Arduino&#8217;s ground is attached the the potentiometer&#8217;s A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino&#8217;s D10, D11, and D13 pins are attached to the potentiometer&#8217;s CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer&#8217;s Vdd pin, number 6, is connected to 5 volts. The Arduino&#8217;s D9 pin is connected to the negative terminal of a speaker. The speaker&#8217;s positive terminal is connected to a 100 Ohm resistor, which is connected to the potentiometer&#8217;s W5 pin, number 11.<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image wp-image-6192\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/spiDigitalPotLab_bb-e1565975655130.png\" alt=\"Figure Schematic of an Arduino attached to a AD5206 Potentiometer and a speaker. The Arduino's ground is attached the the potentiometer's A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino's D10, D11, and D13 pins are attached to the potentiometer's CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer's Vdd pin, number 6, is connected to 3.3 volts. The Arduino's D9 pin is connected to the negative terminal of a speaker. The speaker's positive terminal is connected to a 100 Ohm resistor, which is connected to the potentiometer's W5 pin, number 11.\" class=\"wp-image-6192\" width=\"449\" height=\"890\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/spiDigitalPotLab_bb-e1565975655130.png 898w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/spiDigitalPotLab_bb-e1565975655130-768x1522.png 768w\" sizes=\"(max-width: 449px) 85vw, 449px\" \/><figcaption>Figure 13. Figure Schematic of an Arduino attached to a AD5206 Potentiometer and a speaker.&nbsp;<\/figcaption><\/figure><\/div>\n\n\n\n<p>Figure 13. Schematic of an Arduino attached to a AD5206 Potentiometer and a speaker. The Arduino&#8217;s ground is attached the the potentiometer&#8217;s A5, Vss, and Ground pins, numbered 12, 9, and 4, respectively. The Arduino&#8217;s D10, D11, and D13 pins are attached to the potentiometer&#8217;s CS, SDI, and CLK pins, which are numbered 5, 7, and 8, respectively. The potentiometer&#8217;s Vdd pin, number 6, is connected to 3.3 volts. The Arduino&#8217;s D9 pin is connected to the negative terminal of a speaker. The speaker&#8217;s positive terminal is connected to a 100 Ohm resistor, which is connected to the potentiometer&#8217;s W5 pin, number 11.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Program_the_Microcontroller\"><\/span>Program the Microcontroller<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The AD5206 has a simple command protocol, detailed in the &#8220;Operation&#8221; section of the <a rel=\"noopener noreferrer\" href=\"http:\/\/www.analog.com\/static\/imported-files\/data_sheets\/AD5204_5206.pdf\" target=\"_blank\">data sheet<\/a>. You send two bytes: the channel number you wish to control, and the level for the channel. Setting a channel to 0 makes the resistance between the wiper and the B pin 0 ohms, and setting it to 255 makes the resistance between the wiper and B 10 kilohms. There&#8217;s one slightly confusing issue: the channels are addressed as numbers 0 through 5 in code, yet numbered 1 through 6 on the pin diagram. Just remember that the first channel is channel 0, the second channel is channel 1, and so forth (just like array elements in most programming languages).<\/p>\n\n\n\n<p>You can find Arduino-compatible libraries for many SPI devices that use the SPI library, but never expose it directly in the API. Instead, these libraries will include device-specific commands that use the SPI library to transfer data. Because the AD5206&#8217;s protocol is so simple, however, you don&#8217;t need a device-specific library, you can just use the SPI library. It&#8217;s a good illustration of what&#8217;s going on inside of some of the more complex SPI libraries. <\/p>\n\n\n\n<p>At the beginning of your code, include the SPI library and set up a pin number for the chip select pin. The other SPI pins are set by the microcontroller you&#8217;re using (see the <a rel=\"noopener noreferrer\" href=\"http:\/\/arduino.cc\/en\/Reference\/SPI\" target=\"_blank\">Arduino SPI reference page<\/a> for the pin numbers). Then in the setup function, call <tt>SPI.begin()<\/tt> to initialize communications:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n\/\/ include the SPI library:\n#include \"SPI.h\"\nconst int CSPin = 10;   \/\/ chip select pin number\n\nvoid setup() {\n  \/\/ initialize SPI:\n  SPI.begin();\n  \/\/ set the mode of CSPin\n  pinMode(CSPin, OUTPUT);\n}\n\n<\/pre><\/div>\n\n\n<p>In your main loop, first make a tone on pin 9. &nbsp;Then make two <code>for()<\/code> loops to fade the loudness up and down. The actual work of controlling the digital potentiometer will be handled by a function you&#8217;ll write called <code>digitalPotWrite()<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nvoid loop() {\ntone(9, 440); \/\/ play a tone on pin 9\n\n\/\/ fade the loudness up:\nfor (int loudness = 100; loudness &lt;= 255; loudness++) {\ndigitalPotWrite(4, loudness);\ndelay(20);\n}\ndelay(1000); \/\/ delay 1 second\n\n\/\/ fade the loudness down:\nfor (int loudness = 255; loudness &gt;= 100; loudness--) {\ndigitalPotWrite(4, loudness);\ndelay(20);\n}\n\ndelay(1000); \/\/ delay 1 second\n}\n<\/pre><\/div>\n\n\n<p>Finally, write the <tt>digitalPotWrite()<\/tt> function to control the digital potentiometer via SPI. Because SPI data can go both directions at the same time, i.e. from controller to peripheral and from peripheral to controller, there is a single command, <tt>SPI.transfer()<\/tt>, to transfer data instead of the <tt>read()<\/tt> and <tt>write()<\/tt> commands you&#8217;re used to from asynchronous serial communication. In order to put the digital pot in listening mode, you take its chip select pin low, then when you&#8217;re finished communicating with it, you take the pin high again:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nvoid digitalPotWrite(int address, int value) {\n  \/\/ take the SS pin low to select the chip:\n  digitalWrite(CSPin, LOW);\n  \/\/ send in the address and value via SPI: \n  SPI.transfer(address);\n  SPI.transfer(value);\n  \/\/ take the SS pin high to de-select the chip:\n  digitalWrite(CSPin, HIGH);\n}\n<\/pre><\/div>\n\n\n<p>Once you upload this code, you&#8217;re done! &nbsp;You should hear the tone (middle A, 440Hz) fading in and out. You may have noticed that the <code>for()<\/code> loops fade from 100 to 255. The digital pot is changing the resistance across the wiper on the fifth potentiometer (aka channel 4) from approximately 3921 ohms (100\/255 * 10k) to 10 kilohms.&nbsp;These are the levels that were determined from experiment to be the edges of audibility. But if your hearing is better, you may try adjusting the lower limit to see if you can hear it at a lower level.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This is a very simple use of SPI. Data only goes from controller peripheral in this example, and only two bytes are transferred. However, it gives you an indication of how the process works. Each SPI-based device will have its own command protocol, and data will be transferred from controller to peripheral (and vice versa) using the SPI transfer command. &nbsp;To open&nbsp;communications with a given SPI device, you take its chip select pin low, and to close communications, you take the chip select pin low again. This same procedure will work on all SPI devices once you know the command protocol. &nbsp;Keep in mind that most device-specific libraries will handle the SPI communication for you without you having to see it, just like the <tt>digitalPotWrite()<\/tt> command does in this example.<\/p>\n\n\n\n<p>For another example of SPI in action with the AD5206, see the <a rel=\"noopener noreferrer\" href=\"http:\/\/arduino.cc\/en\/Tutorial\/SPIDigitalPot\" target=\"_blank\">digital pot&nbsp;example<\/a> on the Arduino site. There&#8217;s also an example with a <a rel=\"noopener noreferrer\" href=\"http:\/\/arduino.cc\/en\/Tutorial\/BarometricPressureSensor\" target=\"_blank\">barometric pressure sensor<\/a>. The <a rel=\"noopener noreferrer\" href=\"http:\/\/arduino.cc\/en\/Reference\/SD\" target=\"_blank\">SD card library<\/a>, which allows you to communicate with SD cards, uses the SPI library as well, and is used in the <a href=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-serial-communication\/lab-data-logging-with-an-sd-card-reader-using-spi-communication\/\">Data Logging with an SD card<\/a> lab.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lab, you\u2019ll see synchronous serial communication in action using the Serial Peripheral Interface (SPI) protocol. You\u2019ll communicate with a digital potentiometer chip from a microcontroller. <\/p>\n","protected":false},"author":2,"featured_media":0,"parent":1979,"menu_order":301,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[15,9,40],"tags":[],"class_list":["post-3064","page","type-page","status-publish","hentry","category-lab","category-serial-communication","category-spi"],"_links":{"self":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/3064"}],"collection":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/comments?post=3064"}],"version-history":[{"count":44,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/3064\/revisions"}],"predecessor-version":[{"id":10723,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/3064\/revisions\/10723"}],"up":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/1979"}],"wp:attachment":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/media?parent=3064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/categories?post=3064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/tags?post=3064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}