Multiplexing reading and writing to pins

I need to read around 8 flex sensor values and write to 8 servo's connected to an arduino UNO so I decided to use a multiplexer for this as flex sensors require analog reading and arduino uno only has 5 analog pins.

I have looked at the details here: Arduino Playground - HomePage
What I don't understand is how to read/write to the 8 pins the multiplexer gives me.
I don't see what code to write, how can I get the arduino to recognize the 8 pins on the multiplexer, take the flex sensor values and output to servo's.
Regardless of whether or not a multiplexer is necessary for what I want to do here, I would really like to know for likely future use of a multiplexer.

Note: I already know how to use one flex sensor to move a servo, so I don't need to know how to do that.
Code I have for flex and servo's without multiplexer:

#include <Servo.h> 
 
Servo servo1;
 
int flexsensor1 = 0;  // analog pin used to connect the flex sensors

int val1;    // variables to read the values from the analog pins 
 
void setup() 
{ 
  servo1.attach(9);  // attaches the servo on pin 3,5,6,9,10 to the servo object
  
  Serial.begin(9600); //serial communication, just for debugging
} 
 
void loop() 
{ 
  val1 = analogRead(flexsensor1);            // reads the value of the potentiometer (value between 0 and 1023) 
  val1 = map(val1, 600, 680, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  servo1.write(val1);                  // sets the servo position according to the scaled value
  Serial.print("\t flexsensor1 value = " );              //prints the sensor value on the screen         
  Serial.print(val1);
  Serial.print("\n");  
  delay(15);
}

Thankyou in advance

You attach the mux's pins s0, s1 and s2 to 3 Arduino digital pins, and the mux's output pin z to the Arduino analog pin of choice.

Then you set the s0, s1 and s2 pins high and low to make a binary number from 0 to 7 (8 choices), 000, 001, 010, 011 etc, and the mux selects that y pin; each y pin y0 to y7 has one of the 8 sensors. Then an analog read on your single analog pin, will read the currently selected y pin and thus the currently selected sensor.

That's what it looks like to me anyway..

It shows you in your link, and the code therein.
The 3 digital outputs, 0~7, in binary, will select witch of the 8 analog pins to read 0~7

Ok Im am looking into using a multiplexer 8 channel to read analog values of sensors, however the problem I can't seem to understand is that I have 8 sensors, each takes 40mA, each has VCC, GND, and an Analog Data pin. I can see if I had a large enough power supply, I could turn all the sensors on then using a multiplexer collect the data. But the 328 processor (Pro Mini spec says 40mA max Per Pin)

Can I use two multiplexers wired up to the same digital inputs to select the multiplexer value, one multiplexer will be hooked up to the VCC of the sensor and the other will be the ANALOG pin on the sensor, So when I read Y0 of each multiplexer simultaneously It will turn on the sensor and read the value and then go to the next sensor, thus limiting power usage while getting values from all sensors?

however the problem I can't seem to understand is that I have 8 sensors, each takes 40mA,

No.
That 40mA is for when a pin is being used as an output.
When reading an input it takes almost no current at all.

Ok so I want the multiplexer to power on the sensor. So it will be using 40mA, the other multiplexer will just read the value.

Ok so I want the multiplexer to power on the sensor

Then you can not get 40mA through a multiplexer.

You do not need to power the flex sensors independently.

Draw a schematic of what you want to make.

I need to read around 8 flex sensor values

So you have a resistor, and flex sensor wired up across 5V and ground.
The junction is where you read the signal, that goes into the multiplexer. You address which multiplexer input you want to read by the three digital address lines on the multiplexer going to to three digital outputs on your arduino.

This is what Im thinking could work...

Pins 9, 10, & 11 need to be connected to the arduino. It is those PIND that decide what input is switched to the arduino.

I have a similar setup in a project I did. I do not have the link because I am in my iPad but google the words
Myzen midi footsteps
For the full schematic and code. It will be the first hit.

Yea I understand the 4051 9,10,11 need to be connected. Also, I saw your post about the footsteps project, very cool!

So If I have two multiplexers hooked up and 9,10,11 are going to the arduino and the software calls for Y0 to read on Multiplexer A and to write (HIGH) on Multiplexer B. The goal is to power an individual sensor (Multiplexer B) then get the value from the sensor (Multiplexer A). And to cycle through the inputs on the Multiplexers

I have scoured the data sheet of the 4051 and I dont see how many mA each pin can handle, I found somewhere it saying 70mA, but it's not in the datasheet.

Grumpy_Mike, I see that you used a "23016 port expander" what does this do? It looks like it provides power to an individual LED?

The port expander in effect gives you more input output pins for your code to use.

40 mA seems like a LOT for one sensor, especially a resistive sensor (I assume). That's almost enough to power the entire Arduino. Sparkfun's flex sensors are in the 10s of kiloohms range. Do you have a datasheet for your sensors?

Either way, I wouldn't put more than a couple mA through a multiplexer port, and this datasheet from NXP (http://www.nxp.com/documents/data_sheet/74HC_HCT4051.pdf) says their 4051s are limited to +/- 25 mA.

Ok great. So I can just call analogwrite(pin, HiGh) to use the demux feature of the chip?

chalupien:
Ok great. So I can just call analogwrite(pin, HiGh) to use the demux feature of the chip?

No!
To use the chip you set the data select lines to the required input number and call the analog read function.

What if I need to analog write to make the sensor turn on...

chalupien:
What if I need to analog write to make the sensor turn on...

analogWrite won't turn the sensor on: it's on when there's the right voltage on its V pin. If you do want to turn them off, you could toggle 0V / 5V from a LOW / HIGH digital pin. But it would be easier to use a power supply that keeps them on all the time.

If you're concerned about current draw, stick your ammeter in the circuit and see what current your sensor draws.

By the way we seem to have two similar but not necessarily identical questions going in one thread here, from two different members. Bit confusing.....

chalupien:
What if I need to analog write to make the sensor turn on...

Then you need a second set of multiplexers to do this. Wired up to the same data select pins with the input wired to 5V and the sensor and resistors of such a value you do not exceed the maximum current of the multiplexers.
But there is absoloutly no need to do this unless there are curmstances that you are not telling us about.

Thanks everyone for you help and input, and yes it looks like there were 2 questions running on this thread. 'Chalupien' was asking a similar but different question. Regardless, I think I have enough information to set up and code for the next step of my project.
Any link as well specifically to multiplexers and code for it, would be appreciated

Thanks