A multiplexer or demultiplexer enables you to expand the in-and outputs on your Arduino board. The 4051 is an 8 channel analog multiplexer / demultiplexer, thus:
Futhermore, the 4051 is able to work with analog values; in the case of the Arduino, you are able to use the analog inputs with a voltage between 0-5V and route them to an Analog-In Pin on your Arduino.

To select the Pin we would like to read or write, we have to use the three Select Pins (S0, S1 and S2). Each of these pins have to be connected to one digital out pin on the Arduino. Every pin is representing a number (S0 = 1; S1 = 2; S2 = 4) and if we set one of these Select pins to HIGH, the number the pin is representing will be transmitted to the 4051. For example:
It is not possible to read or write more then one pin on the 4051 at the same time, because you can just select one pin at the same time. But you can read and write this pins quite fast. There is no delay needed between selecting and read or writing the pin.


The left image above is an example how to use 9 multiplexer to read 64 analog Inputs just with one Analog-In-Pin on the arduino. The right image above is an example how to use two 4051 (one as demultiplexer and one as multiplexer) in a 8x8 Matrix to check 64 buttons or other digital Inputs just with one digital-In-Pin on the arduino (with the second setup you can just have two buttons on at the same time, otherwise you have to use the first (left) setup).
//////////////////////////////////////////////////////////////////////code example
/* * codeexample for useing a 4051 * analog multiplexer / demultiplexer * by david c. and tomek n.* for k3 / malm hgskola * */ 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 = binr, some times it is so easy void setup(){ pinMode(2, OUTPUT); // s0 pinMode(3, OUTPUT); // s1 pinMode(4, OUTPUT); // s2 digitalWrite(led, HIGH); beginSerial(9600); } 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); //Serial.println(bin[count]); delay (1000); } }
(edited by tomek ness (k3/fhp))