Problem using multiple CD4051 with pins 10,11,12 on arduino mega

Problem with reading multiple CD4051 with Arduino

Hello everyone,

I'm building a MIDI controller with an Arduino Mega and I'm losing my mind right now...
I am having trouble reading multiple CD4051 with my Arduino. I have connected the multiplexer to the digital pins (12,11,10) of the Arduino and I am only able to read four of the eight channels. It seems like the Mux Pin C (digital Pin 10 in my code) is not working properly. The array is written as follows: The first four values (a,b,c,d) are not read, and the second four values (e,f,g,h) are written into the first and second half of the array -> [e,f,g,h,e,f,g,h]. There are no shorts on the board and the voltages are correct at the pins. I am not sure if this is related to my problem, but when I test with a continuity tester, pins 11 and 12 are shorted at regular intervals. This does not happen with Pin 10 - I have tested this behavior with two different boards.

#include <Adafruit_NeoPixel.h>

#define NeoPixelPin 13
#define NUMPIXELS 98

int brightness = 10;

Adafruit_NeoPixel pixels(NUMPIXELS, NeoPixelPin, NEO_GRB + NEO_KHZ800);

//const int analogMultPins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // Array of analog pins
const int analogMultPins[] = {0}; // Array of analog pins
const int numMux = sizeof(analogMultPins) / sizeof(int); // Number of CD4051 multiplexers
//const int numMux = 2; // Number of CD4051 multiplexers
const int muxPins[] = {12, 11, 10}; // Pins connected to the CD4051 multiplexers

int controlValuesNew[numMux * 8]; // Array to store the new control values

void setup() {
  for (int i = 0; i < 2; i++) {
    pinMode(muxPins[i], OUTPUT);
  };
  pixels.begin();
  setMultiplexer(0);
  Serial.begin(9600); // Initialize serial communication

  Serial.print("Number of CD4051: ");
  Serial.print(numMux);
  Serial.println();
};

void loop() {
  readAllMults();
  setNeoPixelValues();
  pixels.show();
}

void readAllMults() {
  for (int multChannel = 0; multChannel < 8; multChannel++) {
    setMultiplexer(multChannel);
    delay(100);
    for (int mult = 0; mult < numMux; mult++) {
      controlValuesNew[mult * 8 + multChannel] = analogRead(analogMultPins[mult]);
    };
  };
};

void setNeoPixelValues() {
  for (int i = 0; i < numMux * 8; i++) {
    int hue = map(controlValuesNew[i], 0, 1023, 0, 65535);
    pixels.setPixelColor(i, pixels.ColorHSV(hue));
    pixels.setBrightness(brightness);
  }
}

void setMultiplexer( int i ) {

  int pin0Value = i & 0x01;      // get value of first bit
  int pin1Value = (i >> 1) & 0x01; // get value of second bit
  int pin2Value = (i >> 2) & 0x01; // get value of third bit

  digitalWrite( muxPins[0], pin0Value ); // turn first pin on or off
  digitalWrite( muxPins[1], pin1Value ); // turn second pin on or off
  digitalWrite( muxPins[2], pin2Value ); // turn third pin on or off
 
};


Here is part of the schematic

and one of the many multiplexers

I hope you can help me!

Best regards,

Benni

How many pins does this statement set up?

Thank you so much... You wouldn't believe how many hours I spent looking for this mistake in all the wrong places...
Whats best practice to not make mistakes like this in the future? Write the pinsetup out completely?

Let other people look over your code. Honestly. You alone will overlook the same bug over and over again.

Below defines a macro that calculates the number of elements in any type of array (int, float, class etc) at compile time.

#define NUMELEMENTS(x) (sizeof(x) / sizeof(x[0]))

You can use it like


for (int i = 0; i < NUMELEMENTS(muxPins); i++) {
    pinMode(muxPins[i], OUTPUT);
  };

That's a good point, thanks!

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