# digital pot's ( AD5206 ) not working together!

**URL:** <https://forum.arduino.cc/t/digital-pots-ad5206-not-working-together/18666>\
**Category:** Interfacing\
**Created:** [March 7, 2008, 4:16am UTC](https://forum.arduino.cc/t/digital-pots-ad5206-not-working-together/18666 "2008-03-07T04:16:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 7, 2008, 4:16am UTC](https://forum.arduino.cc/t/digital-pots-ad5206-not-working-together/18666/1 "2008-03-07T04:16:55Z")

</div>

hi! iv'e been working with a.square for some time trying to solve my problem, and i still have not found a solution, hopefully somebody here can help...

so i'm trying to make the rgb button pads from sparkfun work using 2 AD5206 digital pot's working either simultaniously, or turn one off and the next on really fast to assume the allusion of constant on...

so i tried the tutorial on the AD5206 from the tutorial page, and it's very nice, but when i try to change slave select, to lets say pin 8, or 18, or 4, or any random pin, it doesent seem to work, and that's why i believe the second digital pot won't work, becuase it seems only pin 10 works for the digital pots, and i have 2, so i need seprate slave selects...

when i try the code a.square gave me, it just flips out and doesent do anything impressive...

so here are some resources that will help you help me:

AD5206 tutorial:

> **[Digital Potentiometer Control | Arduino Documentation](https://docs.arduino.cc/tutorials/communication/DigitalPotControl)**
>
> Control a AD5206 digital potentiometer using the SPI protocol.

my code:

```arduino
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK  13//sck
#define SLAVESELECT 10 // right here is the problem, only pin 10 on the arduino works, no other pin!
byte potpin = 0;
byte r1=0;
byte g2=1;
byte g1=2;
byte b2=3;
byte b1=4;
byte r2=5;
byte res1=255;
byte res2=255;
byte res3=255;
byte res4=255;
byte res5=255;
byte res6=255;
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 i;
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, 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);
  for (i=0;i<6;i++)
  {
    write_pot(i,255);
  }
}

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()
{
 digitalWrite(7, LOW);
write_pot(r2, 0);
 write_pot(g1, 0);
 delay(1);
 digitalWrite(7, HIGH);
 digitalWrite(6, LOW);
 write_pot(g1, 255);
 write_pot(r1, 0);
 write_pot(r2, 255);
 write_pot(b2, 0);
 delay(1);
 digitalWrite(5, LOW);
 digitalWrite(6, HIGH);
  write_pot(r1, 255);
  write_pot(b2, 255);
  write_pot(g1, 0);
  write_pot(b2, 0);
  write_pot(r2, 0);
 delay(1);
 digitalWrite(4, LOW);
 digitalWrite(5, HIGH);
 write_pot(b1, 0);
 write_pot(g2, 0);
 write_pot(g1, 0);
 write_pot(b2, 255);
 write_pot(r2, 255);
 delay(1);
 digitalWrite(4, HIGH);
write_pot(g2, 255);
write_pot(b1, 255);
write_pot(g1, 255); 
 }

```

a.square's code:

```arduino
// Each button randomly cycles through colors. 
// When a button is pressed, all of the buttons change to that color and pause
// The pressed button(s) is/are lit up white while pressed

#define DATAOUT 11//MOSI (pin 7 of AD5206)
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK  13//sck (pin 8 of AD5206)
#define ROWS 4
#define COLS 4
#define H 254
#define L 64

#define slavesel(x) ((x<6) ? 0 : 1)
const byte rowpin[ROWS] = {
  14,15,16,17};
const byte slaveselect[2] = {
  10, 18};

// The pot register numbers for each of the red, green, and blue channels
const byte red[4] = {
  5, 2, 7, 8};
const byte green[4] = {
  3, 0, 6, 11};
const byte blue[4] = {
  1, 4, 9, 10};
byte rGrid[ROWS][COLS] = {
  0};
byte gGrid[ROWS][COLS] = {
  0};
byte bGrid[ROWS][COLS] = {
  0};

const byte buttonWrite[4] = {
  2, 5, 3, 4}; //Pins for the Vin of the buttons
const byte buttonRead[4] = {
  6, 7, 9, 8}; //Pins for reading the state of the buttons
boolean pressed[ROWS][COLS] = {
  0};

byte trajectory[ROWS][COLS] = {
  0};
unsigned long time;
#define PAUSE 1000

// END DEFINITIONS, BEGIN PROGRAM

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
}

byte write_pot(byte address, byte value)
{
  digitalWrite(slaveselect[slavesel(address)], LOW);
  //2 byte opcode
  spi_transfer(address % 6);
  spi_transfer(constrain(255-value,0,255));
  digitalWrite(slaveselect[slavesel(address)], HIGH); //release chip, signal end transfer
}

void setup(){
  randomSeed(1);

  byte i;
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(slaveselect[0],OUTPUT);
  pinMode(slaveselect[1],OUTPUT);

  for(byte r = 0; r < ROWS; ++r){
    pinMode(rowpin[r], OUTPUT); // Initialize rows
    digitalWrite(rowpin[r], LOW); // Turn all rows off
  }

  digitalWrite(slaveselect[0],HIGH); //disable device
  digitalWrite(slaveselect[1],HIGH);
  // 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);
  // clear all of the pot registers
  for (i=0;i<12;i++)
  {
    write_pot(i,0);
  }
  //setup the button inputs and outputs
  for(int i = 0; i < ROWS; ++i){ // ???? Is ROWS

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 7, 2008, 4:17am UTC](https://forum.arduino.cc/t/digital-pots-ad5206-not-working-together/18666/2 "2008-03-07T04:17:57Z")

</div>

and also, this is the original thread, where i have pics up of my setup, so that could help too:

[http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1200846962/15](http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1200846962/15)

thanks in advance for any help!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 9, 2008, 2:47am UTC](https://forum.arduino.cc/t/digital-pots-ad5206-not-working-together/18666/3 "2008-03-09T02:47:40Z")

</div>

thanks alot guys..... i'm just gonna use relays to switch the 2 wires from connecting... problem solved

---

<div class="post-metadata">

**Author:** ![Varun](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Varun](https://forum.arduino.cc/u/Varun)\
**Post date:** [July 12, 2010, 2:47pm UTC](https://forum.arduino.cc/t/digital-pots-ad5206-not-working-together/18666/4 "2010-07-12T14:47:42Z")

</div>

Hi, I am having a similar problem. Is it possible for you to explain your "relay" procedure a bit more? I do not have an electrical background so I feel totally handicapped.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 7:13pm UTC](https://forum.arduino.cc/t/digital-pots-ad5206-not-working-together/18666/5 "2021-05-06T19:13:39Z")

</div>


