SPI help getting digital pot to work

I seem to be having the hardest time getting a digital pot to work. I'm using an Analog AD5235. Could someone help me adapt this tutorial: http://www.arduino.cc/en/Tutorial/SPIDigitalPot

For this pot: http://www.analog.com/static/imported-files/data_sheets/AD5235.pdf

There seem to be clear differences between the two. For example, the AD5206 in the tutorial requires an 11-bit serial data-word of three address bits followed by eight data bits. The AD5235 I'm using requires a 24-bit serial data-word of four command bits, four address bits and 16 data bits.

I can see the difference but cannot for the life of me get it to work properly, with or without the SPI library. The closest I've gotten is for the LED to blink erratically and very random. I can't quite seem to adapt it properly to work with this pot. I'm not totally sure how to send it a 24-bit data-word compared to what is being sent in the tutorial.

Any help is VERY appreciated!!

Do note that there are some extra signals in your chip... pins !WP and !PR have some function and should be tied to VCC.

I'll have a look at the code later when I have some more time.

bubulindo:
Do note that there are some extra signals in your chip... pins !WP and !PR have some function and should be tied to VCC.

I'll have a look at the code later when I have some more time.

Correct. I do have those pins tied to Vdd according to the datasheet.

monkey123:
The closest I've gotten is for the LED to blink erratically and very random. I can't quite seem to adapt it properly to work with this pot. ... Any help is VERY appreciated!!

Show your code then.

Ha, good idea. :slight_smile:

The following code gets my LED to blink at different intensities fairly erratically. I'm using an Arduino MEGA2560. I've set the code to do the same thing to the onboard LED, so the LED controlled with the digipot should mimic it. I'm writing the same data to both channels of the pot and outputting it to the serial port.

#include <SPI.h>

const int SELECT = 53;
const int emptyPin = 10;
const int ledPin = 13;

void setup()
{
  pinMode(SELECT, OUTPUT);
  pinMode(emptyPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  Serial.begin(9600);
}

void loop()
{
  for(int i = 0; i < 1024; i++)
  {
    digitalPotWrite(0, i);
    digitalPotWrite(1, i);
    analogWrite(ledPin, i / 8);
    Serial.println(i);
    delay(2);
  }
  for(int i = 1024; i > 0; i--)
  {
    digitalPotWrite(0, i);
    digitalPotWrite(1, i);
    analogWrite(ledPin, i / 8);
    Serial.println(i);
    delay(2);
  }
}

int digitalPotWrite(int address, int value)
{
  // take the SS pin low to select the chip:
  digitalWrite(SELECT, LOW);
  //  send in the address and value via SPI:
  SPI.transfer(address);
  SPI.transfer(value);
  // take the SS pin high to de-select the chip:
  digitalWrite(SELECT, HIGH);
}

Everything else I've tried, such as including SPI.transfer(0xB); (supposedly the correct command byte) before the SPI.transfer(address); line, gets no results. The LED is off.

monkey123:
The AD5235 I'm using requires a 24-bit serial data-word of four command bits, four address bits and 16 data bits.

...

 //  send in the address and value via SPI:

SPI.transfer(address);
  SPI.transfer(value);

Well I only see you transferring 16 bits here.

I have also tried the following, to no avail. The code above gets the LED to blink sporadically, the code below makes it do nothing:

SPI.transfer(0xB);
SPI.transfer(address);
SPI.transfer(value);

Any ideas?

Success! A little bit of trial and error...

  digitalWrite(SELECT, LOW);
  command = 0xB << 4;
  command += address;
  SPI.transfer(command);
  sendValue = 0xFF << 6;
  sendValue += value;
  SPI.transfer(sendValue >> 8);
  SPI.transfer(sendValue & 0xFF);
  digitalWrite(SELECT, HIGH);