Multiplexers/Arduino/PureData – Part 1

One of the biggest problems with the Atmel328 chips, at least that I have encountered, is that they only offer six analog inputs. What if I need input from eight potentiometers? This problem is easily(?) solved with the use of Multiplexers (MUX). Multiplexers are fairly cheap, and can be found here.  You can also get the chip sans breakout from Sparkfun. Once you have said MUX, how do you use it? That is the purpose of this tutorial….

The MUX needs to be programmed via your Atmel chip, or Arduino. To do that, attach leads from Digital pins 2,3,4,5 on the Arduino, to inputs S0, S1, S2, S3 on your MUX. Like So…IMG_0008

Pay attention to the +V/Gnd connections and the resistor from ‘SIG’ to gnd!

Now that the physical connections have been made, the MUX needs to be programmed. This is done through the Atmel chip and the Arduino IDE. The code that I have used, for ten potentiometers, is as follows:
//////////////////////////// MUX to Atmel 328 code for Arduino IDE  ///////////////////////////
//Control pins (These are your digital outs from the Arduino)
#define CONTROL0 5    
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

//Create an array for data from the the MUX
int mux0array[10]; //sets number of inputs from MUX

void setup()
{
  //Set MUX control pins to output
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
 
  //Open the serial port at 57600 bps
  Serial.begin(57600);
}

bool humanreadable = false;  

void loop()
{
  //This for loop is used to scroll through and store the 16 inputs on the multiplexer
  for (int i=0; i<10; i++) //see bitwise operators in Arduino reference
  {
    digitalWrite(CONTROL0, (i&15)>>3);
    digitalWrite(CONTROL1, (i&7)>>2);  
    digitalWrite(CONTROL2, (i&3)>>1);  
    digitalWrite(CONTROL3, (i&1));     
    
    //Read and store the input value at a location in the array
    mux0array[i] = analogRead(0);
    
    //Reads common pin and sends out over serial
    if(humanreadable)
    {
      Serial.print(analogRead(0), DEC);  
    }
    else
    {
       byte portData = analogRead(0);
       Serial.print(portData & B01111111, BYTE); // Tx bits 0-6
       Serial.print(portData >> 7 & B01111111, BYTE);  // Tx bits 7-13
    }
  }
 

  //These lines parse data from the array into sizes that can be used by Pd
  //You probably don’t need the ‘for loop’ here, as it only loops every ‘one’ times,
  //but it was part of the code that I borrowed from, so I kept it in place. Either way, it works.
  //If you want to check your data in the Arduino Serial Monitor, simply uncomment the
  //Serial.print lines. However, make sure to disable these lines before uploading for final use.

  for (int i=0; i<1; i++)
  {
    //assigns values to each MUX input
    mux0array[0] = map(mux0array[0], 0, 961, 0, 25);
    Serial.write(mux0array[0]);
    //Serial.print(‘ ‘);
    mux0array[1] = map(mux0array[1], 0, 961, 26, 50);
    Serial.write(mux0array[1]);
    //Serial.print(‘ ‘);
    mux0array[2] = map(mux0array[2], 0, 961, 51, 75);
    Serial.write(mux0array[2]);
    //Serial.print(‘ ‘);
    mux0array[3] = map(mux0array[3], 0, 961, 76, 100);
    Serial.write(mux0array[3]);
    //Serial.print(‘ ‘);
    mux0array[4] = map(mux0array[4], 0, 961, 101, 125);
    Serial.write(mux0array[4]);
    //Serial.print(‘ ‘);
    mux0array[5] = map(mux0array[5], 0, 961, 126, 150);
    Serial.write(mux0array[5]);
    //Serial.print(‘ ‘);
    mux0array[6] = map(mux0array[6], 0, 961, 151, 175);
    Serial.write(mux0array[6]);
    //Serial.print(‘ ‘);
    mux0array[7] = map(mux0array[7], 0, 961, 176, 200);
    Serial.write(mux0array[7]);
    //Serial.print(‘ ‘);
    mux0array[8] = map(mux0array[8], 0, 961, 201, 225);
    Serial.write(mux0array[8]);
    //Serial.print(‘ ‘);
    mux0array[9] = map(mux0array[9], 0, 961, 226, 255);
    Serial.write(mux0array[9]);
    //Serial.print(‘ ‘);
    //Serial.print(“-“);
  }
  //Serial.println();  //line feed – for checking values in the Serial Monitor in Arduino IDE
  //Serial.println();
  delay(50);

}
///////////////////End Code//////////////////////

This should give you ten inputs from your MUX that can be read over the comport in Pure Data. To learn how this works, see my tutorial “Multiple Analog Values from Arduino to Pure Data.” If you want to increase the amount of inputs from the MUX, simply increase the size of the array, for example, from [10] to [16].