Multiplexing LED 3x3 Arduino learning

Hi guys, i want to learn how multiplexing LED using arduino, so i create 3x3 LED connected to my arduino. row(anode) connected to pin 8,9,10. coloum(cathode) connected resistor then to pin 2,3,4

basicly i set each row HIGH step by step, then i set coloum to LOW to turn on the LED i want.

it works, but i get problem that if i set delay time faster, my LED will be more dim. I dont know what's the reason.

Here is the code i use

int a = 1000; //delay time
void setup() {
  pinMode(2, OUTPUT); // 2, 3, 4 is chatode
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(8, OUTPUT); // 8, 9, 10 is anode
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);

}




void loop() { // this is for turn on the first led on first row
   scanFirstRow();
   delay(a);
   turnon1();
   delay(a);
   scanSecondRow();
   delay(a);
   scanThirdRow();
   delay(a);
}


void scanFirstRow() { // this is for scanning first row
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
}

void scanSecondRow() { // this is for scanning second row
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);

}

void scanThirdRow() { // this is for scanning third row
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  
}

void turnon1(){ // this is for turn on first light on first row 
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(a);
  digitalWrite(2, HIGH);
}

Welcome to the forum.

If a led is on 1/9 of the time, then its brightness is 1/9. It is supposed to work that way.

When the human eye wants 50Hz to see it as not-blinking, then you need to scan all the leds with 50*9 = 450 Hz. That is a delay of 2ms.
If 20Hz is good enough, then the delay is 5ms.

I would use two for-loops to walk through all the leds, and then inside those for-loops, I would turn on the led that needs to be on.
Something like this:

// For: https://forum.arduino.cc/t/multiplexing-led-3x3-arduino-learning/1052704
//
// This Wokwi project: https://wokwi.com/projects/348142596017619538
//
// Bad example !
// I had this multiplexing thing all wrong.
// All three anodes should be set at the same time,
// and the brightness will only be 1/3. It is now 1/9.

int a = 5; //delay time

void setup() 
{
  pinMode(2, OUTPUT);    // 2, 3, 4 is cathode
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(8, OUTPUT);    // 8, 9, 10 is anode
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  // Turn off the rows (cathodes)
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
}

void loop() 
{
  // Scan all the leds, one by one.
  for( int row = 2; row <= 4; row++)
  {
    // Turn the active row (three cathodes) low
    digitalWrite(row, LOW);

    for( int column = 8; column <= 10; column++)
    {
      // Turn the middle led on.
      if( row == 3 and column == 9)
      {
        digitalWrite(column, HIGH);
      }
      else
      {
        // Randomly turn an other led on and off
        // Just for fun.
        if( random(100) < 2)
        {
          digitalWrite( column, HIGH);
        }
      }

      // Keep the led on some time, to make it visible
      delay(a);

      // Turn the column off (anode of led)
      digitalWrite(column,LOW);
    }

    // Ready with this row, turn it off
    digitalWrite(row,HIGH);
  } 
}

// this is for scanning first row
void scanFirstRow() 
{ 
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
}

// this is for scanning second row
void scanSecondRow() 
{ 
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);

}

// this is for scanning third row
void scanThirdRow() 
{ 
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
}

// this is for turn on first light on first row
void turnon1() 
{ 
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(a);
  digitalWrite(2, HIGH);
}

The sketch in Wokwi simulation:

[ADDED]
After showing my sketch, I noticed that I did it wrong. When all three anodes are set at the same time, then the brightness is 1/3 instead of 1/9. Ha ha, silly me. I will add comment that it is wrong.
Is it for school ?

1 Like

Yes, like @Koepel says, that is expected. If your led is only on for 1/9th of the time, it will be 1/9th as bright.

One way to compensate for this is to increase the current. But the problem is that an Arduino pin can only source or sink about 30mA. And that is assuming you are using Uno/Nano/Mega or similar. Other Arduino types can source or sink only 10~15mA per pin.

Another way is to light more than one led at the same time. In your 3x3 matrix you could light up 3 LEDs at the same time. This way, an led can be on for 1/3rd of the time so will appear 1/3rd as bright. But, you must still not exceed the 30mA for any pin.

You can increase the current that can be provided to a row or column of LEDs by using transistors, for example bc337.

yes mate, i want to learn it for school. interest how multiplexing work, but get some problem, i'll try your code

would you please make let me know where to put the transistor? i'm really newbie XD

hi, i'm back :smiley: this code is work. but i'm confuse how to turn-on each led, or make some led turn on at the same time. would you please guide me?

Like this

2 Likes

That is what writing code is about: Take a problem from the real world and solve it in a logical way.

There are two things in the code:

  1. Code that walks through the leds. Can I call that "scanning" ?
  2. Showing a pattern.

Suppose that some code is scanning the leds, and you have a pattern to show. How would combine them ? I think the solution is an array of 3x3. Fill in the array with the pattern, and let the scanning part deal with it.
If you know an other way, let us know.

Thanks

Make sense but i don't know how to code array :sweat_smile:
That's why i use simple code only. I'll try to find some code about it thanks

What do you want to make ? A fixed pattern or a pattern that changes all the time ?

That's correct. For this assignment you have to pick a pattern which demonstrates multiplexing is working.
Simply lighting all the LEDs or a single full row/column does not require multiplexing. With your 3x3 led matrix, a convincing demonstration would be to alternate between a 'O' and an 'X' every second.

'X'
image

'O'
image

Yes, or perhaps a rotating line:

.*.
.*.
.*.

..*
.*.
*..

...
***
...

*..
.*.
..*

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