Multiplexing potentiometers

Hi, I sought a whole week an answer about my question but found no one so i finally decided to open a new thread (hoping I'm not doubling an existing one)

I have an Arduino UNO and I need to connect up to eight potentiometers, but i need it to be modular (each pot as a single module due to some different uses and space problems) The problem is I'm not sure how to make the board recognize which pot I'm using, because even if I'm using just one analog input on the board i need every single module to control a different thing: e.g. if I connect two modules in series and they end up in the Analog0 port, it's completely useless for me if they both control the same LED's brightness, I need them to control two different LEDs. I was thinking about using for every module one analog multiplexer like a 4051b (which I already have) and a priority encoder (an MC14532B or an SN74HC148N ): the encoder will recognize which line I'm activating (by changing the potentiometer's state) between the one on the module or the one connected in series to it, and send to the multiplexer's address input the bit value of the corresponding port. The multiplexer will then send the analog value to the board input.

Is this a possible working configuration or am I totally wrong? I also found something about the 74HC595 shift register and about I/O expanders like PCF8574 and mcp23016 but I'm not sure of what they exactly are and do. Could they be a better choice for my project?

am I totally wrong?

Not totally wrong but it is more complex than it needs to be. Simply have a 4051 and wire pots to it. You know what pot you are reading by the value you put out on the three data select pins. Like the schematic for this project:- http://www.thebox.myzen.co.uk/Hardware/MIDI_Footsteps.html Just replace the foot sensors with your pots.

Thank you, but I tought of the more complex method because of

Maurello: some different uses and space problems

So if I need to connnect four of them in series with my method I think I will have more flexibility (every module is identical and can connect with every other with no specific order), instead of having different modules that can connect in just one way: the closest to the 4051 will have one output (connected to the 4051 input Y0) and three extensions, the second one will have one output (connected to the extension on the previous module which is connected to 4051's Y1 input) and two extensions, and so on. This if I understood correctly how the 4051 works: I have to send the digital values to the select pins (A0-A2) from the arduino's digital outputs, it can't detect itself which input I'm activating as the encoder does.

some different uses and space problems

Is not an adequate description. Are you serious about getting help? If so a full explanation is required rather than something vague.

it can't detect itself which input I'm activating as the encoder does.

If you have a pot on the input then there is no way a MC14532B or an SN74HC148N can detect any activation of that pot. These are digital input devices.

Draw a schematic of what you are proposing and we can see if it is feasible.

Well I think a need to reconsider my priorities, let’s put aside the modular thing for the moment.
I’m trying to control the brightness of two LEDs using one potentiometer each, through a CD4051BCN mux (they will be 8 when I will understand what’s wrong). I have attached the schematic (I hope I did it right) and here’s the code

const int analogPin = A0;

int r0 = 0;
int r1 = 0;
int r2 = 0;
int count = 0;
int ledPin[2]={10, 11};

unsigned int analRead[2] = {0,0};
unsigned int analValue[2] = {0,0};
int threshold = 2;

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop() {
  for (count=0; count<2; count++) {
    r0 = bitRead(count,0);
    r1 = bitRead(count,1);
    r2 = bitRead(count,2);
    
    digitalWrite(2,r0);
    digitalWrite(3,r1);
    digitalWrite(4,r2);
    
    analRead[count] = analogRead(analogPin);
    
    if (abs(analRead[count] - analValue[count]) >= 2) {
      analogWrite(ledPin[count], analRead[count]);
      analValue[count] = analRead[count];
    } 
  }
}

Can someone please help me?

I have attached the schematic

That is not a schematic. It is a physical layout diagram.

That code is so convoluted it is almost impossible to follow. Start again, just output the two bit patterns to switch the two pots into the analogue input port, and output the values accordingly. What is this supposed to be doing:-

if (abs(analRead[count] - analValue[count]) >= 2) {
      analogWrite(ledPin[count], analRead[count]);
      analValue[count] = analRead[count];

note that tyhe analogRead returns a number between 0 and 1203 and analogWrite take in a number between 0 and 255, so you can't just read one and output it to the other. Divide the result of the analogRead by four before transferring it to analogWrite.

and what is this?

int threshold = 2;

I can't see threshold making an appearance.

Ground all the unused inputs of the multiplexer.

What’s with folks using obsolete parts? This chip CD4051BCN is not available at digikey or mouser.

Try a currently available part - 10-bit, 8-channel ADC
http://www.digikey.com/product-detail/en/MCP3008-I%2FP/MCP3008-I%2FP-ND/319422
Do 3 SPI.transfer( )s to select the channel and read the data back.
Easy to write a for:loop to do that.
Set up SPI divisor of 4 for 2 MHz operation.

It is a physical layout diagram.

Thanks.

output the two bit patterns to switch the two pots into the analogue input port, and output the values accordingly.

You mean the

r0 = bitRead(count,0);
    r1 = bitRead(count,1);
    r2 = bitRead(count,2);
    
    digitalWrite(2,r0);
    digitalWrite(3,r1);
    digitalWrite(4,r2);

part?

What is this supposed to be doing:- Code: if (abs(analRead[count] - analValue[count]) >= 2) { analogWrite(ledPin[count], analRead[count]); analValue[count] = analRead[count];

I was trying to output the value just when it changes and not always.

Divide the result of the analogRead by four before transferring it to analogWrite.

and what is this? I can't see threshold making an appearance.

You're right, I was a bit in a hurry and didn't notice these errors. I grounded all the unused inputs of the multiplexer, is it a precaution to avoid unwanted readings?

What's with folks using obsolete parts?

Many reasons: I had it at home and didn't want to go buy a different one, I found endless discussions, posts and even an official tutorial on the arduino's forum and website, and the 4051 is an analog multiplexer the MCP3008 is an analog to digital converter which (from my beginner's opinion) are two different things used for different purposes.

Ok, no problem using using something on hand already, I've done the same.

two different things used for different purposes.

Yes & no - you're replacing the analog mux on the arduino with an external mux and using the internal ADC.. MCP3008 replaces both the mux and ADC with another ADC (that is not muxed) and reads in the digital data from SPI vs the internal digital bus. But it does the conversion a lot faster.

Maurello:

It is a physical layout diagram.

Thanks

It was supposed to be a form of mild rebuke, they tend to be useless for letting other people see your circuit as they are impossible to read. A real schematic is a good thing it allows us to see if you are starting off with a correct circuit.

It would also help if you used more descriptive names. analRead and analValue sound too much the same how about currentReading and lastReading those are far more descriptive. Remember this is a compiled language so there is no penalty for long variable names.

I would forget about the arrays for the moment and just write simple code.

By the way a much better way of generating the binary code for the data select lines is shown here. http://www.thebox.myzen.co.uk/Tutorial/Arrays.html