How to use a multiplexer to read in voltages accuratlely

Hey everyone, so I am using a cd74hc4067 multiplexer with a Arduino uno to try and read in different voltages and different control pins. Right now i have been trying to see if i can get a accurate reading from just one control pin. i first made a very simple voltmeter using two resistors with known values to try a spit out the Vout into the serial montior with very good results. Here is the code for the first simple voltmeter i made

int analogInput = A5;
float Vout = 0.00;
float Vin = 0.00;
float R1 = 1000.00; // resistance of R1 (1K) 
float R2 = 220.00; // resistance of R2 (220) 
int val = 0;
void setup(){
   pinMode(analogInput, INPUT); //assigning the input port
   Serial.begin(9600); //BaudRate
}
void loop(){
   
   val = analogRead(analogInput);//reads the analog input
   Serial.print("Initial Value being read in as = ");
   Serial.print(val);
   Serial.print("\n");
   Vin = (val * 5.00) / 1024.00; // formula for calculating voltage out i.e. V+, here 5.00
  // Vin = Vout / (R2/(R1+R2)); // formula for calculating voltage in i.e. GND
   if (Vin<0.09)//condition 
   {
     Vin=0.00;//statement to quash undesired reading !
  } 
Serial.print("\t Voltage of the given source = ");
Serial.print(Vin);
Serial.print("\n");
delay(500); //for maintaining the speed of the output in serial monitor
}

I tried then to just add a multiplexer with control pin 0 to try and read in the voltage from the voltmeter I had just made to see if it could take it in accurate and for some reason it is just way off. My thought is that it would be voltage loss from the multiplexer which is why it is not reading it in accurately. I eventually would like to read in multiple different voltage values from other control pins to see if i can use this to make a sensor pad with changing resistor values but so far it has been a struggle. Here is my current code to try and see if anyone can help

//Mux control pins for analog signal (SIG_pin) default for Arduino Uno
const byte s0 = A4; // pin A4 in the arduino (analog in)
const byte s1 = A3; // pin A3 in the arduino
const byte s2 = A2; // pin A2 in the arduino
const byte s3 = A1; // pin A1 in the arduino
//Mux in "SIG" pin default for Arduino Uno
const byte SIG_pin = A0;

const boolean muxChannel[2][4] = {
  {0, 0, 0, 0}, //channel 0
  {1, 0, 0, 0}, //channel 1
  //  {0, 1, 0, 0}, //channel 2
  //  {1, 1, 0, 0}, //channel 3
  //  {0, 0, 1, 0}, //channel 4
  //  {1, 0, 1, 0}, //channel 5
  //  {0, 1, 1, 0}, //channel 6
  //  {1, 1, 1, 0}, //channel 7
  //  {0, 0, 0, 1}, //channel 8
  //  {1, 0, 0, 1}, //channel 9
  //  {0, 1, 0, 1}, //channel 10
  //  {1, 1, 0, 1}, //channel 11
  //  {0, 0, 1, 1}, //channel 12
  //  {1, 0, 1, 1}, //channel 13
  //  {0, 1, 1, 1}, //channel 14
  //  {1, 1, 1, 1} //channel 15
};
float Vout = 0.00;
float Vin = 0.00;
float R1 = 1000.00; // resistance of R1 (1K)
float R2 = 220.00; // resistance of R2 (220)
int val = 0;
int calibra[4][4];

void setup() {
  // put your setup code here, to run once:
  //Setting all the pins to function as a input or output
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(SIG_pin, OUTPUT);

  //Sets pins to what they will start as either on or off
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  Serial.begin(9600);

  //Prints out a message on the Serial Montior
  Serial.println("\n\Starting Up...\n");

  // Fills the initial matrix with a bunch of 0's
  for (byte j = 0; j < 4; j ++) {
    writeMux(j);
    for (byte i = 0; i < 4; i ++)
      calibra[j][i] = 0;
  }
}

//Repeats code continuously
void loop() {
  for (byte j = 0; j < 1; j ++) {
    val = writeMux(j);
    Serial.print("Value initially taken in is ");
    Serial.print(val);
   Vin = (val * 5.00) / 615.00; // formula for calculating voltage out i.e. V+, here 5.00
    //Vin = Vout / (R2 / (R1 + R2));
    Serial.print(", The Voltage reading at Control pin ");
    Serial.print(j);
    Serial.print(" = ");
    Serial.print(Vin);
    Serial.print("\n");
    delay(500);
  }

}

int writeMux(int channel) {
  int controlPin[] = {s0, s1, s2, s3};

  //loop through the 4 sig
  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }
  //read the value at the SIG pin
  int value = analogRead(SIG_pin);

  //return the value
  return value;
}

////Used for Reading what is currently set to the Control SIG pins
//int readMux(int channel){
//  int controlPin[] = {s0, s1, s2, s3};
//  //loop through the 4 sig
//  for(int i = 0; i < 4; i ++){
//    digitalWrite(controlPin[i], muxChannel[channel][i]);
//  }
//
//  //read the value at the SIG pin
//  int val = analogRead(SIG_pin);
//
//  //return the value
//  return val;
//}

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.

1 Like

So you are making A0 an output pin, how will this work ? :thinking:

This is an analog problem, words do not help anywhere as good as a real annotated schematic.

Sorry about not showing a schematic diagram of the circuit.


In the diagram the two hanging wires off the breadboard are for a voltage generator which will vary from 1V - 5V respectively. If there is any other way i can help let me know.

This voltage generator needs to be referenced to the Arduino GND.

gotcha, also the sig_pin that was set to output was screwing up the code a bit. so thanks for pointing that out.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.