Close

Multiplexer pot reading

A project log for Sample based drum machine

Sample based drum machine

lee-sampsonLee Sampson 11/10/2022 at 15:240 Comments

Wanted to test the speed difference between using a multiplexer to read multiple potentiometers from one analog pin vs having them on individual pins.

Code I used. Reads pot 8 times via multiplexer and prints time, then reads pot 8 times the normal way and prints time.

#define number_of_mux 1
#define numOfMuxPins number_of_mux * 8
#define channelA 2
#define channelB 3
#define channelC 4
#define comPin 14

int analogVal[8];
unsigned long timer; 


void setup() 
{

  Serial.begin(9600);

  pinMode(channelA, OUTPUT);
  pinMode(channelB, OUTPUT); 
  pinMode(channelC, OUTPUT); 
  pinMode(comPin, INPUT);
  digitalWrite(channelA, LOW);
  digitalWrite(channelB, LOW);
  digitalWrite(channelC, LOW);

  MuxRead();
  //delay(4000);

  normalRead();
  //delay(4000);

}

void loop() 
{

}

void selectChannel(int chnl)
{ //// Select channel of the multiplexer 

  int A = bitRead(chnl,0); //Take first bit from binary value of i channel.
  int B = bitRead(chnl,1); //Take second bit from binary value of i channel.
  int C = bitRead(chnl,2); //Take third bit from value of i channel.

  digitalWrite(channelA, A);
  digitalWrite(channelB, B);
  digitalWrite(channelC, C);
 
}

void MuxRead()
{
  timer = micros();

  for(int i = 0; i <  numOfMuxPins; i++)
  {
    selectChannel(i);
    analogVal[i] = analogRead(comPin);
  }

  timer = micros() - timer;
  Serial.print("muxRead = ");
  Serial.println(timer);
  Serial.println("**************************************");
}

void normalRead()
{
  timer = micros();

  for(int i = 0; i <  numOfMuxPins; i++)
  {
    analogVal[i] = analogRead(comPin);
  }

  timer = micros() - timer;
  Serial.print("normalRead = ");
  Serial.println(timer);
  Serial.println("**************************************");
}

Results (in micros):

muxRead = 141
**************************************
normalRead = 140
**************************************

 Multiplexer schematic:

Discussions