MCP4131 how to read the wiper position?

I am looking for a code example:

how to read the wiper position of MCP4131.

My writing code is:

#include <SPI.h>
byte address = 0x00;
int CS= 10;
int value = 0;

void setup(){
pinMode (CS, OUTPUT);
SPI.begin();
}

void loop(){
value = 75;
digitalPotWrite(value);
while(1);
}

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

Hi, Have you looked for a library?

google MCP4131 library

The libraries have example codes with them.

Tom... :)

There are tons of articles online, here is one.

J-M-L: thank you for your suggestion, but the problem is the suggested example is writing only.

Yes I have been searching for hours, I can't find a working code for reading the wiper position.

So I hope for same help :)

Reading the wiper position does not really make sense as the MCP4131 has volatile RAM so when power cycled it sets the pot to centre wipe position. As the only way the wiper can change is under program control you should know where it is.

A quick look at the datasheet points to setting the write command in the first byte of a 16 bit command so from your example I think just setting the commands bits in the address variable to read will work and you also need to read back the results of the SPI.transfer.

byte address = 0x00 | 0b1100;

...
byte x1 = SPI.transfer(address);
byte x2 = SPI.transfer(value);

should set the command bits to read instead of write but without hardware I cannot be sure.

Hi, Why do you need to know independent of the pot position command, where the pot is set?

Tom.. :)

TomGeorge:

I need to know the position of the wiper or more correct wipers. Because I am using 2 MCP4131 in an analog circuit (op-amp).

One MCP4131 to control gain and one MCP4131 to control attenuation. I set the 2 MCP4131's with a rotary encoder.

This means the "Gain" MCP4131 is set to a value and the "Attenuation" MCP4131 is set to another value.

I would like to display the two settings (LED display).

I have the system fully working - but not the reading of the 2 wiper positions

How do you set the pots based on the rotary encoder?

I'm assuming the Arduino does it, which brings us back to the same question: If your code sets the pot, you can keep track of what you set it to. Why do you need to read it from the device?

classic: One MCP4131 to control gain and one MCP4131 to control attenuation. I set the 2 MCP4131's with a rotary encoder.

How do you set it ? do you read the rotary encoder in your code on your Arduino and use that to set the gain and attenuation?

I have the system fully working - but not the reading of the 2 wiper positions

could you post your code

I tried to post / insert the code but got the message - exceds the word linit......


I understand your questions, but perhaps I dont follow why it is important to know the full code for the system.

I also know an alternativ solution as you perhaps would like to suggest. that could be to store the last setting of the individual pot of course.

But I really prefer to be able to actually read the setting of the pot by sending a read code to the pot.

Is the reason for your questions - it is very difficult to read the pot ?

Writing to the pot is really simple as the code in #1.

It looks like it can be done. Read about it in the datasheet. Personally, I'd rather have the Arduino remember what it set it to than puzzle out how to do the read, but it looks like sections 6 & 7 have the details.

I think you just send a read command and a dummy data byte and as that byte is clocked in, the stored data in the pot is clocked out.

classic: But I really prefer to be able to actually read the setting of the pot by sending a read code to the pot.

Why? What benefit does doing so provide?

It looks like the device's multiplexed SDO / SDI line makes it more trouble than it's worth. How do you have that line connected to the Arduino's MOSI and MISO pins?

classic: I also know an alternativ solution as you perhaps would like to suggest. that could be to store the last setting of the individual pot of course.

Yes that's what we suggest.

Hi, If you are using two different variable names for each digital pot position command, then you have already stored the last pot position in those two variables. So display those variables.

Tom... :)

Did you try the info I posted in #4? I would like to know if it works or not.

Thank you for the suggestions, I have been working with the problem again to day. But because of my very limited experience with SPI communication I have not fund the right solution.

Yes - i have been working with the suggestion in post #4, but I did not receive any data.

I have been searching (Google) for possible solutions and found several possible solutions but not a direct working solution for MCP4131.

I found also the solution for MCP4131 multiplexing MISO / MOSI (adding a resistor).

So I have to say: I hope for a working code for MCP 4131 as a starting point ---- so far :( So I stop for now and use the rotary encoder step data allready availible.

Thank you ery much for your time.