Having trouble connecting two digital pots MCP 41H51503 to Arduino Mega

Status
Not open for further replies.

sulfur101

Newbie level 6
Joined
Jul 20, 2022
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
77
C++:
#include <SPI.h>

static const int spiClk = 1000000;
byte address = 0x00;
byte address2 = 0x01; // Address for the second digital pot
SPIClass *hspi = NULL;

void setup()
{
  pinMode(53, OUTPUT); // Set SS (Slave Select) pin for the first pot to 53 on Mega
  pinMode(45, OUTPUT); // Set SS pin for the second pot to 45
  hspi = new SPIClass(SPI);
  hspi->begin();
}

void loop()
{
  int counter = 0;
  while (1) {
    for (int i = 0; i <= 255; i++)
    {
      digitalPotWrite(53, address, i); // Control the first pot
      if (counter <= 1) {
        digitalPotWrite(45, address2, 255 - i); // Control the second pot
      }
      delay(10);
    }
    counter++;
    delay(500);
    for (int i = 255; i >= 0; i--)
    {
      digitalPotWrite(53, address, i); // Control the first pot
      digitalPotWrite(45, address2, 255 - i); // Control the second pot
      delay(10);
    }
  }
  counter = 0;
}

void digitalPotWrite(int csPin, byte address, int value)
{
  hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(csPin, LOW); // Select the MCP4131
  hspi->transfer(address);
  hspi->transfer(value);
  digitalWrite(csPin, HIGH); // Deselect the MCP4131
  hspi->endTransaction();
}

the code that i have are two digital potentiometers and they are just not working and are my SCK,SDI pins supposed to be the same?
 

Hi,

It's simpler to use a dual pot.
But you may use two i deoendent.

The problem here is:
* we don't see how you wired it (schematic)
* we don't see which pins you used for the SPI in software
* we don't have a clear error description, including test setup

I see no need to beginTransaction and endTransaction for every single access. I mean: the setup is always the same.

Klaus
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…