Using EEPROM with Digital Potentiometer

Hey all,

I am having some issues with integrating my EEPROM and Digital Potentiometer together.

So, I have saved some values in my EEPROM and I have to extract it to control the position of my digital potentiometer. Here's my code:

#include <Wire.h>    
#include <SPI.h>
#define disk1 0x50    //Address of 24LC256 eeprom chip
const int slaveSelectPin = 10; 
int eeprom_data[6];
int temp=0;
void setup(void)
{
  Serial.begin(9600);
  Wire.begin();  
 
  unsigned int address = 0;

  Serial.print("Contents of EEPROM: ");
  //EEPROM contains 50,100,150,200,25,130
  for(int i=0; i<6; i++)
  {
    eeprom_data[i]=readEEPROM(disk1,i);
    Serial.print(eeprom_data[i]);
    Serial.print(" ");
  } 
  for (int j = 0; j < 6; j++) 
  {
    digitalPotWrite(j, eeprom_data[j]);
    Serial.print(j);
  }

}
 
void loop(){}
 
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) 
{
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.write(data);
  Wire.endTransmission();
 
  delay(5);
}
 
int readEEPROM(int deviceaddress, unsigned int eeaddress ) 
{
  byte rdata = 0xFF;
 
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) rdata = Wire.read();
 
  return rdata;
}
void digitalPotWrite(int address, int value) 
{

  // take the SS pin low to select the chip:

  digitalWrite(slaveSelectPin, LOW);

  delay(100);

  //  send in the address and value via SPI:

  SPI.transfer(address);

  SPI.transfer(value);

  delay(100);

  // take the SS pin high to de-select the chip:

  digitalWrite(slaveSelectPin, HIGH);
}

I have successfully extracted the data from my EEPROM but when I passed the array (eeprom_data) to the digitalPotWrite function, the code just froze there and it does not run the for (int j = 0; j < 6; j++) loop. (Edit: this loop only runs once. I need it to run 6 times)

I am using a 24LC256 EEPROM and an AD5206 digital potentiometer.

Any advice is greatly appreciated. Thanks!

Loop index j, but then uses k?

I can't see a k

I made a typo in that. The problem still persists. I cannot run this loop.

The compiler is good at spotting typos.
Show the code that compiles

This is my updated code, without the typo. It does not have any compiler errors. When I uploaded it to the board, the loop with the digitalPotWrite function doesn't run.

#include <Wire.h>    
#include <SPI.h>
#define disk1 0x50    //Address of 24LC256 eeprom chip
const int slaveSelectPin = 10; 
int eeprom_data[6];
int temp=0;
void setup(void)
{
  Serial.begin(9600);
  Wire.begin();  
 
  unsigned int address = 0;

  Serial.print("Contents of EEPROM: ");
  //EEPROM contains 50,100,150,200,25,130
  for(int i=0; i<6; i++)
  {
    eeprom_data[i]=readEEPROM(disk1,i);
    Serial.print(eeprom_data[i]);
    Serial.print(" ");
  } 
  for (int j = 0; j < 6; j++) 
  {
    digitalPotWrite(j, eeprom_data[j]);
    Serial.print(j);
  }

}
 
void loop(){}
 
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) 
{
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.write(data);
  Wire.endTransmission();
 
  delay(5);
}
 
int readEEPROM(int deviceaddress, unsigned int eeaddress ) 
{
  byte rdata = 0xFF;
 
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) rdata = Wire.read();
 
  return rdata;
}
void digitalPotWrite(int address, int value) 
{

  // take the SS pin low to select the chip:

  digitalWrite(slaveSelectPin, LOW);

  delay(100);

  //  send in the address and value via SPI:

  SPI.transfer(address);

  SPI.transfer(value);

  delay(100);

  // take the SS pin high to de-select the chip:

  digitalWrite(slaveSelectPin, HIGH);
}

Do yourself a favour and add some Serial.println statements where you think it's getting stuck... most valuable tool in your diagnostic box.

I have added a Serial.print command in the loop to check if the loop is running. I have tried different methods but this loop still won't run.

for (int j = 0; j < 6; j++) 
  {
    digitalPotWrite(j, eeprom_data[j]);
    Serial.print(j);
  }

Does the pot expect a single SPI transaction?

Edit: I notice you've edited your original post, so now my earlier comment makes no sense.
I'm out.

I have checked the original code from the AD5206 sample (https://www.arduino.cc/en/Tutorial/LibraryExamples/DigitalPotControl).

It cycles through different channels and changes the position. I am doing the same here. But it doesn't run the mentioned loop. I can't figure out why.

Then add some more, lots more...... in digitalPotWrite

Ok update to last, the loop only runs once which I find it extremely weird

Post latest code.
Post matching Serial output.

Here's the latest code with the debugging lines:

#include <Wire.h>    
#include <SPI.h>
#define disk1 0x50    //Address of 24LC256 eeprom chip
const int slaveSelectPin = 10; 
int eeprom_data[6];
int temp=0;
void setup(void)
{
  Serial.begin(9600);
  Wire.begin();  
 
  unsigned int address = 0;

  Serial.print("Contents of EEPROM: ");
  //EEPROM contains 50,100,150,200,25,130
  for(int i=0; i<6; i++)
  {
    eeprom_data[i]=readEEPROM(disk1,i);
    Serial.print(eeprom_data[i]);
    Serial.print(" ");
  } 
  Serial.print("\n");
  Serial.print("Iteration ");
  for (int j = 0; j < 6; j++) 
  {
    Serial.print("\n");
    Serial.print(j);
    digitalPotWrite(j, eeprom_data[j]);
    
  }

}
 
void loop(){}
 
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) 
{
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.write(data);
  Wire.endTransmission();
 
  delay(5);
}
 
int readEEPROM(int deviceaddress, unsigned int eeaddress ) 
{
  byte rdata = 0xFF;
 
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) rdata = Wire.read();
 
  return rdata;
}
void digitalPotWrite(int address, int value) 
{

  // take the SS pin low to select the chip:

  digitalWrite(slaveSelectPin, LOW);

  delay(100);

  //  send in the address and value via SPI:

  SPI.transfer(address);

  SPI.transfer(value);

  delay(100);

  // take the SS pin high to de-select the chip:

  digitalWrite(slaveSelectPin, HIGH);
}

Serial monitor output:

image

Hopefully, this helps! Thanks!

OK so it obviously never comes back from the first call... like I said... use Serial.println... LOTS OF THEM !!!! You are trying to figure out which bit is blocking...

#include <Wire.h>    
#include <SPI.h>
#define disk1 0x50    //Address of 24LC256 eeprom chip
const int slaveSelectPin = 10; 
int eeprom_data[6];
int temp=0;
void setup(void)
{
  Serial.begin(9600);
  Wire.begin();  
 
  unsigned int address = 0;

  Serial.print("Contents of EEPROM: ");
  //EEPROM contains 50,100,150,200,25,130
  for(int i=0; i<6; i++)
  {
    eeprom_data[i]=readEEPROM(disk1,i);
    Serial.print(eeprom_data[i]);
    Serial.print(" ");
  } 
  Serial.print("\n");
  Serial.print("Iteration ");
  for (int j = 0; j < 6; j++) 
  {
    Serial.print("\n");
    Serial.print(j);
    digitalPotWrite(j, eeprom_data[j]);
    
  }

}
 
void loop(){}
 
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) 
{
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.write(data);
  Wire.endTransmission();
 
  delay(5);
}
 
int readEEPROM(int deviceaddress, unsigned int eeaddress ) 
{
  byte rdata = 0xFF;
 
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) rdata = Wire.read();
 
  return rdata;
}
void digitalPotWrite(int address, int value) 
{

  // take the SS pin low to select the chip:
  Serial.println("Here 0");
  digitalWrite(slaveSelectPin, LOW);

  delay(100);
  Serial.println("Here 1");
  //  send in the address and value via SPI:

  SPI.transfer(address);
  Serial.println("Here 2");

  SPI.transfer(value);
  Serial.println("Here 3");

  delay(100);

  // take the SS pin high to de-select the chip:

  digitalWrite(slaveSelectPin, HIGH);
    Serial.println("Here 4");

}

@red_car thanks for your suggestion. I have added in more stuff similar to what you have said but I still can't solve the problem.

I found out where it stopped (refer to comment in code). I have used your code for easy reference.

void digitalPotWrite(int address, int value) 
{

  // take the SS pin low to select the chip:
  Serial.println("Here 0");
  digitalWrite(slaveSelectPin, LOW);

  delay(100);
  Serial.println("Here 1");
  //  send in the address and value via SPI:

  SPI.transfer(address); //Stops here
  Serial.println("Here 2");

  SPI.transfer(value);
  Serial.println("Here 3");

  delay(100);

  // take the SS pin high to de-select the chip:

  digitalWrite(slaveSelectPin, HIGH);
    Serial.println("Here 4");

}

Here's the corresponding output of the serial monitor:

image

Hope you will be able to give me more insights.

SPI.begin();

in setup.

Thanks so much!! Can't believe I missed out on that line.

Remember Serial.println... and a quick google search on "spi.transfer never returns"... you aren't the first, and won't be the last :slight_smile:

@lordpakweet, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.