Trying to get 4051 Mux/ Demux setup working -- HELP PLEASE


I’m fairly new at Arduino / and am trying to learn by doing. My current project is – I am trying to build an application that reads about 20+ digital buttons. I thought I would try to get a 4051 Mux/ Demux setup working with a series of 74HCT4051N’s to experiment with.

My objective is to fully understand how to read (4) four momentary switches (and identify them from each other) and armed with that I can expand on my own.

I am struggling with getting the basic understanding. I have built the attached circuit.

Trying to learn from examples below – but they are confusing me quite a bit.

They are confusing me a good deal.

I understand that the muiltplexer / demux setup sequerntially runs throught the switches is suppose to reads the value for each button. I haven’t got that to work for me.

Any pointers to get me headed in the right direction would be super apprecaited.

  • codeexample for useing a 4051 * analog multiplexer / demultiplexer
  • by david c. and tomek n.* for k3 / malmö högskola

int led = 13; //just a led
int r0 = 0; //value select pin at the 4051 (s0)
int r1 = 0; //value select pin at the 4051 (s1)
int r2 = 0; //value select pin at the 4051 (s2)
int row = 0; // storeing the bin code
int count = 0; // just a count
int bin = {000, 1, 10, 11, 100, 101, 110, 111};//bin = binär, some times it is so easy
void setup(){
pinMode(2, OUTPUT); // s0
pinMode(3, OUTPUT); // s1
pinMode(4, OUTPUT); // s2
digitalWrite(led, HIGH);
void loop () {
for (count=0; count<=7; count++) {
row = bin[count];
r0 = row & 0x01;
r1 = (row>>1) & 0x01;
r2 = (row>>2) & 0x01;
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
delay (1000);

4051_test.fzz (15.9 KB)

Hi, and welcome.

You're showing a Fritzing sketch you say you built.
Did you do it exactly the way you're showing in the sketch ?
So just what's in there and nothing else ?
I'm asking because i see some errors in there, just watching that Fritzing sketch (so the hardware).
First, in that sketch, there is no power supply to the 4051 chips.
They need power.
2nd. you have wired the buttons to pins 1, 2, 3, and 4 of one of those chips.
I have to assume that the small PCB's the chips are mounted on, have a 1 to 1 connections from chip to PCB pins.
So i took a peek and got me the NXP datasheet.
That datasheet tells me pin 3 is the output of that chip.
You want to connect that to your Arduino input.
The first 4 inputs of the chip are on pins 13, 14, 15, and 12 respectively.
Pins 11, 10 and 9 are the select pins.
Those are the ones you should control by your Arduino and select the input you want to read.

I see a connection from pin 9 of the lower PCB to pin 1 of the upper PCB.
I guess you want to cascade, but you can't use these pins for this.
I can also see some connection from pin 1 to pin 5 on the lower PCB, but i guess that isn't really there, is it ?

Last, but certainly not least:
You have put a resistor at each pushbutton.
The value printed on it is 100 Ohms (brown, black, black).
That is a bit low as a pull down resistor, the most likely reason for those resistors to be there.
A 10 K (brown, black, orange) will do and will reduce current flow drastically.
You also need to check you are connecting the switches correct.
If they are by accident rotated 90 degrees (clockwise or counterclockwise), you can never see the switch switching and the resistor is permanent connected between the power rails.

This was what i can see by just looking at the hardware.
I just also had a quick look at the code.
You seem to scan by selecting different inputs (after you have checked your connections).
But that's all you seem to do:
Select inputs of the 4051, but then nothing...

If you post code, put that in code tags.
And post all of it, or else you keep us guessing about what's supposed to happen.

Expanded your schematic a little. 3 select lines will choose 1 of 8 inputs on each bank, 2 other select lines will choose one of the 3 banks.
Drive the select lines, wait 300uS, read the input. Can be a digital input line, you’re looking for a Low input to determine a pressed button.

Ok those last two reponses helped me a great deal – I was confused by the logic symbol versus pin configuration and definately had a newbie confusion about the powering the chip too…

I’ve taken ONE STEP BACK to make sure I can read the results of just one 4051 chip. If I can get that right then I will take the next step of reading more than one value on the same chip and then expand to nesting 4051’s (more than one chip)

I think I have the first 4051 chip properly wired now. See attached Fritzing sketch.

I think I’ve got a simple push button circuit that should be able to send one signal to the y0 on the 4051 and then I want to send that through the Z to the Arduino Uno’s A0 pin

HERE is my code

int r0 = 0; //value of select pin at the 4051 (s0)
int r1 = 0; //value of select pin at the 4051 (s1)
int r2 = 0; //value of select pin at the 4051 (s2)
int count = 0; //which y pin we are selecting

void setup(){

pinMode(2, OUTPUT); // s0
pinMode(3, OUTPUT); // s1
pinMode(4, OUTPUT); // s2
pinMode(A0, INPUT); // z


void loop () {

for (count=0; count<=7; count++) {

// select the bit

r0 = bitRead(count,0); // use this with arduino 0013 (and newer versions)
r1 = bitRead(count,1); // use this with arduino 0013 (and newer versions)
r2 = bitRead(count,2); // use this with arduino 0013 (and newer versions)

digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);

//Either read or write the multiplexed pin here



Now the results that I expected to get was either a “0” or a “1” respectively.

The results I am getting is a repeating “14” ? This doesn’t make any sense to me?

I am struggling to read the pin value (0 or 1) – any help would be greatly appreciated!

4051_test_2.fzz (8.27 KB)

post a .pdf .jpg .png something for those without fritzing.

here is a the image to go with the frizing file

Ok that is useless as it gives no clue as to functionality desired. might as well be two black boxes with wires in between.


The only functionality I am trying to achieve with this one is to get a "0" or "1" value (from the button press) through y0 of the 4051 chip along through it's z -- and into the Arduino's A0.

A 4051 with pin functions provides no info as to what the pins do. the svhematic I posted makes it very clear what is intended.

You did not connect that orange wire to Y0.
The other orange wire doesn't do anything different when you press that button.
Also, what's with the LED ?

Put code in code tags please.