SPI digipot control w/ Sparkfun Pro Micro Atmega32u4 5V/16MHz

Hey everyone,

I am attempting to use the Sparkfun Pro Micro Atmega32u4 5V/16MHz to control a digipot (MCP4131) via SPI.

Unfortunately, the SS pin (17) is not exposed on this board so I am using A0 (pin 18) for CS.

I have modified the code listed in this tutorial

#include <SPI.h>

byte address = 0x00;
int CS= 18;

void setup()
{

pinMode (CS, OUTPUT);
SPI.begin();  // Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.

// SPI.beginTransaction(SPISettings(15000000, MSBFIRST, SPI_MODE0));

}

void loop()
{
for (int i = 0; i <= 128; i++)
{
digitalPotWrite(i);
delay(10);
}
delay(500);
for (int i = 128; i >= 0; i--)
{
digitalPotWrite(i);
delay(10);
}
}

int digitalPotWrite(int value)
{
digitalWrite(CS, LOW);
SPI.transfer(CS, address);
SPI.transfer(CS, value);
digitalWrite(CS, HIGH);
}

Unfortunately, I cannot get this to work and have a few questions for the forum:

  1. What am I missing from the setup?

The SPI.begin() documentation explicitly states "Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.", which seems like everything I need other than additionally setting MISO to INPUT.

  1. Does a SPI.beginTransaction statement need to be included?

  2. Should CS (pin 18) or SS (pin 17) be targeted in digitalPotWrite ()?

The same tutorial explains the digitalPotWrite function:

The digitalPotWrite() function is the last block of code. In this function, we select the digital potentiometer chip by drawing the CS pin LOW. This is how we select the device, so the MCP4131 is now selected. After this, we call the address that we want to write to. Above, we initialized the address variable to 0x00; this selects the wiper terminal of the potentiometer which we will write to. We then transfer the value which is taken from the i in the for loop to the wiper terminal. This changes its resistance. Lastly, we draw the CS pin HIGH, ending the communication with the MCP4131. Whenever we write to a slave device, we draw the CS LOW. Afterwards, when we're finished with the function, we draw the CS HIGH, to unselect it. Typically, if you only have one SPI slave device, it wouldn't be necessary to do this. But it's good practice, hence, we show it here. But if you have multiple SPI devices you are communicating with, you would have to draw the CS pin HIGH. This is because you can only communicate with one device at a time. Therefore, you would have to draw the CS pin HIGH after communicating with a device.

  1. How is the address variable (0x00) determined? Is this a property of the MCP4131 digipot?

Any insight on these issues would be greatly appreciated! :slight_smile:

Did you install the Arduino IDE plug-in ?

Probably not, which plugin? I am running Arduino 1.8.3.

There is a link to it on the product website.

ieee488:
There is a link to it on the product website.

I've used this board for other applications-- the drivers, etc are properly installed and the Arduino IDE recognizes the board as "Sparkfun Pro Micro". So unless I am misunderstanding (which is not unlikely) :confused: I do not think that is the issue.

Please elaborate if I've misunderstood :slight_smile: