Issue with CD74HC4067 and multiple motors(pwm)

I have the following code:

const int MUX_S0_PIN = 4;
const int MUX_S1_PIN = 5;
const int MUX_S2_PIN = 6;
const int MUX_S3_PIN = 7;
const int MUX_OUTPUT_PIN = 9;
const int NUM_MOTORS = 2;
const char MOTOR_IDS[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};

void setup() {
  pinMode(MUX_S0_PIN, OUTPUT);
  pinMode(MUX_S1_PIN, OUTPUT);
  pinMode(MUX_S2_PIN, OUTPUT);
  pinMode(MUX_S3_PIN, OUTPUT);
  pinMode(MUX_OUTPUT_PIN, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  if (Serial.available() > 0) {
    char id = Serial.read();
    int state = Serial.parseInt();
    for (int i = 0; i < NUM_MOTORS; i++) {
      if (id == MOTOR_IDS[i]) {
        setMuxOutput(i);
        analogWrite(MUX_OUTPUT_PIN, state);
        break;
      }
    }
  }
}

void setMuxOutput(int motorIndex) {
  digitalWrite(MUX_S0_PIN, motorIndex & 0x01 ? HIGH : LOW);
  digitalWrite(MUX_S1_PIN, motorIndex & 0x02 ? HIGH : LOW);
  digitalWrite(MUX_S2_PIN, motorIndex & 0x04 ? HIGH : LOW);
  digitalWrite(MUX_S3_PIN, motorIndex & 0x08 ? HIGH : LOW);
}

this code should read the value for each motor coming from the pure data software. (I'm passing to each motor id numbers between 0 - 255)

The issue I'm facing is when I'm chanig the value of one motor (id A) it will stopped the other motor (id B) . (I'm using only two motors for the moment.

this wired as follow:

is there a mistake in code or wiring? or is just not possible all motors will worked at once ?
I might need to have an array of state so each motor will hae is own variable so the state can be rememberd?

A 4067 multiplexer is a 16 position selector switch (16 to 1 or 1 to 16).
There can only be one motor at the time connected to the signal pin.

To control/PWM 16 small motors at the same time you could use a PCA9685 servo board. If you do, then don't use the 1k base resistors, because that board already has 220 ohm resistors built-in. A 2N2222 is a much better choice for motors than a 2N3904.
Leo..

ok I will look into PCA9685 or I will just use individual outputs from the arduino.

But how I could read continuous data from multiple analog sensor at once (pressing few of them at the same time) ? (using the 4067 mux )

Hi,
Is this to do with this?

Can I suggest you get ONE problem at a time fixed with the SAME project?

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