Im creating an Led matrix and from my understanding, each pin can handle about 20-40 mA, which is suppose to be about one Led. But what im doing is instead of setting each Led to High, i would turn each individual Led on and off so that no two Leds are on at the same time. So even though the display shows all Leds to be on, they are actually turning on and off really quickly. My understanding of this is shaky so bear with me, does this mean i can connect as many Leds as i want without worrying about the 40 mA limit that arduino pins have?
does this mean i can connect as many Leds as i want without worrying about the 40 mA limit that arduino pins have?
No.
The limit applies equally to sourcing, that is getting the current from the pin and sinking, letting the pin act as a ground.
Yes, with only one led on at a time, the pin sourcing the current and the pin sinking the current will both be less than 40mA so no problem there.
But...
How big will your matrix be? If no larger than around 16 LEDs (e.g. 4x4) then this idea is ok. But for larger matrices, this will result in a very dim display.
If you light the matrix one led at a time, with the maximum current the led can handle, usually 20mA, then the led being lit at any instant in time will be very bright. But it will be off for 15 times longer than it is on, so the average brightness, which is what the eye sees, will be only 1/16th as bright.
Let's say you light the led which is on with 16mA, and then each other led is also on in quick succession. All LEDs are on, but each is only getting 1mA on average, so looks as bright as an led which is on full time but with only 1mA current.
You may say "but with the patterns I want to display on my matrix, only a few LEDs will appear to be lit at any time, so I don't need to waste time on the LEDs which are off, so the matrix will be bright". That's true, but there's another problem: if, let's say, you only light 2 LEDs in your pattern, then change to a pattern where 4 LEDs are lit, the overall brightness will suddenly drop to half.
To avoid that happening, you have to give equal time to all LEDs in the matrix, even those which are off.
To make a larger matrix appear bright, you have to increase that average current each led receives. There are two ways to do this.
One way is to increase the current each led receives when it is on. You can only do this safely if you have the data sheet for the exact model of led, and the data sheet says that you can exceed the maximum continuous current. The data sheet will tell you the maximum current for a short pulse (e.g. 80mA), how long that pulse can be (e.g. 100us) and how much gap there must be between pulses (e.g. 1000us). This then gives you two more problems: how to get the timing of your sketch to match those timings, and how to provide that 80mA, which exceeds the limit for the Arduino pins.
The second way is to light more than one led at a time. If your matrix is 16 LEDs and you can light 4 at a time, with 16mA each, then the average current drops to 4mA instead of 1mA, so the display is much brighter without needing to exceed the max continuous current of the LEDs. There is no problem having 4 pins source 16mA each to 4 LED anodes. But all that current has to be sunk by a single pin attached the the 4 cathodes of those LEDs, and that will exceed the max for a pin.
To get around that, you need external driver circuits to relieve the Arduino pins from all that current. These can be as simple as a transistor and base resistor for each pin sinking the current.
For larger matrices, even that is not enough. Although each pin can source or sink a maximum of 40mA, there are other limits, such as 100mA limits for certain groups of pins, and a 200mA limit for the ATmega chip overall. These limits depends on what model of Arduino you are using and what chip it is based on. So you may also need transistors to drive the led anodes and cathodes, to avoid overloading the chip.
At this point, the circuit has become very complex with many components. So if you are wanting a larger matrix, it is much easier to use a specialist chip designed for the purpose, such as max7219 or ht16k33. These off-load the problems of high currents from the Arduino chip, perform all the multiplexing so your sketch does not have to, make the whole circuit much simpler, and use only 2 or 3 Arduino pins.
This is a classic "XY Problem" - and excellent but superfluous contributions by PaulRB.
You simply do not use an Arduino on its own to drive a matrix. Makes no sense.
Just get a MAX7219. In fact, if you already have the matrix constructed and it is other than one of the common matrix modules, just get one of these kits
... which contains all the necessary components on a board on which you can simply omit to mount the sockets for the matrix supplied (it's cheap enough to ignore it) and wire it to your own matrix.
Paul__B:
This is a classic "XY Problem" - and excellent but superfluous contributions by PaulRB.You simply do not use an Arduino on its own to drive a matrix. Makes no sense.
Just get a MAX7219. In fact, if you already have the matrix constructed and it is other than one of the common matrix modules, just get one of these kits
... which contains all the necessary components on a board on which you can simply omit to mount the sockets for the matrix supplied (it's cheap enough to ignore it) and wire it to your own matrix.
its part of a bigger project for school so i wanted to avoid premade stuff as much as possible. Im not familiar with how Led matrix drivers work but ill look into it. Thank you both. Huge help.
vortix2950:
its part of a bigger project for school so i wanted to avoid premade stuff as much as possible.
It's a kit.