Photon controlling 5V output using MCP4151 POT and Photon SPI API

[Edit… I figured this out and solution is below]

I am attempting to control voltage 0-5V using a 4151 POT using SPI on the Photon. My physical connections are:

(4151-Photon)
CC - D5
CK - D4
SDI - D2
VSS - GND
VDD - 5 V
P0B - GND
P0W - (output voltage/test)
P0A - 5 V

Just to test I have this code in setup() (set voltage to 0 in this case):

SPI1.setBitOrder(MSBFIRST);
SPI1.setClockSpeed(10,MHZ);
SPI1.setDataMode(SPI_MODE0);
SPI1.begin();
digitalWrite(D5, LOW);
SPI1.transfer(0x00);

I have tried every which way that I can think of to change things up but the voltage on from the 4151 never changes from around 2.26V,

Could anyone help me out?

Thanks!

I found from this link that with the 4151, you have to call transfer() twice, once with the value of 0 then with the value you that you want to set the level to. Apparently, the first call sets the channel to 0. This code works:

/////////////////////////////////
    SPI1.setBitOrder(MSBFIRST);
    SPI1.setClockSpeed(10,MHZ);
    SPI1.setDataMode(SPI_MODE0);
    SPI1.begin();

    //select the POT device
    digitalWrite(D5, LOW);
    //set the channel
    SPI1.transfer(0);

    //set the POT level
    SPI1.transfer(255);

    //unselect the POT device
    digitalWrite(D5, HIGH);
///////////////////////////////////////////

Now all I have to do is figure out how to convert a String to an int value, heh.

By the way, whenever I put a for loop with the loop() function, and flash, the Photon goes into green breathe mode.

[Edit: code correction per peekay… tested and works]

Any other way that I tried to configure the code with a function ended up with the Photon breathing green. This code works with the physical setup as shown above:

setup(){
    SPI1.setBitOrder(MSBFIRST);
    SPI1.setClockSpeed(10,MHZ);
    SPI1.setDataMode(SPI_MODE0);
    SPI1.begin();
}

void loop() {
    digitalPotWrite("0");
    delay(2000);    
    digitalPotWrite("100");
    delay(2000);
    digitalPotWrite("200");
    delay(2000);
     }

int digitalPotWrite(String setP) {
   
    digitalWrite(D5, LOW);
    SPI1.transfer(0);
    int i = atoi(setP);
    SPI1.transfer(i);
    digitalWrite(D5, HIGH);
    return i;
}

@rabenja, you may want to take a look at porting this Arduino library for mcp4xxx devices:

:wink:

Thanks. I saw that and several others from the Arduino community. Maybe this stuff is just obvious to non-noobs, but there really was no reference for getting the Photon to talk to the 4151. The only think that I was missing was that I needed to call SPI.transfer() twice, which I found out from the link I posted above.

@rabenja, in your code, you should move all the SPI setup code to setup():

    SPI1.setBitOrder(MSBFIRST);
    SPI1.setClockSpeed(10,MHZ);
    SPI1.setDataMode(SPI_MODE0);
    SPI1.begin();

Notes:

MCP41XX datasheet
The MCP 4151 has 257 steps to control the voltage.
In my case I need to control 5V from the Photon SPI. Use the D-side SPI interface pins. Note that the MCP4151 VDD and P0A pins are both connected to 5V. I am powering the Photon with 5V supply to the VIN.

peekay, I swear I tried that this morning and the Photon started breathing green. Now switched the code block to setup and it is fine…

1 Like

@rabenja @peekay123

I have the MCP4151 50k digital pot up and running on the Photon now with zero issues.

Here is the code I’m using to control it:

I’m pretty sure Peekay123 is the one who helped me figure out what needed to be removed to get this library working with the MCP4151.

1 Like