Page 1 of 1

How to build a Digital Potentiometer using a microcontroller

Posted: Fri Jun 24, 2011 6:45 am
by Shane
This tutorial demonstrates the use of the AD5206 Digital Potentiometer. This 24-DIP IC has 6 built in 10K OHM potentiometers which can be controlled using the Arduino's built in SPI interface. In order to update the setting of any of the 6 potentiometers, a 2 byte data transfer must be made. To do this, the Slaveselect pin must be set LOW, then the first data byte is transferred (the potentiometer address), following this is the second data byte (the potentiometer value). To complete the data transfer, Slaveselect is set HIGH.

In this circuit, we have connected an LED to each of the 6 wipers of the AD5206, with the A pins connected to ground and the B pins connected to +5VDC, exactly the same way the potentiometer is wired to be a voltage divider in the potentiometer analogue input tutorial. In this code example, we cycle through each of the 6 built in potentiometers and flash the LEDs quickly ON and slowly OFF to demonstrate the potentiometer function of the AD5206.
1.JPG
1.JPG (41.82 KiB) Viewed 5848 times

Code: Select all

//use SPI to control the AD5206 Digital Potentiometer
 //with LEDs connected to each potentiometer channel (6)

#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=0;
 byte waitVariable = 0;

///////////////////////////////////////////////////////////////////
 //spi transfer function (from ATmega168 datasheet)
 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);
 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++) //set value of each pot to 0, turns off LEDS
 {
 write_pot(i,255);
 }
 }

///////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////
 byte write_pot(int potAddress, int potValue) //dig pot data transfer function
 {
 digitalWrite(SLAVESELECT,LOW); //digital pot chip select is active low
 //2 byte data transfer to digital pot
 spi_transfer(potAddress);
 spi_transfer(potValue);
 digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
 }

///////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////
 void loop()
 {
 waitVariable = 50; //delay variable begins at 50ms
 for (resistance = 0; resistance <50; resistance++) //0 to 50 is most effective range for LED brightness
 {
 write_pot(pot,resistance); //send SPI data to digital potentiometer
 delay(waitVariable); //wait, leave pot at current setting
 waitVariable--; //increase fade rate each time through for loop
 }
 write_pot(pot, 255); //set resistance to 0 to turn off LED completely
 pot++; //cycle through each of the 6 pots in the 5206 chip
 if (pot==6) 
 {
 pot=0;
 }
 }
2.jpg
2.jpg (198.21 KiB) Viewed 5849 times
3.jpg
3.jpg (191.52 KiB) Viewed 5849 times
4.jpg
4.jpg (197.43 KiB) Viewed 5849 times
Courtecy of UMASS AMHERST

Re: How to build a Digital Potentiometer using a microcontroller

Posted: Sat Jul 28, 2012 1:42 pm
by Nandika
Thank you..
This is a good point...

download datasheet
140808745AD5204_6_0.pdf
(180.22 KiB) Downloaded 691 times