SPID digital pot using AD8402

I've followed the Tutorial for the SPID digital pot using the AD5206 but I want to use the AD8402. Mostly things looked good until I tried to run the sketch. On closer inspection i found the AD5206 takes an 11 bit instruction and the AD8402 takes a 10 bit. I'm not capable of changing the transmission mask. Can anyone help? I'm sure it's not too difficult but i just can't get it.

Can you post a link to the tutorial you are following.

Hi Grumpy mike
The tutorial is at

I want to use the digital pot to control a capacitor charging circuit. I have already sorted a liquid crystal display and a controller. I only need the digital pot to work then I am coasting!
Pete

Well I have looked through the data sheets for both devices and the tutorial and can't see why there should be any incompatibility. I assume you have modified the code so you are only writing to the first four pots.
What results are you seeing? How are you measuring the outputs?

Hi Mike
The AD8402 has only two pots so the address is 2 bits. The AD5206 has eight pots so it needs three bits to address 0 to 7. So on the SPI_transfer I'm guessing that in the tutorial 11 bits are sent but I need to send 10.

I think the lines are

char spi_transfer(volatile char data)
{
SPDR = data;
While (!(SPSR & (1<<SPIF)))
{
};
return SPDR;
}

any clues?

The AD8402 has only two pots so the address is 2 bits.

No two pots one bit address. That data sheet shows there is a variant with 4 pots so 2 bits are needed.

The difference between 10 bits and 11 bits is taken up automatically by the program actually sending 16 bits and the chip just looks at the last 10 or last 11 depending on the chip type. That bit of code just ensures the SPI buffer is empty and does not need changing.

OK. I understand that you only need one bit for 2 pots but the data sheet didn't differentiate between two or four pots.(Table 8 pin 12 Wiper RDAC 1, addr 00) so I thought it had to be 2 bits.
however your explaination renders this point superfluous.

Perhaps it is something to do with the SPI library? I assume this is built in.

This is the complete code:-

#define DATAOUT 11//MOSI
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss

byte pot=0;
byte resistance=128; //just to test

char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte
}

void setup()
{

byte clr;
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 (fastest)
SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
delay(10);

}

byte write_pot(int address, int value)
{
digitalWrite(SLAVESELECT,LOW);
//2 byte opcode
spi_transfer(address);
spi_transfer(value);
digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
}

void loop()
{
write_pot(pot,resistance);
delay(10);

}

First off, hit the modify button and highlight the code and click on the # icon. That way it is formatted correctly.

The SPI library is not built in it has to be 'included' in your sketch. I can't see the line that does that. However, if it compiles without errors then it must know about it.
That is the code I looked at in the tutorial, as I said I can't see much wrong with it. Make sure you are wiring it up correctly and you have changed the pin numbers to suite the new chip. Make sure you haven't left out any of the pins, even the ones you are not using need to be tied high or low depending on what you want them to be.

Put a 0.1uF decoupling capacitor on the supply close to the chip.

Can you post a schematic of what you actually built?

Hi Mike
Thanks for your input. Have to go now. I will try and continue this tomorrow. I will post the schematic once I find out how!
regards
Peter

I will post the schematic once I find out how!

Save them on a picture saving site like Flicker. The paste the URL it gives you between the brackets that pop up with the picture icon (third from left) in the reply box.

Hi Mike
Mainly for information. I tried using a Duemilanove and it worked perfectly. Although in the Arduino Mega it says the following:-

" SPI:50 (MISO), 51(MOSI), 52(SCK), 53(SS).[/i] These pins support SPI communication, which, although provided by the underlying hardware, is not currently included in the Arduino language. The SPI pins are also broken out on the ICSP header, which is physically compatible with the Duemilanova and Diecimala.[/i]"

I've always taken this to mean I can use the same pins for both.

When I used the Duemilanovo (pins 10,11 & 13) it works! When I use the same pins on the Mega it doesn't work! Finally I used the pins 51, 52 & 53 on the Mega and it Works! ( I had to define these pins in the sketch.)
many thanks for your invaluable help and by the way I did have the RS pin floating when it should be tied to Vdd.
regards
Peter

Well done:-

by the way I did have the RS pin floating when it should be tied to Vdd.

It happens to us all. :wink:

Thanks for posting this information.

I had the same problem.. I am using duemilanove and AD8403 which has 4 pots. I have very little experience with ICs..
so I tried for several hours, read the datasheet and had no success.
..finally I googled for "arduino +ad840" since I knew that more versions exist.

then I read that you forgot to connect RS pin to VDD.. voila, it's working :slight_smile:

btw, I had to download the SPI library from here Arduino Playground - Spi

following code just uses the second pot of AD8403.. :slight_smile:

#include <Spi.h>

const int slaveSelectPin = 10;

void setup(){
  Serial.begin(115200);
  pinMode(slaveSelectPin, OUTPUT);
}

void loop(){
 for (byte channel = 1; channel < 2; channel++){
  for (byte level = 0; level < 255; level++){
   digitalPotWrite(channel, level);
   delay(10);
   Serial.print(0+channel);
   Serial.print("  ");
   Serial.println(0+level);
  }
  for (byte level = 0; level < 255; level++){
   digitalPotWrite(channel, 255 - level);
   delay(10);
   Serial.print(0+channel);
   Serial.print("  ");
   Serial.println(255-level);
  }  
  delay(500);
 } 
}

void digitalPotWrite(byte address, byte value){
  digitalWrite(slaveSelectPin, LOW);
  Spi.transfer(address);
  Spi.transfer(value);
  digitalWrite(slaveSelectPin, HIGH);
}