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 than one pin on the 4051 at the same time, because you can only select one pin at a time. But you can read and write to the pins quite fast. There is no delay needed between selecting and read or writing the pin.
//////////////////////////////////////////////////////////////////////code example
/* * codeexample for useing a 4051 * analog multiplexer / demultiplexer * by david c. and tomek n.* for k3 / malm� h�gskola * * edited by Ross R. * edited by Igor de Oliveira Sá. */ 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 s0 = 2; int s1 = 3; int s2 = 4; int count = 0; //which y pin we are selecting void setup(){ pinMode(s0, OUTPUT); pinMode(s1, OUTPUT); pinMode(s2, OUTPUT); } 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) //r0 = count & 0x01; // old version of setting the bits //r1 = (count>>1) & 0x01; // old version of setting the bits //r2 = (count>>2) & 0x01; // old version of setting the bits digitalWrite(s0, r0); digitalWrite(s1, r1); digitalWrite(s2, r2); //Either read or write the multiplexed pin here } }
(edited by tomek ness (k3/fhp))