how to get more arduino analog inputs

08 Feb 2016

This was the most popular post in my previous site so keeping it love here in case it is still useful.

I needed extra analog inputs for an arduino project and it seems a multiplexer was the way to go.. It took a little while to sift through a heap of information on using a multiplexer with the arduino, and in the end I grabbed some 4051 ICs from Jaycar..

The 4051 allows you to choose between 8 analog inputs, reading them from just on arduino analog pin. (It can also be used in reverse, adding 8 analog outputs, but I am only interested in extra inputs for the moment)

** To hook it up to the arduio make the following connections: (the arduino pins are relatively arbitrary, though they are the ones used in the example code)

pin 3 : an arduino analog pin –  eg:  ANALOG IN PIN 0
pin 6,7 & 8 : all go to the arduino’s ground (Gnd)
pin 9 (s0): to arduino digital pin 10
pin 10 (s1): to arduino digital pin 9
pin 11 (s2): to arduino digital pin 8
pin 16 : to arduino +5V

** The rest of the pins on the 4051 (8 of them) are for the anolog inputs, as follows:

pin 1 : anolog input 4
pin 2 : anolog input 6
pin 4 : anolog input 7
pin 5 : anolog input 5
pin 12 : anolog input 3
pin 13 : anolog input 0
pin 14 : anolog input 1
pin 15 : anolog input 2

4051 arduino schematic

** Now in the arduino code you select which of the 8 analog inputs to read through arduino pin 1 but setting s0 , s1 and s2 to either low or high.

** S0 = 1 , S1 = 2 and s2 = 4

** so to read from analog input 0 (4051 pin 13) you would set s0,s1&s2 to low (0 + 0 + 0)

** to read from analog input 1 (4051 pin 14) you would set s0 to high and s1&s2 to low (1+0+0)

**and so on…
input 2 (pin 15):  L,H,L  (0+2+0)
input 3 (pin 12):  H,H,L (1+2+0)
input 4 (pin 1):  L,L,H (0+0+4)
input 5(pin 5):  H,L,H (1+0+4)
input 6 (pin 2):  L,H,H (0+2+4)
input 7 (pin 4): H,H,H (1+2+4)

** so while there is some fancy code around to read all of the inputs one after another in a for loop (seehttp://www.arduino.cc/playground/Learning/4051 it was this code that I found most confusing in understanding how the 4051 worked)

** all you really need to do is set the three arduino digital outputs to the correct setting for the analog input you want to read, then do an analogRead of pin 0

for example:

void setup()

{

  //4051 digital control pins

  pinMode(8, OUTPUT);    // s0

  pinMode(9, OUTPUT);    // s1

  pinMode(10, OUTPUT);    // s2

  Serial.begin(9600);

}

void loop()

{

  //Read Value of 4051 analog-in 0 by setting the values of s0,s1 and s2

  digitalWrite(8, LOW);

  digitalWrite(9, LOW);

  digitalWrite(10, LOW);

  delay(10); //not sure if this delay is strictly necessary

  int readInZero = analogRead(0);  // read the input pin

  Serial.print(readInZero); //use the result

   //repeat to read other pins (in this case analog in 2)

   digitalWrite(8, LOW);

   digitalWrite(9, HIGH);

   digitalWrite(10, LOW);

   delay(10);

   int readInTwo= analogRead(0);

   Serial.print(readInTwo);

   //etc. for the other 6 4051 analog inputs

}