Arduino and 4x4 led matrix

Im new to this forum and to the arduino. Ive managed to do alot of the basics, but i need help with my next learning project. the 4x4 led grid.

The leds are arranged and soldered the right way i think. I have now 4 anodes and 4 cathodes. I connect the anodes to the ardiuno trough some resistors. Where do i put the cathodes? in the digital pins? how do i program them to be “ground”? what am i missing here.

Appreciate your help.

Ole Stavne

Did you see this?
http://www.arduino.cc/playground/Main/DirectDriveLEDMatrix
Easy to adapt to your 4x4.

Thanks alot. I have been looking at that one, but cant quite figure out what to change and what to not. Some of the programming is hard to understand.

ive tried adapting the code with no success. can someone pls help me? pls post a code that works or give me some clues at least. if i connect a the positive end of a led at pin 3, and the negative lead for that led on pin 4. what whould be the code to turn it on?

sorry for my noobish question, but we have to start somewhere right? not einstein in a day.

Ole Stavne

gotta start somewhere. the playground is a very good source for learning new things.

lol. I’ve done all the beginner projects from the book that came with my arduino. Blink uses a led connected to ground. thats simple. Ive made a 6 led array and done the knight rider loop. but all the leds there are connected to ground.

What to do if i use a digital pin as ground?
As i said earlier. positive on pin 3 and negative on pin 4. There should be a way to turn the led on.

What are the 4 wires connected to the analog in pins in the picture on the site: http://www.arduino.cc/playground/Main/DirectDriveLEDMatrix

‘As i said earlier. positive on pin 3 and negative on pin 4. There should be a way to turn the led on.’

Set pin3 to output, pin 4 to input,
then

digitalwrite pin3 high
digitalwrite pin4 low

the led should illuminate.

Doesnt work. Only when i wire the catode to ground do i get light. Using pin 3 as positive and 4 as negative doesnt work for me.

This is how far ive gotten on lighting a led this way:

int ledPin = 3;  
int ledOut = 4;

void setup()                   
{
  pinMode(ledPin, OUTPUT);      
  pinMode(ledOut, INPUT);
}

void loop()                    
{
  digitalWrite(ledPin, HIGH);   
  digitalWrite(ledOut, LOW);    
  }

int ledPin = 3;
int ledOut = 11; <<<< ?

Are you using digital pins 3 & 4?

Try just the digital pins with one resistor & led connected.

Edit. Hey your post changed while I was answering :slight_smile:

hehe. i saw that. i tried another pin to check if that worked, pasted the wrong code. modified now. Should be 4.

OOPS! Both pins need to be OUTPUTS,

I knew I had done this, I couldn’t remember how.

Finally. Thanks alot. Been trying to do this for days. and it was so easy. This wont be the last time im asking noob questions here…

ive programmed a lit more:

int ledPin1 = 3;  
int ledPin2 = 5;
int ledPin3 = 6;
int ledPin4 = 9;
int ledOut1 = 2;
int ledOut2 = 4;
int ledOut3 = 7;
int ledOut4 = 8;
int timer = 500;
void setup()                   
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledOut1, OUTPUT);
  pinMode(ledOut2, OUTPUT);
  pinMode(ledOut3, OUTPUT);
  pinMode(ledOut4, OUTPUT);
}

void loop()                    
{
  //første linje
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledOut1, LOW);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, LOW);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, LOW);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, LOW);
  delay(timer);
  
  // andre linje
  
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledOut1, LOW);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, LOW);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, LOW);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, LOW);
  delay(timer);
  
  //linje 3
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledOut1, LOW);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, LOW);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, LOW);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, LOW);
  delay(timer);
  
  //linje 4
  
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledOut1, LOW);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, LOW);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, LOW);
  digitalWrite(ledOut4, HIGH);
  delay(timer);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledOut1, HIGH);
  digitalWrite(ledOut2, HIGH);
  digitalWrite(ledOut3, HIGH);
  digitalWrite(ledOut4, LOW);
  delay(timer);
  

  
 }

this codes runs trough all the leds turning the on and of, line for line.
THere must be a way to do this much easier with easier code. and how do i make more “led modes” with more patterns. pls post example code someone.

Kanskje noe som dette hjelper: :wink:

const int TIMER = 500;
const byte NUMBER_OF_PINS = 4;
const byte NUMBER_OF_OUTS = 4;

byte ledPin[NUMBER_OF_PINS] = {3,5,6,9};
byte ledOut[NUMBER_OF_OUTS] = {2,4,7,8};

void setup(){
//set all pins as output
for (byte i=0; i<NUMBER_OF_PINS; i++){
pinMode(ledPin*, OUTPUT);*

  • }*
  • for (byte i=0; i<NUMBER_OF_OUTS; i++){*
    _ pinMode(ledOut*, OUTPUT);_
    _
    }_
    _
    }_
    void loop(){
    _
    //_
    for (byte pin=0; pin<NUMBER_OF_PINS; pin++){
    _
    digitalWrite(ledPin[pin],HIGH);_
    for (byte out=0; out<NUMBER_OF_OUTS; out++){
    _
    digitalWrite(ledOut[out],HIGH);_
    _
    delay(TIMER);_
    _
    digitalWrite(ledOut[out],LOW);_
    _
    }_
    _
    digitalWrite(ledPin[pin],LOW);_
    _
    }_
    _
    } [/quote]*_
    I think this should be close to what you want to do :slight_smile:

We are on the right track. the blinking on the leds seems a litte random and inverted. i wish i could understand all the arabic in the code. example:

[/code] void loop(){
//
for (byte pin=0; pin<NUMBER_OF_PINS; pin++){
digitalWrite(ledPin[pin],HIGH);
for (byte out=0; out<NUMBER_OF_OUTS; out++){
digitalWrite(ledOut[out],HIGH);
delay(TIMER);
digitalWrite(ledOut[out],LOW);
}
digitalWrite(ledPin[pin],LOW);
[/code]

My next project is controlling a a particular led in the grid from a Processing “program”. Maybe the pattern making will be easier there. I want more patterns, maybe som letters. random animations.

BTW. really sorry taking up forum space on these noob questions. but we all gotta start somewhere. my dream is btw hooking up some lcd’s and program an ui of some kind on that. no particular prosject atm. But im saving for an Epilog laser. Then the projects will come at a steady pace.

thx again for being so nice taking your time helping me out. even thought the questions could be a litte more pro.

I commented a bit. Tried to translate arabic to english :wink:

//constants, these will not change
const byte NUMBER_OF_PINS = 4; //how many ‘pins’ are we going to use?
const byte NUMBER_OF_OUTS = 4; //how many ‘outs’ are we going to use?
const int TIMER = 500; //how many millisecons should a given LED stay on?

//http://arduino.cc/en/Reference/Array
byte ledPin[NUMBER_OF_PINS] = { 3, 5, 6, 9}; //CHANGE THIS TO SUIT YOUR NEEDS
byte ledOut[NUMBER_OF_OUTS] = { 2, 4, 7, 8}; //CHANGE THIS TO SUIT YOUR NEEDS

void setup(){
//http://arduino.cc/en/Reference/For
//set all pins as output
for (byte i=0; i<NUMBER_OF_PINS; i++){
pinMode(ledPin*, OUTPUT);*

  • }*
  • for (byte i=0; i<NUMBER_OF_OUTS; i++){*
    _ pinMode(ledOut*, OUTPUT);_
    _
    }_
    _
    }_
    _/
    * Basically what happenes is this:*
    * 1) Loop through all pins [the pin number is defined in an array called ledPin]*
    * 2) Set the pin at current pin index HIGH*
    * 3) Loop throught all outs [the pin number is defined in an array called ledOut]*
    * 4) Set the pin at current out index HIGH*
    * 5) delay*
    * 6) Set the pin at current out index LOW*
    * 7) Increase index, so that we operate on next out*
    * End*
    * 8) Set the pin at current pin index LOW*
    * 9) Increase index, so that we operate on next pin*
    * End*
    /_
    void loop(){
    _
    //1) Loop through all pins [the pin number is defined in an array called ledPin]_
    for (byte p=0; p<NUMBER_OF_PINS; p++){
    _
    //2) Set the pin at current pin index HIGH*_
    * digitalWrite(ledPin[p],HIGH);*
    * //3) Loop throught all outs [the pin number is defined in an array called ledOut]*
    * for (byte o=0; o<NUMBER_OF_OUTS; o++){
    _
    //4) Set the pin at current out index HIGH*_
    * digitalWrite(ledOut[o],HIGH);*
    * //5) delay*
    * delay(TIMER);*
    * // 6) Set the pin at current out index LOW*
    * digitalWrite(ledOut[o],LOW);*
    * //7) Increase index, so that we operate on next out*
    * //This is done by the for loop*
    * }*
    * //8) Set the pin at current pin index LOW*
    * digitalWrite(ledPin[p],LOW);*
    * //9) Increase index, so that we operate on next pin*
    * //This is done by the for loop*
    * }*
    }[/quote]
    I’ve kind of tested it, and it seems to work.
    Does it do what you want? Or, what does it do? And what does it not?

thanks alot for taking your time making comments. they where more than helpfull.