Using SPI with Arduino (Digital Potentiometer)

I am very new to Arduino, so please excuse any incorrectly used terminology - I’m still getting my head around it all!

I am trying to use an Arduino M0 PRO to communicate with an AD8403 1k digital potentiometer. After following many examples online, implementing them, getting a colleague to check my work, and getting no output from the DigiPot, I decided to read around SPI and try to infer what I needed in my script to communicate properly with the chip.

Below is the code I wrote:

* AD8403 Test On PCB I/O Blade
* 
* CS - to digital pin 10 (SS pin)
* SDI - to digital pin 11(MOSI pin)
* CLK - to digital pin 13 (SCK pin)
* 
* VDD - to 5V
* DGND - to ground
* SHDN - to 5V
* RST - pulled high
* 
* A(1,2,3,4) - to 5V
* W (1,2,3,4) - to oscilloscope
* B(1,2,3,4) - to ground
* AGND(1,2,3,4) - to ground
*/

#include <SPI.h>

const int chipSelect = 10;
int address;
int value;

void digitalPotWrite(int address, int value){
 SPI.transfer(address);
 SPI.transfer(value);
}

void setup() {
 pinMode (chipSelect, OUTPUT);
 SPI.begin();
}

void loop() {
 for (;;){
    for (value = 0; value < 255 ; value++){
       digitalPotWrite(2, value);
    }
 }
}

//END

This is supposed to select chip 3 in the DigiPot (I chose that number so that the binary address sent would be 2 characters in length, as is on the spec sheet), and then scale from 0-255 on the sweep arm of the Pot. This should hopefully then give out a voltage which ranges from 0V up to 5V.

I used a logic analyser after checking with a multimeter, and with both tools I found that I had nothing coming out of the digital pins on the arduino.

The 5V and ground were working.

This is first time using SPI, and my first time really writing a script myself, so any help or suggestions would be appreciated.

Thanks in advance!

Did you look in the IDE?
File → Examples → SPI → DigitalPotContol

Look at this: Matthew McMillan: Arduino - Using digital potentiometers (AD8403)

gfvalvo:
Did you look in the IDE?
File --> Examples --> SPI --> DigitalPotContol

This is the sketch I have tried most recently. It's right in front of me, in fact. Unfortunately, none of the LEDs are lighting up. I have just tried one of the LEDs to ensure that they're not broken too.

Danois90:
Look at this: Matthew McMillan: Arduino - Using digital potentiometers (AD8403)

I have tried this a couple of times this last week. Alas, it has been to no avail. I went to his YouTube video of the example and it had more dislikes than likes, so I can only assume that other people didn't have any luck either.

Thank you both for the suggestions, though. They are appreciated!

I don't suppose anyone would be willing to proofread the code I have done too? As well as trying to resolve this one issue, it has been a learning process, and I am hoping I have learnt enough to be able to write some sort of basic SPI.

chamsta:
This is the sketch I have tried most recently. It's right in front of me, in fact.

Then why doesn't the code you posted look like the example? Where is SPI.begin()? Where are you controlling the chip select?

Also, the newer SPI library uses transactions. See: https://www.arduino.cc/en/Tutorial/SPITransaction

EDIT:
My bad, I see SPI.begin() now. But you're still not activating chip select.

EDIT #2:
Your loop() is gonna zip through all 256 settings before you can observe the changes. Put in some delay.

gfvalvo:
Then why doesn’t the code you posted look like the example? Where is SPI.begin()? Where are you controlling the chip select?

Also, the newer SPI library uses transactions. See: https://www.arduino.cc/en/Tutorial/SPITransaction

Because I am now trying that code after my code didn’t end with a positive result either - I thought that’d be self explanatory? I am trying every code I come across on the internet, so have tried many different setups.

In my code, SPI.begin() is in the setup. Chip select is being controlled later when I try and write using SPI (where I tell the arduino to use chip 2). It’s all in the code I posted in the first question post. Or is this not what you mean?

I haven’t see this transaction thing though, so thank you for that. That being said, the one in the IDE doesn’t use transactions as far as I can tell, so unless the IDE example needs updating too, I can’t see that being a limiting factor?

Please, do tell me if I have misunderstood it?

Thank you for looking into this.

You should compare the routine you wrote for digitalPotWrite() (code in first post)

With the one in the example;

File --> Examples --> SPI --> DigitalPotContol

They are different, something is missing.

This is NOT controlling the device’s chip select pin:

digitalPotWrite(2, value);

The example code does it here:

void digitalPotWrite(int address, int value) {
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin, LOW);    <---------------- ACTIVATE CS~
  //  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(slaveSelectPin, HIGH);    <---------------- DEACTIVATE CS~
}

I see in your code:

const int chipSelect = 10;

Do you have the POT’s CS~ pin connected to Pin 10 on your board?

The example code is dated 10 August 2010. Whether or not you use SPI Transactions is up to you. I always do.

gfvalvo:
I see in your code:

const int chipSelect = 10;

Do you have the POT’s CS~ pin connected to Pin 10 on your board?

I do, yes. Is this incorrect?

gfvalvo:
This is NOT controlling the device’s chip select pin:

digitalPotWrite(2, value);

The example code does it here:

void digitalPotWrite(int address, int value) {

// take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin, LOW);    <---------------- ACTIVATE CS~
  //  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(slaveSelectPin, HIGH);    <---------------- DEACTIVATE CS~
}

Oh, I see! So, would I be incorrect in assuming, then, that the slave select works the same as the chip select in this case?

When I asked about SPI transaction, it was because I wasn’t sure if you meant, in your previous post, that the whole SPI language had changed at some point, and thus the IDE example wasn’t relevant anymore. That’s my misunderstanding.

I’ll have a look into SPI transactions today.

Thank you again for taking the time to reply to this.

SPI Chip Select and Slave Select are the same thing. It is an input pin on the SPI Slave device (output pin the on the SPI Master) that must be activated (usually pulled low) before the master can accesses it. This allows multiple devices to share the same MISO, MOSI, and SCK lines without interference. The SPI Master device must activate one (and only one) of the Chip Selects before initiating a data transfer.

When using the Arduino SPI library, your sketch must first explicitly activate the Chip Select for the device you want to talk to. The DigitalPotContol example does that with the lines I pointed out in Reply #7.

Your code doesn’t do that. This may not be your only problem, but it’s not going to work until you fix a least that.