16-step LED sequencer with Multiplexer

Hello!

Currently I am trying to build a step sequencer with 16 steps. I wanted to have 16 LEDs which light up one after another, just like at a normal step sequencer. I already had a code written for getting analog Inputs but now I would like to send digital signal in order to light up the LEDs individually. Right now, the row 1 to 8 and 9 to 19 light up one after another independently, but I want all the 16 LEDs to light up LED by LED. I thank you in advance!

My code:

int pin_Out_S0 = 0;
int pin_Out_S1 = 1;
int pin_Out_S2 = 2;

int LEDState[32];

int bit1 = 0;
int bit2 = 0;
int bit3 = 0;
void LEDsteuern(int, int);


void setup() {

  //Select-Pins
  pinMode(pin_Out_S0, OUTPUT);
  pinMode(pin_Out_S1, OUTPUT);
  pinMode(pin_Out_S2, OUTPUT);

  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);

  Serial.begin(9600);
}

void loop() {


  for (int i {0}; i < 8; ++i)
  {
    bit1 = bitRead(i, 0);
    bit2 = bitRead(i, 1);
    bit3 = bitRead(i, 2);
    digitalWrite(pin_Out_S0, bit1);
    digitalWrite(pin_Out_S1, bit2);
    digitalWrite(pin_Out_S2, bit3);
     // Serial.print(bitRead(i, 0));

    //int LEDState[i] = bitRead(i, 0);
    //int LEDState[i + 8] = bitRead(i, 1);


    // digitalWrite(14, HIGH);
    //  digitalWrite(15, HIGH);
    LEDsteuern(i, 14);
    LEDsteuern(i + 8, 15);
    delay(200);
  }
}

void LEDsteuern (int zaehler, int digitalPin)
{
  digitalWrite(digitalPin, HIGH);
}

what's the exact hardware?

The "for" loop goes 0 to 7 with i and at each step illuminates LED i and i + 8. So lighting 0 and 8, 1 and 9, &c.

Seems like an easy change to make a loop that goes across all 16… and light just i at each step.

Try to understand the code you have. If you wrote it, you should be able to change it easily. If you didn't write it, try to understand the code you have.

Take a close look at the code you have and try to figure out how it works.

a7

@J-M-L :

I am using a Teensy 4.0 with CD74HC4051E Multiplexers.

I don't really understand the way how the Teensy "tells" the multiplexers which pin they should turn on in order to light up the LED. Also alto777 I'm sorry, the code I posted doesn't work. The one which kinda works is this one:

int pin_Out_S0 = 0;
int pin_Out_S1 = 1;
int pin_Out_S2 = 2;

int LEDState[32];

int bit1 = 0;
int bit2 = 0;
int bit3 = 0;
void LEDsteuern(int, int);


void setup() {

  //Select-Pins
  pinMode(pin_Out_S0, OUTPUT);
  pinMode(pin_Out_S1, OUTPUT);
  pinMode(pin_Out_S2, OUTPUT);

  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);

  Serial.begin(9600);
}

void loop() {


  for (int i {0}; i < 8; ++i)
  {

    bit1 = bitRead(i, 0);
    bit2 = bitRead(i, 1);
    bit3 = bitRead(i, 2);


    digitalWrite(pin_Out_S0, bit1);
    digitalWrite(pin_Out_S1, bit2);
    digitalWrite(pin_Out_S2, bit3);

    digitalWrite(14, HIGH);
    digitalWrite(15, HIGH);
    delay(80);
  }
}

void LEDsteuern (int zaehler, int digitalPin)
{
  digitalWrite(zaehler, HIGH);
}

This one is turning on the 1st and 9th one, then 2nd and 10th, 3rd and 11th and so on... I want them to light up step by step from 1 to 16.

"I am using... CD74HC4051E Multiplexers."

No sign of that in the code you posted. Once anything that serves no purpose is removed, it's a simple program.

Show us the problem you are having in the context of the hardware you are attempting to use. Assuming you really would have no trouble fixing the program you did post, no doubt your problems come from trying to deal also with your multiplexers.

For which a schematic should be provided.

a7

OK I see, something to consider, thank you!

It's lunch time here however. :wink:

But

bit1, bit2 and bit3 form a three digit binary address it seems.

pin 14 talks to one bank of 8, pin 15 to the other, I guess.

So it remains an easy matter.

a7

Share a diagram of what’s connected where. It will be a good start

I never did a schematic before, I hope it's understandable.

No resistors for those leds ?

  • you should add current limiting resistor for each LED (220 to 330Ω would probably do)
  • don't use pin 0 and 1, they are used for Serial Communication. the code below moved 0,1,2 to 2,3,4

try this code

const byte pin_Out_S0 = 2;
const byte pin_Out_S1 = 3;
const byte pin_Out_S2 = 4;

void setup() {

  //Select-Pins
  pinMode(pin_Out_S0, OUTPUT);
  pinMode(pin_Out_S1, OUTPUT);
  pinMode(pin_Out_S2, OUTPUT);

  // Common pin routed to selected output
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
}

void loop() {

  digitalWrite(14, HIGH);
  digitalWrite(15, LOW);
  for (byte i = 0; i < 8; ++i) {
    digitalWrite(pin_Out_S0, bitRead(i, 0));
    digitalWrite(pin_Out_S1, bitRead(i, 1));
    digitalWrite(pin_Out_S2, bitRead(i, 2));
    delay(80);
  }
  
  digitalWrite(14, LOW);
  digitalWrite(15, HIGH);
  for (byte i = 0; i < 8; ++i) {
    digitalWrite(pin_Out_S0, bitRead(i, 0));
    digitalWrite(pin_Out_S1, bitRead(i, 1));
    digitalWrite(pin_Out_S2, bitRead(i, 2));
    delay(80);
  }
}

this should light up the LEDs one by one

Since the CD74HC4051E Multiplexers share the same control pins, the only way you decide what goes out is by setting the copy pin to HIGH or LOW and repeat the loop

As you use PWM pins (14 and 15 - A0 and A1 on Teensy 4.0) you can also use analogWrite() instead of digitalWrite() for those 2 pins (not the S0,S1,S2 pins) to set a PWM signal and you can control the intensity of the LED

Alright, I will add the resistors! I just switched the pins to 2,3,4 as you wrote it. If I run the code, the LEDs light up in a glitchy way.

Video:
https://www.youtube.com/watch?v=pC8N9Eg6Udk&feature=youtu.be&ab_channel=M%C3%A1t%C3%A9Si

Don't run this without the resistors... you are at risk of drawing too much current from your Arduino pins and damage your components / teensy

Ensure the output of the CD74HC4051E are connected in sequence to adjacent pins.

IC.png

You are right, the problem is probably the sequence how I connected the LEDs. I will check them again and add the resistors. Could you maybe explain the math behind the reason why I have to pick a resistor between 220 to 330 Ohm?

On the IC they are all over the place

IC.png

a LED draws as much current as it can from its source and depending on the LED color/build also has a Forward Voltage.

You want a small enough current flowing through your Arduino pins, I thin the teensy will start suffering if you go above 25mA. You have a know voltage "U" when you put your pin HIGH on your teensy and you have a resistor in series with the LED.

So you use the infamous U = RI rule to find R based on the Forward Voltage from the LED and I, the current you want.

My rule of thumb is something between 220 and 330 should be good enough for most cases with 3.3 or 5V Arduinos and common LEDs. The higher the resistance, the lower the current. So if you want to play safe, go for higher - but not too high or the LED won't light on as it needs enough current. Most LED would light up at 5mA probably those days

R = (V(source) - V(led)) / I(led)

V(source) is the source voltage, measured in volts (V),
V(led) is the voltage drop across the LED, measured in volts (V),
I(led) is the current through the LED, measured in Amperes (Amps/A), and
R is the resistance, measured in Ohms (Ω).

if you don't want to do the math, there are online tools

Thank you!! Now the LED lights up in the right order, but my pins are connected this way:

1.LED - A0
2.LED - A1
3.LED - A4
4.LED - A5
5.LED - A2
6.LED - A3
7.LED - A6
8.LED - A7

Is there any logic behind that or is maybe the code wrong and I just rewired it in a wrong way to make the LEDs light up in the right order though?

that's weird

do you have just the chip or do you have a module bought from somewhere ?

Just the chip.. Sorry, I wrote module before.

OK - very weird. are you sure you wired S0,S1 and S2 in the right way?

teensy pin 2 --> module pin 11
teensy pin 3 --> module pin 10
teensy pin 4 --> module pin 9

if you exchanged one of those then it's not counting 0 to 7 in the right order

The order now, after changing the address cables:

1.LED - A0
2.LED - A1
3.LED - A2
4.LED - A3
5.LED - A4
6.LED - A5
7.LED - A6
8.LED - A7

makes more sense.. :slight_smile: :slight_smile:

Thank you so much!! :))

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