SPI Digital Potentiometer

Hi,

I have a MCP4261 dual channel 10K digital potentiometer. I’ve modified the example SPI digital potentiometer sketch and I want to just set a value for the potentiometer and not have it cycle through all the wiper values. Just set to ONE wiper value. Here is my code but it’s not working at all, can someone tell me what I’m doing wrong? Thank you.

// inslude the SPI library:
#include <SPI.h>

// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;

void setup() {
// set the slaveSelectPin as an output:
pinMode (slaveSelectPin, OUTPUT);
// initialize SPI:
SPI.begin();
}

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

void loop() {

// go through the 2 channels of the digital pot:
for (int channel = 0; channel <= 1; channel++)
{
// set value for each potentiometer
if (int channel = 0)
{
int level = 255;
}
if (int channel = 1)
{
int level = 200;
}

}

}

  1. code need to be enclose in special tags, (section 7 how to do): http://forum.arduino.cc//index.php?topic=149014.0 important, else some parts of code can be readable

  2. Wrong

 if (int channel = 1)

Correct ( for test == double assignment = only one ):

 if (channel == 1)
  1. in loop() you never call setLed()
    if (int channel = 0)

Why are you defining a new variable here? Why are you assigning it the value 0?

    {
      int level = 255;
    }
    if (int channel = 1)
    {
      int level = 200; 
    }

More local variables that immediately go out of scope.

The loop() function accomplishes nothing, since nothing is ever sent to the potentiometer.

Int channel = 0.

In that statement I'm thinking that when the for loop cycles from int channel = 0, I can assign a wiper value to potentiometer 0; same when the for loop statement cycles to int channel = 1, I can then assign a wiper value to potentiometer 1.

if (channel == 0)
{  setLed(0, 255);    
}
if (channel == 1)
{  setLed(1, 200);    
}

When I adjusted my code according to nid69ita, my digital pot still does not adjust the LED brightness. Whatever value I input into setLed(0, VALUE) where VALUE= 0-253, the LED remains at the same brightness…

Here’s how I hooked up my circuit:
pin 1 to D10
pin 2 to D13
pin 13 to D12
pin 3 to D11
pin 4 to ground
pin 8, 12,14 to +5V
pin 10 with 1K resistor to ground
pin 9 to LED to Ground

Here’s my updated code:

#include <SPI.h>
const int slaveSelectPin = 10;

void setup() {
pinMode (slaveSelectPin, OUTPUT);
SPI.begin();
}
void setLed(int reg, int levelLed)
{

digitalWrite(slaveSelectPin,LOW);
SPI.transfer(reg);
SPI.transfer(levelLed);
digitalWrite(slaveSelectPin,HIGH);
}

void loop() {
//Cycle through both pot channels.
for (int channel = 0; channel <= 1; channel++)
{
// set value for each potentiometer
if (channel == 0)
{ setLed(0,150);
}
if (channel == 1)
{ setLed(1, 250);
}

}

}

Does SPI.transfer() take an int or a byte/uint8_t? Does your digital potentiometer expect an int or a byte/uint8_t? Does the digital potentiometer expect an int for the channel number or a byte/uint8_t?

The datasheet shows the address is the upper 4 bits of the first transfer, not the lower 4 bits. Page 45 and 46 here: http://ww1.microchip.com/downloads/en/DeviceDoc/22059b.pdf You need to shift the address 4 bits left. That should not affect wiper 0 tho.

wiper 0 address and write command would be 0x00 wiper 1 address and write command would be 0x10

edit: After some thought and reading, I found that I lied. If you make both those calls, both would set wiper 0.

    if (channel == 0)
    { 
        // set wiper 0 to 150
        setLed(0,150);
    }
    if (channel == 1)
    {
        // set wiper 0 to 506?
        setLed(1, 250);  
    }

The format is this: A3 - A2 - A1 - A0 - C1 - C0 - D9 - D8 - - D7 - D6 - D5 - D4 - D3 - D2 - D1 - D0 I put a "- -" at the point that the first transfer ends and the second transfer starts. That is 4 address bits (A3 to A0), 2 command bits (C1 and C0), then 10 data bits (D9 to D0).

At this point, I'm a little loss and confused as to how to get the digital pot going. It seems a lot more complicated than the example SPI sketch that allows for the LEDs to fade in and out which I thought was suppose to be a lot more complicated.

How exactly would I set the the channel that I want using bytes? I was under the assumption that I could just cycle through the channels the way I did before and just use simple loops and Boolean statements to get it working.

But which SPI example you changed?

On Arduino Playground I found this library for MCP4261: http://playground.arduino.cc//Code/Mcp4261

It isn’t that difficult. Did you try something like this? I changed the parameters to byte rather than integer.

void setLed(byte reg, byte levelLed) 
{
  byte regAdd = reg;
  // shift register address 4 bits left
  regAdd = regAdd << 4;

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