# Schematic and Arduino sketch that work

Help please =( , I have been spending hours looking at supposed "tutorials" on the internet showing how to construct a 3 by 3 LED cube. While the description of the construction seems quite well done and easy to follow, when it comes to providing a readable schematic and a sketch that will even upload without coming back with a multitude of errors, it is a different story. I realise that nobody owes me any of their valuable time in showing how to learn about these things but why waste peoples time showing them something that clearly will not ever work. I am no expert (rank , enthusiastic amatuer comes to mind) but if someone on this forum would be kind enough to point me in any legitimate direction I would be gratefull. Have a good day or night as the case may be, Pedro

Hello Pedro,

The Arduino project is all about tinkering, experimenting and learning, personally i find it hard to follow other peoples code, if something doesn't work as you expected investigate and find out why

My advise would be to first construct a 3x3x1 matrix and when you master that add another layer to make 3x3x2 then another 3x3x3, if you need to know how to multiplex i will try and help but i work and don't get much time

Hello P18F4550 and thanks for your reply. Yes I suppose that approaching it from simplifying a cube to a series of matrices makes sense.Thanks again.

If you could post the code you are trying to use and the schematic you are trying to use the members would probably bend over backwards to assist you in finding the faults in both.

Saying it does not work without something to go on is kind of vague at best.

One approach - drive 3 layers of LEDs with anodes connected in columns. Arduino drives LED anodes, ULN2003/2803, or discrete transistors, sink the up to180mA of current from up to 9 LEDs. Or 20mA if just 1 is on.

Each layer’s cathodes are connected together.
Drive the anodes, drive cathodes for layer 1.
Drive the anodes, drive cathodes for layer 2.
Drive the anodes, drive cathodes for layer 3.
Repeat every 5mS.
The rest of the time, read switches, button, serial port, etc to make decision about how the display will change.

Hello Crossroads. Cool name there, I love the blues too. Look I am a rank amatuer here and to be frank I do not understand your suggestion , although I’m sure it’s a very good one. P18F4550 suggested that I start with a 3 x 3 matrix and expand on that which is what I am now trying. First I have built a simple circuit to power one led via transistors switching the anode and cathode on and off via digital signals from the Arduino uno R2. I also wrote a sketch to operate the circuit and so far it seems to be all ok and working but although it works maybe I am kidding myself as to how it works if that makes sense?
If someone could cast an eye over my circuit and Arduino sketch I would be gratefull for any feedback (verbal, not electronic ? )
Thanks Pedro.

Two transistor LED switch.pdf (40.9 KB)

Hi Pedro, You're on the right track, the operation of the transistors is just flipped over with respect to the comments:

(Oh - put a resistor between arduino and the NPN base also)

``````   void setup ()                                 // run once, when the sketch starts
{
pinMode (12 , OUTPUT);             // sets the digital pin 12 as output - drives Base of PNP transistor
// Low here turns PNP on
pinMode (13 , OUTPUT);             // sets the digital pin 13 as output - drive Base of NPN transistor
// High here turns NPN on
}
void loop ()                                   // run over and over again
{
digitalWrite (12 , HIGH);               // sets the +ve side of led on via transistor >> PNP off
digitalWrite (13 , LOW);               // sets the -ve side of led on via transistor >> NPN off
// so LED is actually Off here
delay(1000);                                 // waits for one second
digitalWrite (12 , LOW);               // sets the +ve side of led off via transistor >> PNP on
digitalWrite (13 , HIGH);              // sets the -ve side of led off via transistor >> NPN on
// LED is actually On here
delay(1000);                                // waits for one second
}
``````

You could start with a 1x9 matrix (which I drew up) physically layed out in a 3x3 grid. Then put 2 more on top of that (the three rows I drew). The Anodes of the LEDs in each column get connected together, the cathodes in each layer get connected.

Thus you drive the anodes, and only drive 1 cathode on at a time to light up 1 row at a time. Repeat that quickly in software, turning some or all of the LEDs in each row for say 10mS at a time, and to the eye it will seem that all are being driven all the time.

Hello Crossroads, thanks for your help and I am glad that I'm basically thinking along the right lines.. I put a 4.7k resistor between the arduino and NPN base and the LED seems not quite as bright. Presumably these resistor values should be calculated depending on the specs of the LED's and transistors? I just picked resistor values that I have seen in similair circuits online without knowing the maths behind their values. Thanks again and have a great day Pedro.

I'd put 20ma in to make sure the transistor turns on hard. Vbe of many transistors is 0.7. So there'd be 4.3V across a base resistor. 4.3/.02 = 215 ohm, use a 220 ohm resistor.

Thanks Crossroads,I'll try that once I stop burning out LED's by connecting power to the wrong place on the breadboard (opps...)

I have been constructing a simple 3 x 3 LED matrix which I want to control with an Arduino R2. I want to be able to control the LED’s individually to create sequences, initially in this matrix and ultimately in a 3 x 3 x 3 cube. I realise that you can buy chips to do these sort of things but I am doing it to learn about electronics and the Arduino interface. I have attached a circuit diagram of the matrix connected to the Arduino.
When I connect the matrix, (including the transistors and resistors) without the Arduino, to 5v and earth for the approprite LED, they all work as expected.

Then I connected the LED matrix to Arduino pins 10, 11 and 12 to provide power and pins 4, 5 and 6 for grounds. I think this is where it is becoming confusing for me, the concept of digital signals and the subsequent coding. I assumed that if I set pin 12 HIGH and pin 6 LOW then LED 1 would light which it does as long as it is the only LED actually connected to the Arduino via D 12 and D 6 . If I connect up more than one “pair” of wires (power and ground for relevant LED) I get more than one LED flashing even if I use basic code such as ;

void setup ()
{

pinMode (4 , OUTPUT);
pinMode (5 , OUTPUT);
pinMode (6 , OUTPUT);
pinMode (10 , OUTPUT);
pinMode (11 , OUTPUT);
pinMode (12 , OUTPUT);

}
void loop ()
{
digitalWrite( 12 , HIGH);
digitalWrite ( 6 , LOW);
delay(1000);
digitalWrite( 12 , LOW);
digitalWrite ( 6 , HIGH);

delay(1000);
}

I get LED’s 1, 4 and 7 flashing with all connections made between matrix and Arduino. But with all connections and the following code where I have not set digital pins 4 and 5 as OUTPUTS, I get LED’s 1, 2 and 3 flashing alternately. But obviously this is not allowing me to access LED’s 4 through 9.

pinMode (6 , OUTPUT);
pinMode (10 , OUTPUT);
pinMode (11 , OUTPUT);
pinMode (12 , OUTPUT);

}
void loop ()
{
digitalWrite( 12 , HIGH); // sets led 1 on
digitalWrite ( 6 , LOW);
delay(1000);
digitalWrite( 12 , LOW); // sets led 1 off
digitalWrite ( 6 , HIGH);
delay(1000);
digitalWrite( 11 , HIGH); // sets led 2 on
digitalWrite ( 6 , LOW);
delay(1000);
digitalWrite( 11 , LOW); // sets led 2 off
digitalWrite ( 6 , HIGH);
delay(1000);
digitalWrite( 10 , HIGH); // sets led 3 on
digitalWrite ( 6 , LOW);
delay(1000);
digitalWrite( 10 , LOW); // sets led 3 off
digitalWrite ( 6 , HIGH);
delay(1000);
}

As I have previously stated I am a beginner at all this and I am suspecting that this problem has something to do with the last state of some of the pins as my circuit tests o.k. with straight analog input. It is all very confusing I must say and would appreciate it if someone could point me in the right direction.
Thanking you Pedro.

In relation to my last post I don,t think that this attached Pedro.

Photo circuit .doc (57 KB)

Sorry I,m making a botch of this post… The .png file that I attached is a bit hard to scroll on so here is a .jpg file that you can zoom in on . Pedro (again)

Pins 10,11,12 you have controlling PNP transistors - PNPs are On when the Base is Low, and Off when the base is high. You need to adjust your logic to reflect that. 10,11,12 Low AND 4,5,6 High will turn on LEDs.

Either 10,11,12 High OR 4,5,6 Low will turn off LEDs.

Hello Crossroads. I’ll modify my thinking to get my head around that and see how I go. Thanks so much for all your help you are a generous man with your time on this forum, Pedro.

Sigh... still having no luck, but then I checked my circuit and I have a schematic for PNP transistors being contolled by pins 10, 11 and 12 but the 2N2222 transistors I am using are NPN type. Also I have schematic for the NPN transistors being controlled by pins 4, 5 and 6 but the 2N2907 transistors I am using are PNP type. Help I am even more confused that I was before (might just buy a cheap set of christmas tree lights and be done with it ...)

If I put this code;

void setup () {

pinMode (4 , OUTPUT); pinMode (5 , OUTPUT); pinMode (6 , OUTPUT); pinMode (10 , OUTPUT); pinMode (11 , OUTPUT); pinMode (12 , OUTPUT);

} void loop ()

{

digitalWrite( 12 , LOW); digitalWrite( 6 , HIGH); delay(1000); digitalWrite( 12 , HIGH); digitalWrite( 6 , LOW);

delay(1000);

}

with all connection between LED matrix and Arduino LED's 1 ,5 and 7 flash. The same as when I had ;

void setup () {

pinMode (4 , OUTPUT); pinMode (5 , OUTPUT); pinMode (6 , OUTPUT); pinMode (10 , OUTPUT); pinMode (11 , OUTPUT); pinMode (12 , OUTPUT);

} void loop () { digitalWrite( 12 , HIGH); digitalWrite ( 6 , LOW); delay(1000); digitalWrite( 12 , LOW); digitalWrite ( 6 , HIGH);

delay(1000); }

Is the fact that I have used the wrong type of transistors making my hair greyer than it was this morning, thanks Pedro.

You're quite welcome. Good luck.

There are several errors in the drawing as shown… The NPN are shown as emitter ffollowers and there are 2 issues there, 1. the emitter cant ever go higher than the base, in this case for a functioning transistor the emitter MUST be .7 V lower than the collector (5V source) so we now have a max supply that is ~ 15% lower than it should be and 2. while the emitter current is the SUM of the collector and base currents the available voltage for series led’s is lower making a good power supply a must. For optimal results switch the NPN’s and PNP’s respecting polarity… THe emitter of a PNP is the most positive element of the transistor and to turn it on you must “Pull” current from the base to ground so remember to include a 1K resistor in the base circuit and good design practice would have a 10K pull-up from base to Vcc as this prevents uncertain (read Floating) base connections from turning on the PNP transistor and use the NPN’s to Sink current and use a 470R resistor for the base current limiting resistor again good practice would have a 10K resistor from base to ground. When connected in this manner both the PNP’s and the NPN’s are operating as true switches and the only losses will be ohmic (wire/connection) resistance and the Vce sat of the transistors. Were it me, for light loads (under 150 Ma/device) I would choose a 2SC945 for the NPN and a 2N3906 for the PNP for 2 reasons 1. the '945 has a typ dc gain of ~ 100 @ 150 mA Ic and the '3906 although a lower gain device still has the base current flowing in the emitter lead of the device thus extra base current will be dissipated in the LED rather than to ground as your drawing clearly shows and the smaller the value of base resistor the more wasted current. Never assume that the port of a microcontroller is responsible for insuring that the state of any connection to it. Use Pull-ups (5 10K) and pull downs of the same value. Pull UP PNP’s and Pull DOWN NPN’s… finally P & N channel mosfets would be my preferred devices for power control of that type… 5A P Ch mosfets can be had for 25 -50 cents a piece… pretty much the same for N channel devices in the same current range. OR one could use TPIB595’s and ULN2803’s for higher currents and simplified wiring.

Doc

"Is the fact that I have used the wrong type of transistors making my hair greyer than it was this morning"

Yes.

``````void setup ()
{

pinMode (4 , OUTPUT);
pinMode (5 , OUTPUT);
pinMode (6 , OUTPUT);
pinMode (10 , OUTPUT);
pinMode (11 , OUTPUT);
pinMode (12 , OUTPUT);

}
void loop ()

{

digitalWrite( 12 , LOW);
digitalWrite( 6 , HIGH);
delay(1000);
digitalWrite( 12 , HIGH);
digitalWrite( 6 , LOW);

delay(1000);

}
``````

would work with the schematic you drew, now you just need to wire up the correct parts.

Thanks again Crossroads, I had a good long hard look at what I have done and with your help I finally got it to work as I set out to. Do you think that I should construct a new circuit, matching what I had in the original schematic with Pins 10,11,12 controlling PNP transistors and Pins 4, 5, 6 controlling NPN transistors rather than how they currently are, or doesn,t it matter? Something tells me that as my physical circuit currently is, it is incorrect to have current sinking transistors to pins 10, 11 ,12 and current sourcing transistors to pins 4, 5, 6. It's all very confusing to the uninitiated I can tell you. Also (I realise that I might be pushing my luck here) now that I have a 3 x 3 matrix working using six Arduino pins what is the next step to controlling a 3 x 3 x 3 cube. From what I have read it involves using shift registers? Would it be possible for you to point me in the right direction, keeping in mind that I am a rank novice to all this. Thanks again for your help Pedro.