Help with multiplexer (74HC4051)

I am using an IC that has a data input of 8 bit. Instead of using 8 pins on my Arduino I want to use a multiplexer.
So I connected it in the same way it's here: http://www.arduino.cc/es_old/Tutoriales/74HC4051
but instead of doing a analogRead at the end, I do a digitalWrite to the "COM OUT/IN" pin on the multiplexer. And when trying to write a certain 8 bit value it doesn't work.

I did a test reading a control pin on the multiplexer outputs but it's not even stable, it's erratic.

Is it possible to use this multiplexer in the way I want? What's wrong????

Thanks!

And when trying to write a certain 8 bit value it doesn't work.

I don't understand, you can't write an 8 bit value with a multiplexer. You can read 8 bits one bit at a time.
The code in that link is appalling.
Here is how to use and array:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html
This is a project where I used a multiplexer, the code is included.
http://www.thebox.myzen.co.uk/Hardware/MIDI_Footsteps.html

oh... ok, so a multiplexer cannot be used to write?
There is the problem then, hehe. I thought it could be used for reading as well as writing.
I would need to use a shift register then, right?

Cool project by the way!

No, wait... you can write on it.

On the datasheet (Philips 0uo046lw3xgikpf1d0zg15wpefpy datasheet pdf) it says:

The 74HC/HCT4051 are 8-channel analog
multiplexers/demultiplexers with three digital select inputs
(S0to S2), an active LOW enable input (E), eight
independent inputs/outputs (Y0to Y7) and a common
input/output (Z).

What I want to do is write on 8 different pins by using the multiplexer, one at a time by writing each time the selector value and then the pin value. Am I that wrong?

It's an "8-channel" multiplexer, not an "8-bit" multiplexer, which implies something
different [although I'm not sure what that would even be].

You can connect only 1 channel at a time, and it doesn't latch, so as soon as you
switch to another channel, the signal to the previous channel will no longer
be valid [ie, floats]. You can use it to read input lines.

If you want to write to 8 pins you will want to use an 8-bit latch, which will have
3 pins for channel select, plus a Din pin [hi/low].

It is not simple to both read and write to 8 pins, to do that you will need a latch
with ability to set the output channels to hi-Z, as well as a another chip to
do the reads. Even worse, most latches will not allow you set individual pins
to hi-Z, but do all pins at the same time.

If you look at the info on the I/O pins in the mega328 d/s, you'll see they have a
lot of circuitry for individual pin control. You would have to emulate something
like that in order to allow individual pins to be either input or output at the
time.

oooohhhhhhh... I see.
So a shift register it is.

Thanks for the clarification.

A shift register will work, although you want to be careful that
you're not sending flukey signals to the output pins as you're shifting
the 8-bits through the device.

I've tinkered a little with 4051 chips and had to put a high value resistor on the control pins to ground to prevent them from floating, causing some erratic behavior. Below is a 16 channel version.

Only a total idiot would use a 4051 to write data to each of its outputs. There are a lot of idiots on the web.
The example you showed was reading LDRs.

Normally the shift registers are latched so you don't have to worry about values changing as you shift them along.

I have used the 16-channel 4067 with good success to allow a high-accuracy, single channel ADC make its readings. Another option would have been using multiple ADCs but there were board space considerations. Plus, with 16 input channels and only a need for 11 inputs, I could set aside 5 channels for reference resistors - thus allowing calibration and verification.

Thanks all for the help!
But now I need more :slight_smile:

I am using the 74HC4051 multiplexor and I connected 3 buttons and 2 pots to it.
I am getting very erratic readings on the pots, it might be accurate enough to use it for buttons, but not for the pot.
For example, I select channel 0 (0,0,0) for reading A0 wich is connected to the pot, I spin the pot to the maximum value and the output is this:

pitchRead: 1014.00
pitchRead: 1013.00
pitchRead: 1014.00
pitchRead: 1013.00
pitchRead: 1013.00
pitchRead: 1014.00
pitchRead: 1013.00
pitchRead: 1014.00
pitchRead: 1023.00
pitchRead: 26.00
pitchRead: 24.00
pitchRead: 25.00
pitchRead: 26.00
pitchRead: 40.00
pitchRead: 1013.00

Also, if I press one of the buttons the reading is higher, if I press the 3 buttons at the same time more.
Maybe I need to use one multiplexor for the pots and another for the buttons???

Why the sudden drop on the reading?

Why the sudden drop on the reading?

Do your 4051 control pins float when not actuated?

I'm sorry but what do you mean when not actuated?

As a test I connected every input from the multiplexor to the ground except a potentiometer.

*When I select one of the ground channels (A1-A7) it gives a reading of 47-49 when the pot is at maximum or at minimum, it's affecting it very little to nothing.
*When I select the pot channel (A0) and set the pot to minimum it gives me this:

pitchRead: 47.00
pitchRead: 48.00
pitchRead: 48.00
pitchRead: 48.00
pitchRead: 49.00
pitchRead: 70.00
pitchRead: 73.00
pitchRead: 46.00
pitchRead: 46.00
pitchRead: 47.00
pitchRead: 53.00
pitchRead: 56.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 49.00
pitchRead: 71.00
pitchRead: 76.00
pitchRead: 50.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 80.00
pitchRead: 62.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 48.00
pitchRead: 48.00

And when I spin the pot at maximum it gives me this:

pitchRead: 973.00
pitchRead: 974.00
pitchRead: 973.00
pitchRead: 974.00
pitchRead: 971.00
pitchRead: 965.00
pitchRead: 962.00
pitchRead: 46.00
pitchRead: 69.00
pitchRead: 54.00
pitchRead: 46.00
pitchRead: 46.00
pitchRead: 47.00
pitchRead: 47.00
pitchRead: 62.00
pitchRead: 969.00
pitchRead: 973.00
pitchRead: 974.00
pitchRead: 975.00
pitchRead: 975.00
pitchRead: 974.00
pitchRead: 975.00
pitchRead: 976.00
pitchRead: 964.00
pitchRead: 975.00
pitchRead: 974.00
pitchRead: 974.00
pitchRead: 974.00
pitchRead: 974.00
pitchRead: 973.00
pitchRead: 973.00
pitchRead: 974.00
pitchRead: 973.00
pitchRead: 973.00
pitchRead: 974.00
pitchRead: 974.00
pitchRead: 974.00
pitchRead: 973.00
pitchRead: 974.00
pitchRead: 47.00
pitchRead: 975.00
pitchRead: 974.00
pitchRead: 975.00
pitchRead: 974.00
pitchRead: 974.00
pitchRead: 974.00
pitchRead: 974.00

I am constantly selecting the cannel in the loop.

Are you using a three-legged potentiometer as described here? If not, that could be your problem. A pot that doesn't feature a wiper + two other terminals will require a drop-down resistor of the right value to work as expected.

yes, I am using a pot like that.
It's working fine if it's connected directly to an Arduino pin

OK, and you are actively driving the right pins LOW? Have you verified that all input selector pins show zero volts continuously?

I've noticed that using or not the multiplexer when I press any button connected on the board the reading gets a little higher, that might be interference with the board I'm using, is not really a problem at this point.

But the weird thing is that it seems that the multiplexer every time in a while changes the channel it's reading

I'm sorry but what do you mean when not actuated?

From past discussions:

  1. Unpredictable outputs may occurr with a 4051 type chip (this is the chip I have experimented with) when the input control lines (A, B, C, and D in the post) are either not driven at ~5v input or not connected to ground.
  2. If the arduino digital output pins do not sink to ground when in the "low" state, then the control lines to the multiplex chip may "float" and produce undesired results.

Old post showing a possible parallel setup instead of using a shift regester:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1227132571/22

OK, and you are actively driving the right pins LOW? Have you verified that all input selector pins show zero volts continuously?

I feel so stupid... The problem was that I didn't defined the selectorPins as output, so they were erratic...

Thank you so much you all!!!

EDIT: It's working perfectly!