Cascade two 4051 to read 16 buttons

Hi all,

I am tring to cascade two cd4051s inorder to 16 buttons.

Wiring is done based on http://www.567.dk/arduino/arduinocd4051b02.png this diagram and buttons have 220ohm resistors on readpins.

The problem is I can only read from one 4051 that is connected to Analog 0, the second 4051 does not respond. If I connect the non responding 4051 to A0 it starts working, so I am pretty sure the it is wired correctly.

I checked http://forum.arduino.cc/index.php/topic,21547.0.html this atticle and my others… modified my code, rewired everything, etc… but still no luck.

Most probably I am missing something basic but I am stucked and really appreciate some help.

My code:

int b0 = 0;
int b1 = 0;
int b2 = 0;

const int mux1 = A0;
const int mux2 = A1;

int buttonValue[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int lastButtonValue[16] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
const int ledPin =  13;  

void setup(){

  pinMode(2, OUTPUT); 
  pinMode(3, OUTPUT);  
  pinMode(4, OUTPUT);  
  pinMode(ledPin, OUTPUT); 
}

void loop () {

for (int buttonCount = 0; buttonCount < 8; buttonCount++) {
    b0 = bitRead(buttonCount,0);
    b1 = bitRead(buttonCount,1);
    b2 = bitRead(buttonCount,2);

    digitalWrite(4,b0);
    digitalWrite(3,b1);
    digitalWrite(2,b2);
 
    buttonValue[buttonCount] = digitalRead(mux1);
 
    buttonValue[buttonCount + 8] = digitalRead(mux2);
    
    if(buttonValue[buttonCount] == LOW && lastButtonValue[buttonCount] == HIGH) {
      digitalWrite(ledPin, HIGH); 
    } 

    if(buttonValue[buttonCount] == HIGH && lastButtonValue[buttonCount] == LOW) {
      digitalWrite(ledPin, LOW);  
      }    
      lastButtonValue[buttonCount] = buttonValue[buttonCount];
  }  
}

Many thanks in advance!

Deniz

Your code uses pins 2, 3, & 4 for the data select but your circuit uses pins 10, 11, & 12. You have no supply decoupling on any chips. You never seem to address the second mux chip in the lower part of the code with all the ifs in it.

sorry I forgot to mention, my data lines are also using pins 2,3,4 on my circuit, as it is in the code. I have added the wiring picture to give a reference to cascade type I am using. I have modified the circuit while coding. Two chips are sharing the same data pins connected to oins 2,3,4 and they have individually connected to A0 and A1 pins for read channel.

Grumpy_Mike: You have no supply decoupling on any chips. You never seem to address the second mux chip in the lower part of the code with all the ifs in it.

Can you open up this a bit? I dont really know how to read from the second chip. I am searching for an reference tutorial for the last two days and this is the result with I could find.

Note: I am using Arduino micro

This loop:-

for (int buttonCount = 0; buttonCount < 8; buttonCount++) {

only goes from 0 to 7 which is one multiplexer. You store the data from the second mux in the index plus 8 part of the array but when it comes to looking at the values:-

 if(buttonValue[buttonCount] == LOW && lastButtonValue[buttonCount] == HIGH) {
      digitalWrite(ledPin, HIGH); 
    } 

    if(buttonValue[buttonCount] == HIGH && lastButtonValue[buttonCount] == LOW) {
      digitalWrite(ledPin, LOW);  
      }    
      lastButtonValue[buttonCount] = buttonValue[buttonCount];
  }

You never do anything with the values in buttonCount + 8 so as buttonCount only runs from 0 to 7 you think nothing is happening with the other mux.

Thanks for your input Mike. I have adopted the part of the code that scans the inputs from other projects.

http://forum.arduino.cc/index.php/topic,21547.0.html

These are my source projects. They are all using the same login with two 4051 chips

for (int buttonCount = 0; buttonCount < 8; buttonCount++) {
    b0 = bitRead(buttonCount,0);
    b1 = bitRead(buttonCount,1);
    b2 = bitRead(buttonCount,2);

    digitalWrite(4,b0);
    digitalWrite(3,b1);
    digitalWrite(2,b2);
 
    buttonValue[buttonCount] = digitalRead(mux1);
 
    buttonValue[buttonCount + 8] = digitalRead(mux2);

buttonvalue stores 16 value array variable. When I read “digitalRead(mux1);” Button count should go from 0-7 and I should be able to parshe the pin values of the first mux into the first 8 values of the buttonValue array variable. Then, “digitalRead(mux2)” should continue and “buttonValue[buttonCount + 8]” should register the pinvalues from the second mux into 8the to 15th values of the array. The for loop is going from 0 to 7 since I have two chips, and the reads from the second chip is incremented by 8 “buttonCount + 8” inorder to be able assign values to the second half of the 16 value array variable.

I have also suspected from the same part while experimenting and have change “buttonCount < 8;” part of the for loop to “buttonCount < 16;” and tested. It did not help. the same problem continued.

I think you are right about the problematic part but I do not know how to solve it. I need to read two different chips in a single for loop and then assign the 8 input readings from the first chip to the first 8 values of an array variable and the 8 other input values from the second chip to the second half of the same array.

I have also suspected from the same part while experimenting and have change “buttonCount < 8;” part of the for loop to “buttonCount < 16;” and tested. It did not help. the same problem continued.

Of course the problem continued because then you were writing over the mux 2 results with a repeat reading of mux 1.

Use two loops, one to read into the array and then the other to test what you have read. The first loop goes from 0 to 7 and the second from 0 to 15.

Thank you so much Mike. I followed your suggestions and it is working now.

Here is the final code two read from two cascaded 4051s, for future reference…

int b0 = 0;
int b1 = 0;
int b2 = 0;

const int mux1 = A0;
const int mux2 = A1;

int buttonValue[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int lastButtonValue[16] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
const int ledPin =  13;  

void setup(){

  pinMode(2, OUTPUT); 
  pinMode(3, OUTPUT);  
  pinMode(4, OUTPUT);  
  pinMode(ledPin, OUTPUT); 
}

void loop () {

muxone();
muxtwo();
}

void muxone () {

for (int buttonCount = 0; buttonCount < 8; buttonCount++) {
    b0 = bitRead(buttonCount,0);
    b1 = bitRead(buttonCount,1);
    b2 = bitRead(buttonCount,2);

    digitalWrite(4,b0);
    digitalWrite(3,b1);
    digitalWrite(2,b2);

    buttonValue[buttonCount] = digitalRead(mux1);
  
    
    if(buttonValue[buttonCount] == LOW && lastButtonValue[buttonCount] == HIGH) {
      digitalWrite(ledPin, HIGH); 
    } 

    if(buttonValue[buttonCount] == HIGH && lastButtonValue[buttonCount] == LOW) {
      digitalWrite(ledPin, LOW);  
      }    
      lastButtonValue[buttonCount] = buttonValue[buttonCount];
  }
  
  }
  
  
  void muxtwo () {

for (int buttonCount = 0; buttonCount < 16; buttonCount++) {
    b0 = bitRead(buttonCount,0);
    b1 = bitRead(buttonCount,1);
    b2 = bitRead(buttonCount,2);

    digitalWrite(4,b0);
    digitalWrite(3,b1);
    digitalWrite(2,b2);
    buttonValue[buttonCount + 8] = digitalRead(mux2);
    
    if(buttonValue[buttonCount] == LOW && lastButtonValue[buttonCount] == HIGH) {
      digitalWrite(ledPin, HIGH); 
    } 

    if(buttonValue[buttonCount] == HIGH && lastButtonValue[buttonCount] == LOW) {
      digitalWrite(ledPin, LOW);  
      }    
      lastButtonValue[buttonCount] = buttonValue[buttonCount];
  }
  
  }

hay can you upload the circuit for that