[Help] Mux Demux 4051 code

Hii, I need help with my task i have 5 IC 4051 i want to make mux and demux like http://playground.arduino.cc/Learning/4051 i got the problem with this code, i want to read analog input from all pin in 4051 IC

this is my source :

void setup()

{
//4051 digital control pins

pinMode(0, OUTPUT); // s0
pinMode(1, OUTPUT); // s1
pinMode(2, OUTPUT); // s2
pinMode(3, OUTPUT); // s3
pinMode(4, OUTPUT); // s4
pinMode(5, OUTPUT); // s5
Serial.begin(9600);
}

void loop()

{


//baca 0
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
int nilaisensor0 = analogRead(0);
float voltage0 = (nilaisensor / 1024) * 5 ; //
delay(10);

Serial.print(voltage0);
Serial.println("input0 Volt");
delay(1000);

//baca 1
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
int nilaisensor1 = analogRead(0);
float voltage1 = (nilaisensor / 1024) * 5 ; //
delay(10);

Serial.print(voltage1);
Serial.println("input1 Volt");
delay(1000);

//baca 2
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
int nilaisensor = analogRead(0);
float voltage = (nilaisensor / 1024) * 5 ; //
delay(10);

Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 3
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 4
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 5
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 6
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 7
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 8
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 9
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 10
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 11
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 12
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 13
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 14
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 15
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 16
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 17
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 18
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 19
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 20
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 21
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 22
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 23
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 24
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 25
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 26
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 27
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 28
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 29
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);

Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 30
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);

Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

//baca 31
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(10);


Serial.print(voltage);
Serial.println(" Volt");
delay(1000);
}

can you fix this problem?

You can't use pins 0 and 1 as these are already used by serial.

Mark

The 4051 is an 8-way analog multiplexer, it takes 3 address lines.

You claim to have 5 4051’s and are using 6 select lines. How are they
connected together? I assume pins 0,1,2 to all the first 4 in parallel,
then 3,4,5 to the last which takes the outputs of those to the analog pin?

Couple of things: pins 0 and 1 are already connected to the USB/serial chip
on the board, don’t use them unless you understand the consequences…

Secondly you can avoid all the multiplicity of code by encoding the address
values in to an integer and then decoding that to write the pins:

#define LOW_ADDRESS_BASEPIN 2   // pins 2,3,4 for low address
#define HIGH_ADDRESS_BASEPIN 5  // pins 5,6,7 for high address

void setLowAddressPins (int addr) // 0..7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (LOW_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}
void setHighAddressPins (int addr) // 0..7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (HIGH_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}

void loop ()
{
  for (int addr = 0 ; addr < 32 ; addr++)
  {
    setHighAddressPins (addr >> 3) ;  // top 3 bits for high address
    setLowAddressPins (addr & 7) ;    // bottom 3 bits for low address
    int value = analogRead (...) 
    ....
  }
}

You could of course combine the two sets of 3 pins into one set of 6 and just have a
single address pin setting routine. The functions assume address bits for each tier of
analog multiplexers are contiguous Arduino pin numbers.

MarkT:
The 4051 is an 8-way analog multiplexer, it takes 3 address lines.

You claim to have 5 4051’s and are using 6 select lines. How are they
connected together? I assume pins 0,1,2 to all the first 4 in parallel,
then 3,4,5 to the last which takes the outputs of those to the analog pin?

Couple of things: pins 0 and 1 are already connected to the USB/serial chip
on the board, don’t use them unless you understand the consequences…

Secondly you can avoid all the multiplicity of code by encoding the address
values in to an integer and then decoding that to write the pins:

#define LOW_ADDRESS_BASEPIN 2   // pins 2,3,4 for low address

#define HIGH_ADDRESS_BASEPIN 5  // pins 5,6,7 for high address

void setLowAddressPins (int addr) // 0…7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (LOW_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}
void setHighAddressPins (int addr) // 0…7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (HIGH_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}

void loop ()
{
  for (int addr = 0 ; addr < 32 ; addr++)
  {
    setHighAddressPins (addr >> 3) ;  // top 3 bits for high address
    setLowAddressPins (addr & 7) ;    // bottom 3 bits for low address
    int value = analogRead (…)
    …
  }
}



You could of course combine the two sets of 3 pins into one set of 6 and just have a 
single address pin setting routine. The functions assume address bits for each tier of
analog multiplexers are contiguous Arduino pin numbers.

this is source my sketch
but in my case, i only use 4 IC 4051 in first line and 1 IC 4051 in second line,

Have you actually wired up the chips and gotten them to work?

...
//baca 8
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(10);



Serial.print(voltage);
Serial.println(" Volt");
delay(1000);

...

Better read up on how to use functions. Your code is too long.

zoomkat:
Have you actually wired up the chips and gotten them to work?

yes their work

MarkT:
The 4051 is an 8-way analog multiplexer, it takes 3 address lines.

You claim to have 5 4051’s and are using 6 select lines. How are they
connected together? I assume pins 0,1,2 to all the first 4 in parallel,
then 3,4,5 to the last which takes the outputs of those to the analog pin?

Couple of things: pins 0 and 1 are already connected to the USB/serial chip
on the board, don’t use them unless you understand the consequences…

Secondly you can avoid all the multiplicity of code by encoding the address
values in to an integer and then decoding that to write the pins:

#define LOW_ADDRESS_BASEPIN 2   // pins 2,3,4 for low address

#define HIGH_ADDRESS_BASEPIN 5  // pins 5,6,7 for high address

void setLowAddressPins (int addr) // 0…7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (LOW_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}
void setHighAddressPins (int addr) // 0…7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (HIGH_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}

void loop ()
{
  for (int addr = 0 ; addr < 32 ; addr++)
  {
    setHighAddressPins (addr >> 3) ;  // top 3 bits for high address
    setLowAddressPins (addr & 7) ;    // bottom 3 bits for low address
    int value = analogRead (…)
    …
  }
}



You could of course combine the two sets of 3 pins into one set of 6 and just have a 
single address pin setting routine. The functions assume address bits for each tier of
analog multiplexers are contiguous Arduino pin numbers.

why when i try your code i got some error

i am sorry if i asking dumb question :D,
i am newbie with this :smiley:

The code has no setup() function.

Add one that sets all the pins to outputs as required.


Rob

Graynomad:
The code has no setup() function.

Add one that sets all the pins to outputs as required.


Rob

i am sorry, can you help me and give a example?

i try like this but didn’t work

#define LOW_ADDRESS_BASEPIN 2  // pins 2,3,4 for low address
#define HIGH_ADDRESS_BASEPIN 5  // pins 5,6,7 for high address

void setLowAddressPins (int addr) // 0..7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (LOW_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}
void setHighAddressPins (int addr) // 0..7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (HIGH_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
Serial.begin(9600);
}

void loop ()
{
  for (int addr = 0 ; addr < 32 ; addr++)
  {
    setHighAddressPins (addr >> 3) ;  // top 3 bits for high address
    setLowAddressPins (addr & 7) ;    // bottom 3 bits for low address
    int value = analogRead (0);
    float voltage = (value / 1024) * 5 ;
    delay(10);
 Serial.print(voltage);
 Serial.println(" volt");
 delay(1000);
  }
}

Your original code had a setup() function, this doesn’t. Do you not think one is required any more?

Also what is

Serial.begin(9600);

Doing in the middle of the setHighAddressPins() func? You can’t just randomly stick function calls in places because it looks good.

Look at your original code and modify the setup() to work with this code. For example Serial.begin(9600); should be in setup();

And you need to set all the MUX control pins to outputs. You did it in the original setup() but as they are contiguous you can do this

for (int i = LOW_ADDRESS_BASEPIN; i < HIGH_ADDRESS_BASEPIN + 3; i++) {
    pinMode(i, OUTPUT);
}

Rob

i modified original code like this :

#define LOW_ADDRESS_BASEPIN 2  // pins 2,3,4 for low address
#define HIGH_ADDRESS_BASEPIN 5  // pins 5,6,7 for high address

void setup()
{
  for (int i = LOW_ADDRESS_BASEPIN; i < HIGH_ADDRESS_BASEPIN + 3; i++) 
  pinMode(i, OUTPUT);
  Serial.begin(9600);
}
void setLowAddressPins (int addr) // 0..7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (LOW_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}
void setHighAddressPins (int addr) // 0..7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (HIGH_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}

void loop ()
{
  for (int addr = 0 ; addr < 32 ; addr++)
  {
    setHighAddressPins (addr >> 3) ;  // top 3 bits for high address
    setLowAddressPins (addr & 7) ;    // bottom 3 bits for low address
  
  int value = analogRead(0);
  float voltage = (value / 1024) * 5 ; //
  delay(10);
  Serial.print(voltage);
  Serial.println(" Volt");
  delay(1000);
  
}
}

when i print in serial monitor stil no voltage input :frowning:

I've got two words for you, integer maths.

Let's say value = 500 after the analogRead()

500/1024 = 0 0 * 5 = 0

I think that's where your problem is.

Oh, and close but no cigar with the CODE tags :)


Rob

Graynomad:
I’ve got two words for you, integer maths.

Let’s say value = 500 after the analogRead()

500/1024 = 0
0 * 5 = 0

I think that’s where your problem is.

Oh, and close but no cigar with the CODE tags :slight_smile:


Rob

i am sorry Rob,
hehehe its i typo between value and ( 5 * 1024),
hehehe

so now i modify like this

#define LOW_ADDRESS_BASEPIN 2  // pins 2,3,4 for low address
#define HIGH_ADDRESS_BASEPIN 5  // pins 5,6,7 for high address

void setup()
{
  for (int i = LOW_ADDRESS_BASEPIN; i < HIGH_ADDRESS_BASEPIN + 3; i++) 
  pinMode(i, OUTPUT);
  Serial.begin(9600);
}
void setLowAddressPins (int addr) // 0..7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (LOW_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}
void setHighAddressPins (int addr) // 0..7
{
  for (int i = 0 ; i < 3 ; i++)
    digitalWrite (HIGH_ADDRESS_BASEPIN+i, 1 & (addr >> i)) ;
}

void loop ()
{
  for (int addr = 0 ; addr < 32 ; addr++)
  {
    setHighAddressPins (addr >> 3) ;  // top 3 bits for high address
    setLowAddressPins (addr & 7) ;    // bottom 3 bits for low address
  int value = analogRead(0);
  float voltage = (5 / 1024) * value ; //
  delay(10);
  Serial.print(voltage);
  Serial.println(" Volt");
  delay(1000);
  }
}

its right?
and i got voltage is 0 too
where is i wrong again :frowning:

sorry if my english so bad :smiley:

Isn't this the same problem, what's 5/1024?


Rob

i wanna read analog voltage like this http://arduino.cc/en/Tutorial/ReadAnalogVoltage and i follow this code :D

i follow this code

No you don't.

(5.0 / 1024.0)

Rob

so, how can i read analog voltage more than six input? i need your help :(

Do you now have a good reading from the inputs?

If so you're almost there.

Instead of a single variable called "voltage" have an array of 32 (or how many inputs you have) floats called say "voltages" and in your for loop place the calculated values into voltages[addr]

Time to do some homework on arrays.


Rob

anyone can help with my problem? i still cant read analog input from Mux and Demux 4051 i have 4 mux IC and 1 demux IC, i got problem with use function,

Post your latest code.


Rob