4051BE Mux Issue

Status
Not open for further replies.

mtiger

Well-known member
Hi All,

Currently testing a CD4051BE Mux with MIDI. I have set up 8 buttons connected directly to the MUX (0 - 7), with address A, B and C connected to the a Teensy(digital pins 2, 3, 4). Com 3 is connected to digital pin 5 on Teensy. All buttons are being pulled to ground with 220 ohm resistors and I am having the issue where when i press button 1, it prints out pin pin 13 (0) and pin 1 (4). So technically it is serial printing button 1 and 5 together, when i only press button 1. It does the same with buttons 2, 3 and 4 as follows

When I press:
Button 1 -(Prints Button 1 & 5)
Button 2 -(prints button 2 & 6)
Button 3 -(prints button 3 & 7)
Button 4 -(prints button 4 & 8)

The other 4 buttons don't send any signal nor print.

Please see the code.

Code:
//Multiplexer CD4051BE Address Pins
int addressA = 2; //Address Pin A (Pinout 11)
int addressB = 3; //Address Pin B (Pinout 10)
int addressC = 4; //Address Pin C (Pinout 9)

int b0 = 0; //button values
int b1 = 0; //button values
int b2 = 0; //button values
int b3 = 0; //button values
int b4 = 0; //button values
int b5 = 0; //button values
int b6 = 0; //button values
int b7 = 0; //button values


int b0_prev = 1; // previous button values
int b1_prev = 1; // previous button values
int b2_prev = 1; // previous button values
int b3_prev = 1; // previous button values
int b4_prev = 1; // previous button values
int b5_prev = 1; // previous button values
int b6_prev = 1; // previous button values
int b7_prev = 1; // previous button values


void setup() {
  Serial.begin(9600);

  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, INPUT_PULLUP);

}

void loop() {
  midiMuxButtons();
}


//MIDI BUTTON FUNCTION
void midiMuxButtons ()  {

  //BUTTON 1//
  digitalWrite(addressA, LOW);
  digitalWrite(addressB, LOW);
  digitalWrite(addressC, LOW);
  delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
  b0 = digitalRead(5); // Data Pin from Mux to Microcontroller

  if (b0 == HIGH && b0_prev == LOW) {
    // signal just went low
    usbMIDI.sendNoteOn(0, 110, 1);
    Serial.println("Button 1:");
  }
  if (b0 == LOW && b0_prev == HIGH) {
    // signal just went high
    usbMIDI.sendNoteOff(0, 110, 1);
  }
  b0_prev = b0;

  //BUTTON 2//
  digitalWrite(addressA, HIGH);
  digitalWrite(addressB, LOW);
  digitalWrite(addressC, LOW);
  delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
  b1 = digitalRead(5); // Data Pin from Mux to Microcontroller

  if (b1 == HIGH && b1_prev == LOW) {
    // signal just went low
    usbMIDI.sendNoteOn(1, 110, 1);
    Serial.println("Button 2:");
  }
  if (b1 == LOW && b1_prev == HIGH) {
    // signal just went high
    usbMIDI.sendNoteOff(1, 110, 1);
  }
  b1_prev = b1;


  //BUTTON 3//
  digitalWrite(addressA, LOW);
  digitalWrite(addressB, HIGH);
  digitalWrite(addressC, LOW);
  delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
  b2 = digitalRead(5); // Data Pin from Mux to Microcontroller

  if (b2 == HIGH && b2_prev == LOW) {
    // signal just went low
    usbMIDI.sendNoteOn(2, 110, 1);
    Serial.println("Button 3:");

  }
  if (b2 == LOW && b2_prev == HIGH) {
    // signal just went high
    usbMIDI.sendNoteOff(2, 110, 1);
  }
  b2_prev = b2;

  //BUTTON 4//
  digitalWrite(addressA, HIGH);
  digitalWrite(addressB, HIGH);
  digitalWrite(addressC, LOW);
  delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
  b3 = digitalRead(5); // Data Pin from Mux to Microcontroller

  if (b3 == HIGH && b3_prev == LOW) {
    // signal just went low
    usbMIDI.sendNoteOn(3, 110, 1);
    Serial.println("Button 4:");

  }
  if (b3 == LOW && b3_prev == HIGH) {
    // signal just went high
    usbMIDI.sendNoteOff(3, 110, 1);

  }
  b3_prev = b3;

  //BUTTON 5//
  digitalWrite(addressA, LOW);
  digitalWrite(addressB, LOW);
  digitalWrite(addressC, HIGH);
  delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
  b4 = digitalRead(5); // Data Pin from Mux to Microcontroller

  if (b4 == HIGH && b4_prev == LOW) {
    // signal just went low
    usbMIDI.sendNoteOn(4, 110, 1);
    Serial.println("Button 5:");

  }
  if (b4 == LOW && b4_prev == HIGH) {
    // signal just went high
    usbMIDI.sendNoteOff(4, 110, 1);
  }
  b4_prev = b4;

  //BUTTON 6//
  digitalWrite(addressA, HIGH);
  digitalWrite(addressB, LOW);
  digitalWrite(addressC, HIGH);
  delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
  b5 = digitalRead(5); // Data Pin from Mux to Microcontroller

  if (b5 == HIGH && b5_prev == LOW) {
    // signal just went low
    usbMIDI.sendNoteOn(5, 110, 1);
    Serial.println("Button 6:");

  }
  if (b5 == LOW && b5_prev == HIGH) {
    // signal just went high
    usbMIDI.sendNoteOff(5, 110, 1);
  }
  b5_prev = b5;

  //BUTTON 7//
  digitalWrite(addressA, LOW);
  digitalWrite(addressB, HIGH);
  digitalWrite(addressC, HIGH);
  delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
  b6 = digitalRead(5); // Data Pin from Mux to Microcontroller

  if (b6 == HIGH && b6_prev == LOW) {
    // signal just went low
    usbMIDI.sendNoteOn(6, 110, 1);
    Serial.println("Button 7:");
  }
  if (b6 == LOW && b6_prev == HIGH) {
    // signal just went high
    usbMIDI.sendNoteOff(6, 110, 1);
  }
  b6_prev = b6;

  //BUTTON 8//
  digitalWrite(addressA, HIGH);
  digitalWrite(addressB, HIGH);
  digitalWrite(addressC, HIGH);
  delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
  b7 = digitalRead(5); // Data Pin from Mux to Microcontroller

  if (b7 == HIGH && b7_prev == LOW) {
    // signal just went low
    usbMIDI.sendNoteOn(7, 110, 1);
    Serial.println("Button 8:");
  }
  if (b7 == LOW && b7_prev == HIGH) {
    // signal just went high
    usbMIDI.sendNoteOff(7, 110, 1);
  }
  b7_prev = b7;

  while (usbMIDI.read()); // read and discard any incoming MIDI messages
  delay(25);
}

Where am i going wrong. I've checked the wiring and everything is correct.

Any help would be great.
Thanks Guys :)
 
Last edited:
Looking at your results it looks like something is amiss with your MSB/addressC and it's never going high. Would suggest writing a basic test program where you hard set the three address pins in setup and then just loop reading the pin in and then writing it's state out to the LED on pin 13. With everything moving slower things get a bit easier to work out what is going on.

May also be worth packaging your series of button checks into a single for loop which avoids cut and paste issues that may also be buried in this long run of nearly identical but critically different code.
 
Hi Gremlin, thanks for your response.

Sorry but i'm not sure what you mean.

''addressC' does go high from button 5 (b4) onwards
 
Maybe you are using the wrong pins?

That your setup is doing:
Code:
void setup() {
  Serial.begin(9600);

  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, INPUT_PULLUP);

}

But your setting:
Code:
 digitalWrite(addressA, LOW);
  digitalWrite(addressB, LOW);
  digitalWrite(addressC, LOW);
BUT: you have:
Code:
int addressA = 2; //Address Pin A (Pinout 11)
int addressB = 3; //Address Pin B (Pinout 10)
int addressC = 4; //Address Pin C (Pinout 9)
So you are touching PIN 4, which has never had a pinmode done on it...
 
Thanks Kurt, completely misread that error.

I ended up rewriting the whole test code over again, and it does work now. Appreciate the insight guys :)
 
Status
Not open for further replies.
Back
Top