root / arduino-1.0 / libraries / SPI / examples / DigitalPotControl / DigitalPotControl.ino @ 58d82c77
History | View | Annotate | Download (1.93 KB)
1 | 58d82c77 | Tom Mullins | /* |
---|---|---|---|
2 | Digital Pot Control |
||
3 | |||
4 | This example controls an Analog Devices AD5206 digital potentiometer. |
||
5 | The AD5206 has 6 potentiometer channels. Each channel's pins are labeled |
||
6 | A - connect this to voltage |
||
7 | W - this is the pot's wiper, which changes when you set it |
||
8 | B - connect this to ground. |
||
9 | |||
10 | The AD5206 is SPI-compatible,and to command it, you send two bytes, |
||
11 | one with the channel number (0 - 5) and one with the resistance value for the |
||
12 | channel (0 - 255). |
||
13 | |||
14 | The circuit: |
||
15 | * All A pins of AD5206 connected to +5V |
||
16 | * All B pins of AD5206 connected to ground |
||
17 | * An LED and a 220-ohm resisor in series connected from each W pin to ground |
||
18 | * CS - to digital pin 10 (SS pin) |
||
19 | * SDI - to digital pin 11 (MOSI pin) |
||
20 | * CLK - to digital pin 13 (SCK pin) |
||
21 | |||
22 | created 10 Aug 2010 |
||
23 | by Tom Igoe |
||
24 | |||
25 | Thanks to Heather Dewey-Hagborg for the original tutorial, 2005 |
||
26 | |||
27 | */ |
||
28 | |||
29 | |||
30 | // inslude the SPI library: |
||
31 | #include <SPI.h> |
||
32 | |||
33 | |||
34 | // set pin 10 as the slave select for the digital pot: |
||
35 | const int slaveSelectPin = 10; |
||
36 | |||
37 | void setup() { |
||
38 | // set the slaveSelectPin as an output: |
||
39 | pinMode (slaveSelectPin, OUTPUT); |
||
40 | // initialize SPI: |
||
41 | SPI.begin(); |
||
42 | } |
||
43 | |||
44 | void loop() { |
||
45 | // go through the six channels of the digital pot: |
||
46 | for (int channel = 0; channel < 6; channel++) { |
||
47 | // change the resistance on this channel from min to max: |
||
48 | for (int level = 0; level < 255; level++) { |
||
49 | digitalPotWrite(channel, level); |
||
50 | delay(10); |
||
51 | } |
||
52 | // wait a second at the top: |
||
53 | delay(100); |
||
54 | // change the resistance on this channel from max to min: |
||
55 | for (int level = 0; level < 255; level++) { |
||
56 | digitalPotWrite(channel, 255 - level); |
||
57 | delay(10); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | } |
||
62 | |||
63 | int digitalPotWrite(int address, int value) { |
||
64 | // take the SS pin low to select the chip: |
||
65 | digitalWrite(slaveSelectPin,LOW); |
||
66 | // send in the address and value via SPI: |
||
67 | SPI.transfer(address); |
||
68 | SPI.transfer(value); |
||
69 | // take the SS pin high to de-select the chip: |
||
70 | digitalWrite(slaveSelectPin,HIGH); |
||
71 | } |