I want to make a very simple 2x2 LED Cube as one of my first projects with my arduino but am unsure how to start wiring the LED's together, whether it will need resistors or capacitors.
Would anybody be able to help me design the circuit and I will work out the code afterward?
P.S: if possible, please send me a fritzing file of it!
A 4 by 4 by 4 can be made using one of the MAX7219 matrix kits from eBay. You don't need the matrix display but it is jsut as cheap to buy the kit anyway, with which you have a nice ready-made PCB and can start with a 2 x 2 x 2 or 3 x 3 x 3 and progress.
You will need a soldering iron.
mitchK:
P.S: if possible, please send me a fritzing file of it!
Beyond that you really want to learn how BlinkWithoutDelay works whether from the Arduino Example sketch (included in your IDE under File->Examples->02.digital->BlinkWithoutDelay) or from the How To Projects Thread on the forum or from Nick Gammon's excellent blogs, the first one I list in my signature space below.
Why? Because you learn how to make many things work together "at the same time" by getting back to something later rather than wait and hold everything else up. 16 million cycles per second can keep a lot of plates in the air.
mitchK:
I want to make a very simple 2x2 LED Cube as one of my first projects with my arduino but am unsure how to start wiring the LED's together, whether it will need resistors or capacitors.
Would anybody be able to help me design the circuit and I will work out the code afterward?
P.S: if possible, please send me a fritzing file of it!
Alright, I'm not sure if you mean 2x2x2 [which is 8 LEDs] or 2x2 [which is 4]. I'll assume you mean 2x2 which is what you wrote at least twice.
See the image [Note: I updated schematic with a 3x2, but the code is for 2x2]:
The resistors I use for RED LEDs is about 330 Ohms. At 5V, I think it gives about 15mA of current, and that's perfect for long life/brightness. And for blue/green, I use 160 ohm, usually.
This method is multiplexing. You can have many more rows and columns. The basic principle is you connect all Anodes [ + ] of LEDs in each column together. I named them P1 and P2 for Positive. Then you connect all Cathodes [ - ] of LEDs in each row together. I named them N1 and N2 for Negative. Also note the resistor, you only need 2 resistors, not 4, because only 2 of these LEDs will ever be on at the same time. So if you had, let's say, 5x5 [25 LEDs], you only need 5x resistors! The drawback is, the more columns you add, the less time the LEDs are actually on. [5 columns = each row is on for 1/5 the time]
Now to turn any given LED on, you have to: a) make that column's positive pin Positive, b) make that row's negative pin Negative.
For the multiplexing part, the principal is: you may only ever have one positive [Or negative, depending on wiring] wire on at any given time, then you turn on all negative [or positive] pins you want, then you go to the next row. rinse and repeat
I'll give you an example:
int p1 = 3;
int p2 = 4;
int n1 = 5;
int n2 = 6;
int P[] = { p1, p2 }; //put into array, so we can loop through them
int N[] = { n1, n2};
void setup()
{
for(int i = 0; i < 2; i++)
{
digitalWrite(P[i], LOW); //write all pins low
digitalWrite(N[i], LOW);
}
}
bool pattern[2][] =
{
{true, false } //this is the pattern, the top-left LED on, top-right off
{false, true } //button-left off, bottom-right on
};
byte r = 0;
void loop()
{
//turn off all positives by making them low
for(int i = 0; i<2; i++)
{
digitalWrite(P[i], LOW);
}
//turn on each row that's on
for(int i = 0; i<2; i++)
{
if(pattern[r][i] == true)
digitalWrite(N[i], LOW); //Low means on for a negative pin
else
digitalWrite(N[i], HIGH); //high is off since for an LED to be on, P has to be HIGH and N has to be low
}
digitalWrite(P[r], HIGH); //turn on this column of LED
delay(10); //let the row stay lit for a small amount of time
r++; //go to next column
if(r >= 2) r = 0; //if we reach last column go to beginning
}
Ok, now the code isn't tested but it should be at least very close. You can easily make it greater [3x3, 4x4, 5x5, etc] by wiring in more LEDs, and changing the 2's in the for loops to 3, 4, or 5.
Also, this is just for one still image. If you want to have an animation, you just need to make the pattern[2][] into a 3D array:
and then implement a frame byte, and periodically increment the frame [with overflow reset, so it starts over from the beginnning.
Note: this code is just an example for you to hopefully understand how multiplexing works, and give you a starting point. This would be much better to put the loop() part into a Timer1 interrupt, and run it at regular intervals, while you can handle the frame increment logic and do other stuff in loop().
Also! there is more to it than just wiring this straight to the Arduino's pins. Let me explain current sourcing vs current sinking. You can use all IO pins as a voltage source or voltage sink. Let's say you put pin 2 HIGH, and pin 3 LOW. Connect a load to pin 2 and 3, and there is 5V going to it. The arduino's absolute max before damage starts happening is 40mA per pin. Sinking [pin is +], or sourcing [pin is -]. So you can easily source 1 LED on each pin, so you can connect each Positive column pin to 1 respective arduino pin. [Remember, only 1 LED will be on per resistor since you only turn one
You cannot, however, sink the current for more than 40mA on a pin. So if you had 15mA per LED, you can sink 2 LEDs on the same pin [2 LEDs have the cathode going to the same arduino pin, and each anode going to different arduino pins--30mA], but you cannot do 3 [45mA].
So, at this point you need to take each N1, N2, N3 etc. pin, and wire that to a NPN transistor or n-channel MOSFET. That way, you can control the ground of say 5 LEDs with 1 arduino pin.
I hope that I haven't made it too complicated, and let me know if you don't understand any part of this and I'll try to elaborate.