Digital Potentiometer Control

Control a AD5206 digital potentiometer using the SPI protocol.

In this tutorial you will learn how to control the AD5206 digital potentiometer using Serial Peripheral Interface (SPI). For an explanation of SPI see the SPI Library reference.

Digital potentiometers are useful when you need to vary the resistance in a circuit electronically rather than by hand. Example applications include LED dimming, audio signal conditioning and tone generation. In this example we will use a six channel digital potentiometer to control the brightness of six LEDs. The steps we will cover for implementing SPI communication can be modified for use with most other SPI devices.

Hardware Required

  • Arduino board
  • AD5206 Digital Potentiometer
  • 6 LEDs
  • 6 220 ohm resistors
  • Hook-up wires
  • Breadboard

Introduction to the AD5206 Digital Potentiometer

Click for for the AD5206's datasheet.

Pinout for the AD5206.
Pinout for the AD5206.

Description of the pins.
Description of the pins.

The AD5206 is a 6 channel digital potentiometer. This means it has six variable resistors (potentiometers) built in for individual electronic control. There are three pins on the chip for each of the six internal variable resistors, and they can be interfaced with just as you would use a mechanical potentiometer. The individual variable resistor pins are labeled Ax, Bx and Wx, ie. A1, B1 and W1. For example, in this tutorial we will be using each variable resistor as a voltage divider by pulling one side pin (pin B) high, pulling another side pin (pin A) low and taking the variable voltage output of the center pin (Wiper). In this case, the AD5206 provides a maximum resistance of 10k ohm, delivered in 255 steps (255 being the max, 0 being the least).

Circuit

The circuit for this tutorial (made with Fritzing).
The circuit for this tutorial (made with Fritzing).

Schematic

The schematic for this tutorial.
The schematic for this tutorial.

Code

1/*
2
3 Digital Pot Control
4
5 This example controls an Analog Devices AD5206 digital potentiometer.
6
7 The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
8
9 A - connect this to voltage
10
11 W - this is the pot's wiper, which changes when you set it
12
13 B - connect this to ground.
14
15 The AD5206 is SPI-compatible,and to command it, you send two bytes,
16
17 one with the channel number (0 - 5) and one with the resistance value for the
18
19 channel (0 - 255).
20
21 The circuit:
22
23 * All A pins of AD5206 connected to +5V
24
25 * All B pins of AD5206 connected to ground
26
27 * An LED and a 220-ohm resisor in series connected from each W pin to ground
28
29 * CS - to digital pin 10 (SS pin)
30
31 * SDI - to digital pin 11 (MOSI pin)
32
33 * CLK - to digital pin 13 (SCK pin)
34
35 created 10 Aug 2010
36
37 by Tom Igoe
38
39 Thanks to Heather Dewey-Hagborg for the original tutorial, 2005
40
41*/
42
43// inslude the SPI library:
44#include <SPI.h>
45
46// set pin 10 as the chip select for the digital pot:
47
48const int chipSelectPin = 10;
49
50void setup() {
51
52 // set the chipSelectPin as an output:
53
54 pinMode(chipSelectPin, OUTPUT);
55
56 // initialize SPI:
57
58 SPI.begin();
59}
60
61void loop() {
62
63 // go through the six channels of the digital pot:
64
65 for (int channel = 0; channel < 6; channel++) {
66
67 // change the resistance on this channel from min to max:
68
69 for (int level = 0; level < 255; level++) {
70
71 digitalPotWrite(channel, level);
72
73 delay(10);
74
75 }
76
77 // wait a second at the top:
78
79 delay(100);
80
81 // change the resistance on this channel from max to min:
82
83 for (int level = 0; level < 255; level++) {
84
85 digitalPotWrite(channel, 255 - level);
86
87 delay(10);
88
89 }
90
91 }
92
93}
94
95void digitalPotWrite(int address, int value) {
96
97 // take the SS pin low to select the chip:
98
99 digitalWrite(chipSelectPin, LOW);
100
101 delay(100);
102
103 // send in the address and value via SPI:
104
105 SPI.transfer(address);
106
107 SPI.transfer(value);
108
109 delay(100);
110
111 // take the SS pin high to de-select the chip:
112
113 digitalWrite(chipSelectPin, HIGH);
114}

Original tutorial by Heather Dewey-Hagborg, update by Tom Igoe and Christian Cerrito

Last revision 2018/05/17 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.