Hi, I am using Arduino MKRZero Board to communicate with Analog Devices AD5292 Digital Potentiometer using SPI interface.
The AD5292 uses 16 bit words for communication and so I am using SPI.transfer16() function.
My configuration is the Pins D0, D1, D2 and D3 on MKRZero forms a combination which acts as a chip select to select one out of 10 AD5292's that I have in my hardware. I am only using the AD5292 at position 0000 for testing in my current example. Pin D6 on MKRZero is RDY (Pin 14 on AD5292), Pin D7 on MKRZero is RESET (Pin 1 on AD5292) for AD5292 and these two pins are always set to HIGH as I am not using functions associated with these pins. Pin D8 (COPI/MOSI on MKRZero) is connected to Pin DIN (Pin 10 on AD5292), Pin D9 (SCK on MKRZero) is connected to Pin SCLK (Pin 11 on AD5292) and Pin D10 (CIPO/MISO on MKRZero) is connected to Pin SDO (pin 13 on AD5292).
Below is my entire code:
#include <SPI.h>
#include <bitset>
const byte delay_ms = 25; // delay after flush
const char sep = ':'; // Command and RDAC value seperator
void setup() {
/* There are 4 Chip Select lines, depending on this combination
* one of the 10 potentiometers is selected. I am just using
* Potentiometer at position 0000 for current testing. */
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
/* RDY and RESET Pins */
pinMode(6, OUTPUT); // RDY
pinMode(7, OUTPUT); // RESET
/* Setting RDY and RESET pins to HIGH */
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
/* Setting up SPI */
SPI.begin();
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE1));
/* Setting up Serial */
Serial.begin(115200);
}
void loop() {
/* Data send over serial must be greater than at least 2 bytes. Minimum requirements are
* 1 Command byte, 1 Seperator byte and 1 RDAC value byte.
* Values are send over Serial in following as Command_value:RDAC_value
* Possible Values that I am interested in (x = Don't Care):
* 6:2 - Enable Wiper Position Update (always the first value to be sent)
* 1:0 to 1:1023 - write 0 to 1023 to RDAC, changes wiper position to 0 (i.e. Min) or 1023 (i.e. Max)
* 0:x - NOP Command i.e. Do Nothing
* 2:x - Read RDAC Wiper Settings from the SDO output in next frame
*/
if(Serial.available() > 2){
int serialIndex = Serial.available() - 1; // ignoring the last byte which can be new line
char cmd_chars[16]; // to hold Command bytes
char rdac_chars[16]; // to hold RDAC bytes
memset(cmd_chars, 0, 16); // making sure all bytes in the array is set to 0
memset(rdac_chars, 0, 16); // making sure all bytes in the array is set to 0
int sepIndex = 0; // Seperator Index
bool sepFound = false; // variable to know when Seperator has been found
Serial.print("Received over Serial: "); // this will print the exact bytes received, including the seperator
for(int ii = 0; ii < serialIndex; ++ii){
char val = Serial.read();
Serial.print(val); // printing current byte
if(val == sep){
sepFound = true;
sepIndex = ii;
}
else{
if(sepFound){
rdac_chars[ii - sepIndex - 1] = val;
}
else{
cmd_chars[ii] = val;
}
}
}
/* Converting to integer */
int cmd_int = atoi(cmd_chars);
int rdac_int = atoi(rdac_chars);
/* Writing data to potentiometer */
writeDataToPotentiometer(cmd_int, rdac_int);
/* Making sure there is a gap of one line between two different Serial Inputs */
Serial.println();
Serial.println();
flushSerialInput(); // flushing serial
}
}
void writeDataToPotentiometer(int cmd, int rdac)
{
/* bitsets to hold Command and rdac bits */
std::bitset<4> cmd_bits;
std::bitset<10> rdac_bits;
/* Settings all bits to zero */
cmd_bits.reset();
rdac_bits.reset();
/* Getting Command bits from its integer value */
int cmd_index = 0;
int cmd_tmp = cmd;
while(cmd_tmp > 0){
cmd_bits[3 - cmd_index] = cmd_tmp % 2;
cmd_tmp = cmd_tmp / 2;
++cmd_index;
}
/* Printing Command bits over Serial to check if it is correct */
Serial.println();
Serial.print("Command:: Int: ");
Serial.print(cmd);
Serial.print(" | Binary: ");
for(int ii = 0; ii < 4; ++ii){
Serial.print(cmd_bits.test(ii));
}
/* Getting RDAC bits from its integer value */
int rdac_index = 0;
int rdac_tmp = rdac;
while(rdac_tmp > 0){
rdac_bits[9- rdac_index] = rdac_tmp % 2;
rdac_tmp = rdac_tmp / 2;
++rdac_index;
}
/* Printing RDAC bits over Serial to check if it is correct */
Serial.println();
Serial.print("RDAC:: Int: ");
Serial.print(rdac);
Serial.print(" | Binary: ");
for(int ii = 0; ii < 10; ++ii){
Serial.print(rdac_bits.test(ii));
}
/* bitset to hold the entire 16 bits that is to be sent to the potentiometer. The bits are as follows:
* First 2 bits: 00 (always the same)
* Next 4 bits: Command bits (0110: Unfreeze Wiper, 0001: Write RDAC, 0000: Do Nothing, 0010: Read RDAC in Next frame)
* Next 10 bits: RDAC values, varies from 0 to 1023
*/
std::bitset<16> pot_bits;
pot_bits.reset(); // making sure all bits are initially set to zero
for(int ii = 2; ii < 16; ++ii){
if(ii < 6){
pot_bits[ii] = cmd_bits[ii - 2]; // copying Command bits to correct position
}
else{
pot_bits[ii] = rdac_bits[ii - 6]; // copying RDAC bits to correct position
}
}
/* Printing out all 16 bits over serial to check if it is correct */
Serial.println();
Serial.print("Potentiometer Bits: ");
for(int ii = 0; ii < 16; ++ii){
Serial.print(pot_bits.test(ii));
}
/* Getting the 2 byte value to be sent to potentiometer */
uint16_t potVal = 0; // 2 byte value to be sent to potentiometer
for(int ii = 15; ii >= 0; --ii){
potVal += pot_bits.test(ii) * pow(2, 15 - ii);
}
/* Printing the 2 byte potentiometer value */
Serial.println();
Serial.print("Value Sent to Potentiometer: ");
Serial.print(potVal);
/* A 2 byte variable to receive data from potentiometer */
uint16_t receiveVal = 27; // 27 is just a placeholder to see if it is overriden
/* Setting all Chip Select pins to LOW since my test potentiometer is at 0000 position */
for(int ii = 0; ii < 4; ++ii){
digitalWrite(ii, LOW);
}
/* Sending and Receiving from Potentiometer */
receiveVal = SPI.transfer16(potVal);
/* Setting all Chip Select pins to HIGH to complete the SPI transfer */
for(int ii = 0; ii < 4; ++ii){
digitalWrite(ii, HIGH);
}
/* Printing over Serial the value received from Potentiometer */
Serial.println();
Serial.print("Value Received from Potentiometer: ");
Serial.print(receiveVal);
}
void flushSerialInput() {
/* Function to flush data if needed */
while (Serial.available() > 0) {
Serial.read();
delay(delay_ms);
}
}
Below are the outputs that I received:
Received over Serial: 6:2
Command:: Int: 6 | Binary: 0110
RDAC:: Int: 2 | Binary: 0000000010
Potentiometer Bits: 0001100000000010
Value Sent to Potentiometer: 6146
Value Received from Potentiometer: 0
Received over Serial: 1:0
Command:: Int: 1 | Binary: 0001
RDAC:: Int: 0 | Binary: 0000000000
Potentiometer Bits: 0000010000000000
Value Sent to Potentiometer: 1024
Value Received from Potentiometer: 0
Received over Serial: 1:1023
Command:: Int: 1 | Binary: 0001
RDAC:: Int: 1023 | Binary: 1111111111
Potentiometer Bits: 0000011111111111
Value Sent to Potentiometer: 2047
Value Received from Potentiometer: 0
Received over Serial: 2:0
Command:: Int: 2 | Binary: 0010
RDAC:: Int: 0 | Binary: 0000000000
Potentiometer Bits: 0000100000000000
Value Sent to Potentiometer: 2048
Value Received from Potentiometer: 0
Received over Serial: 1:1020
Command:: Int: 1 | Binary: 0001
RDAC:: Int: 1020 | Binary: 1111111100
Potentiometer Bits: 0000011111111100
Value Sent to Potentiometer: 2044
Value Received from Potentiometer: 0
The wiper position of AD5292 is set correctly, which means the SPI.transfer16() function is sending data correctly and my pins are connected correctly.
The returned value is always 0, since I initialize the return value variable with a value of 27, this means that the return value variable is being overridden by SPI.transfer16() function. But it is always 0, I would expect it to give me 2047 at least in the last output since in second last command is 2:0 or 2048 in integer terms, which is to read RDAC wiper settings in next frame.
Using an oscilloscope, I can see the SDO Pin of AD5292 is sending data pulses back and I can see the same being received on MKRZero's MISO pin (D10, where the SDO pin of AD5292 is connected) but the data received from the serial input is always zero. What am I missing? Is there some catch to get the data from SPI.transfer16() function?

