Coding Question for Project

Hello everyone!

I am attempting my first project making an LED Cube its 4 X 4 X 4. I have Built it and got it running for the most part but I am not sure if the coding is correctly implemented. Heres a piece of test code I have written to test all the columns of lights.

int led1 = 4;
int led2 = 5;
int led3 = 6;
int led4 = 7;
int gnd1 = 8;
int gnd2 = 9;
int gnd3 = 10;
int gnd4 = 11;



void setup()                    // run once, when the sketch starts
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
 
  
  digitalWrite(gnd1, LOW);
  digitalWrite(gnd2, LOW);
  digitalWrite(gnd3, LOW);
  digitalWrite(gnd4, LOW);
}


void loop()                     // run over and over again
{
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  
  pinMode(gnd1, OUTPUT);
delay(500);
  pinMode(gnd1,INPUT);
 
 pinMode(gnd2, OUTPUT);
delay(500);
  pinMode(gnd2,INPUT);

  pinMode(gnd3, OUTPUT);
delay(500);
  pinMode(gnd3,INPUT);
  
  pinMode(gnd4, OUTPUT); 
delay(500);
  pinMode(gnd4,INPUT);
  
 
}

Moderator edit: Code tags added

Just for a little clarification led variables are my columns and gnd variables are for levels of the cube. So I have 4 columns on and each level goes on and off for delay 500. Now it works fine but im not sure if there is a chance I might have of blowing up my arduino board lol. Basically what I want to know is what is the safer bet to turn off and on pins essentially. I need to be able to manipulate the ground source mostly while keeping the columns set to high.

a> Have my voltage source set to high then my ground source also go high which iirc would make two colliding voltage sources of equal voltage which then makes the total 0?

b> Have my voltage source set to high then my ground source go from output of low to just an input?

c>Better Idea? =p

A and B both seem to work but not sure if there a possibility of damaging a component for sure or not.

Thank you!

I don't think you need to keep changing the pinMode - just write them HIGH or LOW.

HIGH on both sides of LED turns it off.
HIGH on Cathode, LOW on anode turns it off.
LOW on Cathode, LOW on anode turns it off.

To prevent ghosting, I would suggest:
Cathodes off, anodes on, cathode_level1 on, pause.
Repeat for next level2, then 3, then 4.

What is sinking the Ground current? Hopefully not the Arduino pins? That will make them fail eventually unless you only have 20mA/16 to 40mA/16 going thru each column.
Having a ULN2003/2803 sink that current would be much better.

Hey thanks for responding,

I am using the arduino pins as ground, from my understanding as long as I kept the current down to those levels it would be fine. I have each column in series with a 1k resistor which should bring it down to 5 mA way below the acceptable limit. Am I correct in this thinking? I will look into this other component you posted as I am unfamiliar with it.

Also the ways you listed is using the properties of a diode correct to turn off and on?

"I have each column in series with a 1k resistor which should bring it down to 5 mA way below the acceptable limit. Am I correct in this thinking?"
Depends. If you are only driving 1 row with that 1 column, then yes.

Diode turn on/off - yes, those are correct. Diodes can have the same voltage on each pin, or 5V on cathode/Gnd on Anode (reverse bias); and to turn on, a higher anode voltage than the cathode (forward bias).

Yes, I have 16 pins driving 1 column each.

Ok great thanks for the info!

Is this what you have?
If you don't have the transistors (or a part like ULN2003 or ULN2803) than the arduino is not capable of sinking all 16 LED at 5mA.
Well, it will for a while, but at >40mA total it won't for very long.

Hi sorry it took me so long to reply. I did not have the transistors in my circuit but I have been doing more reading and seeing that they are needed. Im still not quite sure how they work in the circuit. I understand how the transistor works as a component but not how it contributes. At the moment I just have the leds multiplexed with resistors, ill make sure not to run it till I understand more whats going on.

Thanks

The arduino has little transistors, they get hot quick and will burn up. The external transistors are bigger & heftier and can handle more current without burning up.
With a matrix, you can see that you have 16 outputs going into 1 input - and guess what, that 1 input is not going to last long.

The other option is to only allow 1 output at a time for that 1 input, that really slows down the display refresh time and makes for a much dimmer display.
A part like MAX7219/7221 uses custom hardware doing nothing else and refreshes an 8x8 display at an 800 Hz rate while receiving data to display via the SPI interface.
If you update each column and let it display for 1mS, that's a display refresh every 8mS, or a 125Hz refresh rate. Leave each column on 2mS for 62.5 ZHz refresh rate. 2mS gives you lots of time, relativey, to do stuff in between column refreshes. Leave each column on for 4mS, so 32mS total for a 31.25 Hz refresh rate. Should still look flicker free (movies are 24 Hz supposedly, TV 30 Hz - tho my new one says its doing 240 Hz with lots of processing to fill in somehow.

Either way, you need transistors to sink all the current the outputs are putting out.

Alright so this is going to sound like a stupid question but when you refer to sinking the current is that similar to when im grounding the current in the arduino?
So if im using 4 pins to ground each level of my cube I would need a transistor attached to each of those with a pin connected to the base of each transistor? I have trouble understanding grounding essentially haha.

Hey also wanted to ask you a quick electrical question, in our labs at school we have a voltage source for like 6v and 25v etc. What would be the best cheap alternative to something like for home use. I was working on a 555 timer circuit and wanted to be able to use to work on it at home I would assume I can use the the arduino as a voltage source but if i wanted higher then 5v or if i didnt want to damage the arduino doing something wrong lol.

Sinking current, effect is same as connecting pin to ground in a 5V/Gnd system. Yes, 1 transister per layey/level as I drew it up.

I use these power adapters/wallwarts all the time for projects.

Typically the 5V 1 or 2A, to supply 5V directly (such as into the power header).
The 7.5V to supply power to go into the barrel jack.
The 12V when I want to power strings of LEDs and have a transistor (NPN like above) or transistor array (ULN2003/ULN2803) to sink current for banks of LEDs.
I used to do a lot of analog stuff and was always assembling +/-9V, +/-12V, even +/-15 V supplies for op amps, DACs, ADCs.
So I would end up needing a 5V high current supply for logic & control signals, and the +/- supplies for the analog stuff.
A lot of those parts have evolved into single supply parts now.

If you did need a multi output supply, you could consider one of these
http://www.mpja.com/5V-20A-and-12V-4A-and-12V-1A-Power-One-Power-Supply/productinfo/18835+PS/
http://www.mpja.com/5V-12V-12V-24V-Astec-Power-Supply/productinfo/14066+PS/
http://www.mpja.com/33V5V-12V-5V-12V-12V-and-Power-One-Power-Supply/productinfo/17847+PS/

Thank you for the awesome info, I have only finished my first circuits class so still learning we havnt got into transistors last thing was op amps.

Last quick question, In your schematic it shows it going to a digital ground I assume by the arrow after the line of transistors. What do you ground things to when say you need a ground point. For example if I have a transistor and I want to sink current and I have my transistor on a breadboard what do I connect my ground to essentially? If that makes sense. I see the arduino has a dedicated ground but what If im not using arduino and just making a circuit and I want to complete my circuit. I know my programmable power supply at school had a ground associated with its voltage source.

All the grounds need to connect together. That can be one rail of the breadboard.

Ah ok so when they refer to ground it just hast to be to something metal I assume, such as a rail on a breadboard? Feel like im making this more complicated then it should be lol. When they refer to earth ground, analog ground, digital ground it kind of just confuses me.

No, ground usually means the negative terminal of the battery, or the power supply, that is supplying power.
There may be different voltage sources - all the grounds must be connected so all components have the same reference point.

Earth ground usually refers to the green wire in a 3-wire power cord that does end up connecting to the earth. For safety reasons, the outer metal enclosure of electronics is connected to earth ground.

Analog ground and Digital ground will be portions of the circuit board that can be separated, but do get connected at one common point eventually, like the last part of the circuit that goes to the battery- or the power supply. They may kept apart for noise reasons in critical analog sensitive circuits.

I would say for most arduino related things there is no splitting of analog ground and digital ground, there is just ground, one pin on the power header and one pin on the other side of the board, connected to the barrel jack and the USB port and the ICSP connector.

Just wanted to say thank you for all the information you have been a great help.

just to clarify something

I use these power adapters/wallwarts all the time for projects.

Typically the 5V 1 or 2A, to supply 5V directly (such as into the power header).
The 7.5V to supply power to go into the barrel jack.
The 12V when I want to power strings of LEDs and have a transistor (NPN like above) or transistor array (ULN2003/ULN2803) to sink current for banks of LEDs.

I understand the 7.5v into the barrel jack of the arduino I assumed yes?

the 5v not sure what the power header, maybe I just dont know the name your calling it.

Last when you are working with these and your not plugging it into a barrel jack do you just cut off the connector plug and work with the wires?

Power Header - the 6 pins with +5v, 3.3V, Vin, Gnd, Gnd, Reset.(I guess 8 pins now, 2 more added on the most recent boards)

7.5V into the barrel jack.

I work with the wires, usually into screw terminals on a custom board, or right to the power pin on a board like a pro mini.