{SOLVED}HELP i cant figure out how i use this code for the 4051 multiplexer

Hello,

im quite new to this so i dont really understand it but im working on a project and i have to read out 8 analog sensors but the arduino that i have to use only has 5 analog pins, so i looked around and i found something called a multiplexer but i cant figure out how i use this. i found some example code but i cant figure out how it works and how i can get my readings for it.

im referring to this example code:

 /*
 * 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

  }  
}

i found it on this page: Arduino Playground - HomePage

and this is a example of the code where im doing my readings from:

int Vibsensor;
int readU1;
int readV1;
int readW1;
int readU2;
int readV2;
int readW2;
int readDC;


void setup()
{
	Serial.begin(9600);      //Only for debugging
	Serial.println("TEST");
}

void loop()
{

	readU1 = analogRead(A0);
	get_tempU1();
	readV1 = analogRead(A1);
	get_tempV1();
	readW1 = analogRead(A2);
	get_tempW1();
	readU2 = analogRead(A3);
	get_tempU2();
	readV2 = analogRead(A4);
	get_tempV2();
	readW2 = analogRead(A5);
	get_tempW2();
	readDC = analogRead(A6);
	get_tempDC();
	Vibsensor = analogRead(A7);
	get_Vibration();
	
	delay(8000);
}

//PowerSupply U1 pin: A0 
void get_tempU1()
{

	float tempU1C = readU1 * 0.217226044 - 61.1111111;
	float tempU1F = tempU1C + 273.15;
	Serial.println("           ");
	Serial.println("****PowerSupply U1****");
	Serial.println("Temp: ");
	Serial.print(tempU1C);
	Serial.print(" °C");


	Serial.println("           ");
	Serial.println("Fahr: ");
	Serial.print(tempU1F);
	Serial.print(" °F");
	Serial.println("           ");
	Serial.println("**********************");
	Serial.println("           ");
	delay(1000);
}

//PowerSupply V1 pin: A1
void get_tempV1()
{
	float tempV1C = readV1 * 0.217226044 - 61.1111111;
	float tempV1F = tempV1C + 273.15;
	Serial.println("           ");
	Serial.println("****PowerSupply V1****");
	Serial.println("Temp: ");
	Serial.print(tempV1C);
	Serial.print(" °C");


	Serial.println("           ");
	Serial.println("Fahr: ");
	Serial.print(tempV1F);
	Serial.print(" °F");
	Serial.println("           ");
	Serial.println("**********************");
	Serial.println("           ");
	delay(1000);
}

void get_Vibration()
{
	
	//While sensor is not moving, analog pin receive 1023~1024 value
	if (Vibsensor < 1022) {
		
		Serial.print("Sensor Value: ");
		Serial.println(Vibsensor);
	}

	else {
		
		Serial.print("Sensor Value: ");
		Serial.println(Vibsensor);
	}


	delay(100); //Small delay
}

//PowerSupply W1 pin: A2
void get_tempW1() {

	float tempW1C = readW1 * 0.217226044 - 61.1111111;
	float tempW1F = tempW1C + 273.15;

	Serial.println("           ");
	Serial.println("****PowerSupply W2****");
	Serial.println("Temp: ");
	Serial.print(tempW1C);
	Serial.print(" °C");


	Serial.println("           ");
	Serial.println("Fahr: ");
	Serial.print(tempW1F);
	Serial.print(" °F");
	Serial.println("           ");
	Serial.println("**********************");
	Serial.println("           ");

	delay(1000);
}


//Drive U2 pin: A3
void get_tempU2() {

	
	float tempU2C = readU2 * 0.217226044 - 61.1111111;
	float tempU2F = tempU2C + 273.15;
	Serial.println("           ");
	Serial.println("*******Drive U2*******");
	Serial.println("Temp: ");
	Serial.print(tempU2C);
	Serial.print(" °C");


	Serial.println("           ");
	Serial.println("Fahr: ");
	Serial.print(tempU2F);
	Serial.print(" °F");
	Serial.println("           ");
	Serial.println("**********************");
	Serial.println("           ");
	

	delay(1000);
}


//Drive V2 pin: A4
void get_tempV2() {

	
	float tempV2C = readV2 * 0.217226044 - 61.1111111;
	float tempV2F = tempV2C + 273.15;
	Serial.println("           ");
	Serial.println("*******Drive v2*******");
	Serial.println("Temp: ");
	Serial.print(tempV2C);
	Serial.print(" °C");


	Serial.println("           ");
	Serial.println("Fahr: ");
	Serial.print(tempV2F);
	Serial.print(" °F");
	Serial.println("           ");
	Serial.println("**********************");
	Serial.println("           ");
	

	delay(1000);
}


//Drive W2 pin: A5
void get_tempW2() {

	float tempW2C = readW2 * 0.217226044 - 61.1111111;
	float tempW2F = tempW2C + 273.15;
	Serial.println("           ");
	Serial.println("*******Drive W2*******");
	Serial.println("Temp: ");
	Serial.print(tempW2C);
	Serial.print(" °C");


	Serial.println("           ");
	Serial.println("Fahr: ");
	Serial.print(tempW2F);
	Serial.print(" °F");
	Serial.println("           ");
	Serial.println("**********************");
	Serial.println("           ");

	delay(1000);
}


//DC Link pin: A6
void get_tempDC() {
	
	float tempDCC = readDC * 0.217226044 - 61.1111111;
	float tempDCF = tempDCC + 273.15;

	Serial.println("           ");
	Serial.println("******* DCLink *******");
	Serial.println("Temp: ");
	Serial.print(tempDCC);
	Serial.print(" °C");


	Serial.println("           ");
	Serial.println("Fahr: ");
	Serial.print(tempDCF);
	Serial.print(" °F");
	Serial.println("           ");
	Serial.println("**********************");
	Serial.println("           ");

	delay(1000);
}

Markf2017:
Hello,

im quite new to this so i dont really understand it but im working on a project and i have to read out 8 analog sensors but the arduino that i have to use only has 5 analog pins, so i looked around and i found something called a multiplexer but i cant figure out how i use this. i found some example code but i cant figure out how it works and how i can get my readings for it.

Ok, so if you look at a datasheet for the 4015 this is the pin description:

so of the Arduino you wire up E,S1,S2 and S3 to the digital IO and Z to one of the analog inputs.
Your sensors analog outputs to Y0 through Y7 (if using 8 sensors that is)

so in code probably something like this:

//define digital pin used to connect to 4051
#define S1 4
#define S2 5
#define S3 6
#define EN 7
uint8_t sensor_select = 0;

void setup() {

  Serial.begin(115200);
  
  //initialise digital pins
  pinMode(S1, OUTPUT);
  digitalWrite(S1, LOW);
  pinMode(S2, OUTPUT);
  digitalWrite(S2, LOW);
  pinMode(S3, OUTPUT);
  digitalWrite(S3, LOW);
  pinMode(EN, OUTPUT);
  digitalWrite(EN, HIGH);

  Serial.println("READY!");
}

void loop() {
  uint16_t x;

  digitalWrite(EN, LOW); //enable output Z
  analogRead(A0); //output Z connected to A0
  x = analogRead(A0);
  digitalWrite(EN, HIGH); //diaable output Z

  Serial.print("MUX Input ");
  Serial.print(sensor_select, DEC);
  Serial.print(" selected, readout: ");
  Serial.println(x, DEC); //raw value printed out

  sensor_select = (sensor_select + 1) % 8; //increment selection 0-7 cyclicly

  //update select inputs based on "sensor_select" value
  digitalWrite(S1, (sensor_select & 0x01));
  digitalWrite(S2, ((sensor_select >> 1) & 0x01));
  digitalWrite(S3, ((sensor_select >> 2) & 0x01));

  delay(1000); //loop updates every second

}

Thank's sherzaad with some modifications i got my code to work

//define digital pin used to connect to 4051
#define S1 4
#define S2 5
#define S3 6
#define EN 7
uint8_t sensor_select = 0;
int vibSensor;

void setup() {

  Serial.begin(115200);

  //initialise digital pins
  pinMode(S1, OUTPUT);
  digitalWrite(S1, LOW);
  pinMode(S2, OUTPUT);
  digitalWrite(S2, LOW);
  pinMode(S3, OUTPUT);
  digitalWrite(S3, LOW);
  pinMode(EN, OUTPUT);
  digitalWrite(EN, HIGH);

  Serial.println("READY!");
}

void loop() {
  get_tempU1();
  get_tempV1();
  get_tempW1();
  get_tempU2();
  get_tempV2();
  get_tempW2();
  get_tempDC();
 get_Vibration();

  delay(1000); //loop updates every second

}

//PowerSupply U1 pin 0
void get_tempU1()
{
  uint16_t x;

  sensor_select = 0;
  digitalWrite(S1, (sensor_select & 0x01));
  digitalWrite(S2, ((sensor_select >> 1) & 0x01));
  digitalWrite(S3, ((sensor_select >> 2) & 0x01));

  digitalWrite(EN, LOW); //enable output Z
  analogRead(A0); //output Z connected to A0
  x = analogRead(A0);
  


  float tempU1C = x * 0.217226044 - 61.1111111;
  float tempU1F = tempU1C + 273.15;
  Serial.println("           ");
  Serial.println("****PowerSupply U1****");
  Serial.println("Temp: ");
  Serial.print(tempU1C);
  Serial.print(" °C");
  Serial.println("           ");
  Serial.println("Fahr: ");
  Serial.print(tempU1F);
  Serial.print(" °F");
  Serial.println("           ");
  Serial.println("**********************");
  Serial.println("           ");
  digitalWrite(EN, HIGH); //disable output Z

  delay(1000);
}

//PowerSupply V1 pin 1
void get_tempV1()
{
  uint16_t x;

  sensor_select = 1;
  digitalWrite(S1, (sensor_select & 0x01));
  digitalWrite(S2, ((sensor_select >> 1) & 0x01));
  digitalWrite(S3, ((sensor_select >> 2) & 0x01));

  digitalWrite(EN, LOW); //enable output Z
  analogRead(A0); //output Z connected to A0
  x = analogRead(A0);

  float tempV1C = x * 0.217226044 - 61.1111111;
  float tempV1F = tempV1C + 273.15;
  Serial.println("           ");
  Serial.println("****PowerSupply V1****");
  Serial.println("Temp: ");
  Serial.print(tempV1C);
  Serial.print(" °C");
  Serial.println("           ");
  Serial.println("Fahr: ");
  Serial.print(tempV1F);
  Serial.print(" °F");
  Serial.println("           ");
  Serial.println("**********************");
  Serial.println("           ");
  digitalWrite(EN, HIGH); //disable output Z
  delay(1000);
}

//PowerSupply W1 pin 2
void get_tempW1() {
  uint16_t x;

  sensor_select = 2;
  digitalWrite(S1, (sensor_select & 0x01));
  digitalWrite(S2, ((sensor_select >> 1) & 0x01));
  digitalWrite(S3, ((sensor_select >> 2) & 0x01));

  digitalWrite(EN, LOW); //enable output Z
  analogRead(A0); //output Z connected to A0
  x = analogRead(A0);


  float tempW1C = x * 0.217226044 - 61.1111111;
  float tempW1F = tempW1C + 273.15;

  Serial.println("           ");
  Serial.println("****PowerSupply W2****");
  Serial.println("Temp: ");
  Serial.print(tempW1C);
  Serial.print(" °C");
  Serial.println("           ");
  Serial.println("Fahr: ");
  Serial.print(tempW1F);
  Serial.print(" °F");
  Serial.println("           ");
  Serial.println("**********************");
  Serial.println("           ");
  digitalWrite(EN, HIGH); //disable output Z
  delay(1000);
}


//Drive U2 pin 3
void get_tempU2() {
  uint16_t x;

  sensor_select = 3;
  digitalWrite(S1, (sensor_select & 0x01));
  digitalWrite(S2, ((sensor_select >> 1) & 0x01));
  digitalWrite(S3, ((sensor_select >> 2) & 0x01));

  digitalWrite(EN, LOW); //enable output Z
  analogRead(A0); //output Z connected to A0
  x = analogRead(A0);

  float tempU2C = x * 0.217226044 - 61.1111111;
  float tempU2F = tempU2C + 273.15;
  Serial.println("           ");
  Serial.println("*******Drive U2*******");
  Serial.println("Temp: ");
  Serial.print(tempU2C);
  Serial.print(" °C");
  Serial.println("           ");
  Serial.println("Fahr: ");
  Serial.print(tempU2F);
  Serial.print(" °F");
  Serial.println("           ");
  Serial.println("**********************");
  Serial.println("           ");
  digitalWrite(EN, HIGH); //disable output Z

  delay(1000);
}

//Drive V2 pin 4
void get_tempV2() {
  uint16_t x;

  sensor_select = 4;
  digitalWrite(S1, (sensor_select & 0x01));
  digitalWrite(S2, ((sensor_select >> 1) & 0x01));
  digitalWrite(S3, ((sensor_select >> 2) & 0x01));

  digitalWrite(EN, LOW); //enable output Z
  analogRead(A0); //output Z connected to A0
  x = analogRead(A0);

  float tempV2C = x * 0.217226044 - 61.1111111;
  float tempV2F = tempV2C + 273.15;
  Serial.println("           ");
  Serial.println("*******Drive v2*******");
  Serial.println("Temp: ");
  Serial.print(tempV2C);
  Serial.print(" °C");
  Serial.println("           ");
  Serial.println("Fahr: ");
  Serial.print(tempV2F);
  Serial.print(" °F");
  Serial.println("           ");
  Serial.println("**********************");
  Serial.println("           ");
  digitalWrite(EN, HIGH); //disable output Z

  delay(1000);
}

//Drive W2 pin 5
void get_tempW2() {
  uint16_t x;

  sensor_select = 5;
  digitalWrite(S1, (sensor_select & 0x01));
  digitalWrite(S2, ((sensor_select >> 1) & 0x01));
  digitalWrite(S3, ((sensor_select >> 2) & 0x01));

  digitalWrite(EN, LOW); //enable output Z
  analogRead(A0); //output Z connected to A0
  x = analogRead(A0);

  float tempW2C = x * 0.217226044 - 61.1111111;
  float tempW2F = tempW2C + 273.15;
  Serial.println("           ");
  Serial.println("*******Drive W2*******");
  Serial.println("Temp: ");
  Serial.print(tempW2C);
  Serial.print(" °C");
  Serial.println("           ");
  Serial.println("Fahr: ");
  Serial.print(tempW2F);
  Serial.print(" °F");
  Serial.println("           ");
  Serial.println("**********************");
  Serial.println("           ");
  digitalWrite(EN, HIGH); //disable output Z
  delay(1000);
}

//DC Link pin 6
void get_tempDC() {
  uint16_t x;

  sensor_select = 6;
  digitalWrite(S1, (sensor_select & 0x01));
  digitalWrite(S2, ((sensor_select >> 1) & 0x01));
  digitalWrite(S3, ((sensor_select >> 2) & 0x01));

  digitalWrite(EN, LOW); //enable output Z
  analogRead(A0); //output Z connected to A0
  x = analogRead(A0);

  float tempDCC = x * 0.217226044 - 61.1111111;
  float tempDCF = tempDCC + 273.15;

  Serial.println("           ");
  Serial.println("******* DCLink *******");
  Serial.println("Temp: ");
  Serial.print(tempDCC);
  Serial.print(" °C");
  Serial.println("           ");
  Serial.println("Fahr: ");
  Serial.print(tempDCF);
  Serial.print(" °F");
  Serial.println("           ");
  Serial.println("**********************");
  Serial.println("           ");
  digitalWrite(EN, HIGH); //diaable output Z
  delay(1000);
}

//Vibration sensor pin 7
void get_Vibration()
{
  uint16_t x;

  sensor_select = 7;
  digitalWrite(S1, (sensor_select & 0x01));
  digitalWrite(S2, ((sensor_select >> 1) & 0x01));
  digitalWrite(S3, ((sensor_select >> 2) & 0x01));

  digitalWrite(EN, LOW); //enable output Z
  analogRead(A0); //output Z connected to A0
  vibSensor = analogRead(A0);

  //While sensor is not moving, analog pin receive 1023~1024 value
  if (vibSensor < 1022) {

    Serial.print("Sensor Value: ");
    Serial.println(vibSensor);
  }

  else {

    Serial.print("Sensor Value: ");
    Serial.println(vibSensor);
  }

  digitalWrite(EN, HIGH); //disable output Z

  delay(100); //Small delay
}

It looks like you could reduce the number of functions in the program by passing the sensor number as a parameter and returning the value read to the calling program

Markf2017:
Thank's sherzaad with some modifications i got my code to work

some modifications huh! :wink:

but as @UKHeliBob mentioned you could have made a better job at coding and reduce a LOT of repetition.

but for a newbie, that was a good effort! :slight_smile: