Controlling a 10-bit Digital Potentiometer via SPI with Arduino

1. A brief introduction to the Serial Peripheral Interface (SPI)

The Serial Peripheral Interface a.k.a SPI or four-wire serial interface is a full-duplex serial data protocol used by microcontrollers for communicating with one or more peripheral devices. Devices communicate in master/slave mode and there are four logic signals:

  • - MOSI: Master Out Slave In, is the master line to send data to the peripheral.
  • - MISO: Master In Slave Out, is the slave line to send data to the master.
  • - SCK: Serial Clock, the clock pulses which synchronize data transmission generated by the master.
  • - SS: Slave Select, the pin on each device that the master can use to enable and disable specific devices.

For more on this topic, visit Wikipedia's page on SPI.

2. Wiring the Master - Slave

For this tutorial we will use the Non-volatile memory, 1024 Position (10-bit) Digital Potentiometer AD5131 from Analog Devices. Wiring the IC to Arduino should be pretty straight-forward by looking at the pin configuration from the Data Sheet.

You might encounter different naming conventions for the lines on the SPI interface; the most commonly used are:

  • - The Motorola terms: MOSI and MISO for 'Master Out Slave In' and 'Master In Slave Out'.
  • - The PIC terms: SDI and SDO for 'SPI Data In' and 'SPI Data Out'.

MOSI always connects to MOSI or SDI (slave) and MISO always connects MISO or SDO (slave). CLK or SCK both refer to the serial clock and SS (slave select) or CS (chip select) are the same line.

On the Arduino Uno or Duemilanove the SPI lines are available from the digital pins 11 (MOSI), 12 (MISO) and 13 (SCK). Also, MOSI, MISO and SCK are available in a consistent physical location on the ICSP header.

If you are using a different Arduino board, the SPI lines might be located on different pins numbers. To find this information, please go to the SPI Library reference on the Arduino website.  Please note that when using the SPI feature on the Arduino, the digital pins 11, 12 and 13 can't be used as digital I/O.

Now, let's take a look at our wiring schematic.

Note that, as in this example we are not using all the features from the AD5231.  We left some of the pins unconnected (O1, O2 and RDY). Also, the pins marked with the optional features Write Protect (WP) and Hardware Override (PR) should be connected to VDD if they are not used.

3. Basic Operation

The AD5231 Digital Potentiometer is designed to operate as a true variable resistor replacement device for analog signals. The digital potentiometer wiper position is determined by the RDAC register contents (Address 0x0). The RDAC register acts as a scratchpad register, allowing as many values changes as necessary to place the potentiometer wiper in the correct position.

The basic mode of setting the variable resistor wiper position (programming the scratchpad register) is accomplished by loading the serial data input register with the instruction: 0xB, Address 0x0, and the desired wiper position data.

4. Serial Data Interface

The AD5231 uses a 24-bit serial data-word loaded Most Significant Bit first (MSBFIRST). The CS (chip select) pin must be held LOW until the complete data-word is loaded into the SDI pin. When CS returns to HIGH, the serial data-word is decoded. The command bits (Cx) control the operation of the digital potentiometer. The address bits (Ax) determine which register is activated. The data bits (Dx) are the values that are loaded into the decoded register.

 

MSB | Command Byte 0
Data Byte 1
Data Byte 0 | LSB
C3 C2 C1 C0 Ax Ax Ax Ax
X X X X X X D9 D8
D7 D6 D5 D4 D3 D2 D1 D0

5. Programing the Interface with Arduino

The Arduino SPI library allows us to transfer [SPI.transfer(byte)] only 8-bits (1 byte) at a time. In order to transfer a complete instruction (24-bits) to the digital potentiometer we will have to send the instruction in 3 parts of 8-bits each.

The first byte or 'Command Byte' is composed by the Command + Address which allows us to write data into the RDAC register:

Command = 0xB = 1011
Address = 0x0 = 0000
then:
Command + Address = 10110000

The following 2 bytes, the Data Bytes, contain the 10-bits (from 0 to 1023) wiper position. The Data Byte 1 only contains 2 bits from this value and 6 bits that are not used (X). In order to achieve this operation we will use the bitwise operands Logical Right Shift '>>' and AND '&'.

If you are not familiar with bitwise operations, I suggest reading  the 'Absolute Beginner's Guide to Bit Shifting' post from StackOverflow.

For this example, we will assume that we want to send the value 620 to the digital potentiometer to move the Wiper W to approximately 60%  full-scale position, then:

620 = 0000001001101100
620 >> 8 = 00000010 //Shifting 8 bits to the right
620 & 11111111 = 01101100  //Truncate to 8-bits on the LSB side

And finally, the Arduino code.

#include <SPI.h> 

const int csPin = 10;

void setup() {

 SPI.begin();
 SPI.setBitOrder(MSBFIRST); //We know this from the Data Sheet

 pinMode(csPin,OUTPUT);
 digitalWrite(csPin, HIGH);
}

void loop() {
 for(int i=0; i<1023; i++) { 
  digitalPotWrite(0,i);
  delay(10);
 } 
}

void digitalPotWrite(int address, int value) {
 digitalWrite(csPin, LOW); //select slave
 byte command = 0xB0; //0xB0 = 10110000 
 command += address; 
 SPI.transfer(command); 
 byte byte1 = (value >> 8);
 byte byte0 = (value & 0xFF); //0xFF = B11111111
 SPI.transfer(byte1);
 SPI.transfer(byte0);
 digitalWrite(csPin, HIGH); //de-select slave
}

There are much more features available in the AD5231 such as memory storing and restoring, increment/decrement, ±6 dB/step log taper adjustment, wiper setting read-back and extra EEMEM  for user-defined information, that are not covered in this post.

7 Comments

  1. Pingback: Book of Genesis… The IT Version | Unwritten Thoughts

  2. Sasha | April 13th, 2013

    Hi. I have been wanting to follow this tutorial. But I have no idea what the final product looks like. Can you maybe upload a demo?

  3. Shintaro | June 29th, 2013

    Hello,

    would it be a lot different for the MAX5483.
    http://www.maximintegrated.com/datasheet/index.mvp/id/4730

    I have difficulties in getting the code work and I dont know it is is the code on arduino or if it is my bad soldering skills..

  4. Juan-Manuel Fluxà Juan-Manuel Fluxà | June 29th, 2013

    Hi @Shintaro,
    For the MAX5483 it would be a bit different. By looking at the Datasheet, first thing is you have to select the interface mode you want to use. For selecting the SPI interface you have to pull the SPI/UD pin (pin 6 on TSSOP package) UP. So, just wire up a digital output from the arduino to this pin and make sure is HIGH before trying to write into the interface.
    Second thing, take a look at the “Table 2. Command Decoding” (Datasheet page 14). The writing command is B00000000 and your Data bytes are MSB to the left, so the code would look something like this (not tested):
    void digitalPotWrite(int address, int value) {
    digitalWrite(csPin, LOW); //select slave
    byte command = 0x0;
    SPI.transfer(command);
    byte byte1 = (value >> int(value/255));
    byte byte0 = ((value & B11) << 6);
    SPI.transfer(byte1);
    SPI.transfer(byte0);
    digitalWrite(csPin, HIGH); //de-select slave
    }
    Good luck.

  5. Shintaro | June 29th, 2013

    Thanks! It seems to work
    So I put
    SPI.setBitOrder(MSBFIRST);
    right?

  6. Shintaro | June 29th, 2013

    Something different:
    The value sequence does not seem to “loop”:

    void loop() {
    digitalPotWrite(0,23);
    delay(1000);
    digitalPotWrite(0,33);
    delay(1000);
    }

    I think it is because I still dont understand the address part of “void digitalPotWrite(int address, int value)”

  7. Andy | July 6th, 2013

    I’ve tried using your code for a 10-bit DAC (MAX5250). The
    data sheet says it takes 16 bits of data so I did the following
    code just to see if I could get an output of near 3.3V since I have
    3.3V as my refAB voltage. I’ve got pin 3 of the chip hooked up to a
    multimeter though, and the voltage is jumping all over the place.
    Could you look at my code and the datasheet and maybe give some
    insight as to what I’m doing wrong? Do I need to change the
    setdatamode or setclockdivider? #include const int csPin = 10; void
    setup() { SPI.begin(); SPI.setBitOrder(MSBFIRST); //We know this
    from the Data Sheet pinMode(csPin,OUTPUT); digitalWrite(csPin,
    HIGH); } void loop() { digitalWrite(csPin, LOW); //select slave
    SPI.transfer(B00111111); SPI.transfer(B11111100);
    digitalWrite(csPin, HIGH); //de-select slave delay(100); }
    http://datasheets.maximintegrated.com/en/ds/MAX5250.pdf

Post a Comment