Led Matrix problem

Or I'm doing something wrong or I don't understand something.

I got 6x leds, 2 col., 3 row.
The pins I'm using and how they are arranged:

Led Matrix: Dp 0,1,2,3,4.
Green1 : 3,0 | Red1 : 4,0
Green2 : 3,1 | Red2 : 4,1
Green3 : 3,2 | Red3 : 4,2

So I wrote a simple code to test the leds and at start worked well but after doing some
talks that I need turns out that or I'm doing something wrong or I don't understand something.

Code:

int Redgnd = 4;
int Greengnd = 3;
int red1 = 0;
int green1 = 0;


void setup() {                

  pinMode(Redgnd, OUTPUT);  
  pinMode(Greengnd, OUTPUT);   
 pinMode(red1, OUTPUT);  
 pinMode(green1, OUTPUT);
}


void loop() {
       
       digitalWrite(Redgnd, LOW);
       digitalWrite(red1, HIGH);
       delay(500);
       
       digitalWrite(Redgnd, LOW);
       digitalWrite(red1, LOW);
       delay(500);
   
}

What I get in the code is Blink the Red1 led and only Red1 but looks like Green1 blinks as well, is this because the Digital pin (Greengnd) is defined as LOW/GND from the beginning?

I'd like to post more "complex" things but I want to figure out this one 1st.

I re-wrote the code found it confused to myself as well :smiley:
Done a paper design to understand better and wrote this:

Code:

int GND_0 = 0;
int GND_1 = 1;
int GND_2 = 2;
int Red = 4;
int Green = 3;


void setup() {                

  pinMode(GND_0, OUTPUT);  
  pinMode(GND_1, OUTPUT);   
  pinMode(GND_2, OUTPUT);  
  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
}

void loop() {
       
       digitalWrite(GND_0, LOW);
       digitalWrite(Red, HIGH);
       delay(500);
       
       digitalWrite(GND_0, LOW);
       digitalWrite(Red, LOW);
       delay(500);  
}
}

try adding

digitalWrite(Greengnd, HIGH);

in the setup function.

EDIT:
int GND_0 = 4;
int GND_1 = 3;
int GND_2 = 0;
int Red = 4;
int Green = 3;

This is really confusing, before you had 2 ground lines, now you have 3, and you have pins 3 and 4 doing 2 things? This is very confusing...

That's the normal code:

int GND_0 = 0;
int GND_1 = 1;
int GND_2 = 2;
int Red = 4;
int Green = 3;

I just made a mistake while copy/pasting

well my advice stays the same, set all your grounds to HIGH first. Otherwise they are default LOW and you have a connection, which explains the multiple LEDs going on.

The problem using this code right now is that even if I use
Row:
0
1
2
and Col. 4, 3

Leds:
1 - 2
3 - 4
5 - 6

If I blink 0(LOW) - 4(HIGH)
0(LOW) - 4(LOW) which is led nr.1 then all the col. will blink as well, leds 1,3,5
but If I try to put the grounds(1,2) to HIGH how I'm gonna light up in the same time the 4 and 6 leds?

Code:

int GND_0 = 0;
int GND_1 = 1;
int GND_2 = 2;
int Red = 4;
int Green = 3;


void setup() {                

  pinMode(GND_0, OUTPUT);  
  pinMode(GND_1, OUTPUT);   
  pinMode(GND_2, OUTPUT);  
  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
}

void loop() {
       
       digitalWrite(GND_0, LOW);
       digitalWrite(Red, HIGH);
       
       delay(500);
       
       digitalWrite(GND_0, LOW);
       digitalWrite(Red, LOW);
       delay(500);

Can you first try adding

digitalWrite(GND_0, HIGH);
digitalWrite(GND_1, HIGH);
digitalWrite(GND_2, HIGH);

in the setup function and see what happens
Just start simple, start with just getting 1 led on at a time

I think I found the solution to my problem:

_4 _3
0 - G1 - R1

1 - G2 - R2

2 - G3 - R3

Now this is how my leds are right now (Green leds, Red Leds) and pins
I thought If I use the pin 4 always as LOW and pin 3 always as HIGH then I can simple change the
leds in rows to light up G1 or R1 by simple changing the pin 0 to HIGH or LOW and same for the others, good idea right?

Code:

int GND_0 = 0; // LOW/HIGH
int GND_1 = 1; //LOW/HIGH
int GND_2 = 2; //LOW/HIGH
int Red = 4; //always LOW
int Green = 3; //always HIGH


void setup() {                

  pinMode(GND_0, OUTPUT);  
  pinMode(GND_1, OUTPUT);   
  pinMode(GND_2, OUTPUT);  
  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
}

void loop() {
       
       digitalWrite(GND_0, HIGH);
       digitalWrite(Green, HIGH);
       digitalWrite(Red, LOW);
       
       delay(500);
       
       digitalWrite(GND_0, LOW);
       digitalWrite(Green, HIGH);
       digitalWrite(Red, LOW);
       delay(500);  
}

For starters, your diagram shows pin 3 as reds, but your code defines pin 3 as green.
Second of all, if Red is always LOW (assuming you meant pin 3 like in your diagram), then R1,R2,R3 have no power and cannot light up

Let me explain how I want to make them light up, I got 6 leds 3x red 3x green, i got as well 3x switches (but that's another story) what I want to do is while all switches are OFF all red leds are HIGH if I turn a switch ON then the red/green leds of that switch (red will turn off and green leds will light up).

That's the basic idea of what I want to do.

A question : I'm using the Digital pins 0 and 1 which they are for RX/TX can they affect my leds? because I tested this code:

int GND_0 = 0; // LOW/HIGH
int GND_1 = 1; //LOW/HIGH
int GND_2 = 2; //LOW/HIGH
int Red = 4; //always LOW
int Green = 3; //always HIGH


void setup() {                

  pinMode(GND_0, OUTPUT);  
  pinMode(GND_1, OUTPUT);   
  pinMode(GND_2, OUTPUT);  
  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
}

void loop() {
       
       digitalWrite(GND_0, HIGH);
       digitalWrite(GND_1, HIGH);
       digitalWrite(GND_2, HIGH);
       digitalWrite(Green, HIGH);
       digitalWrite(Red, LOW);
       
       delay(500);
       
       digitalWrite(GND_0, LOW);
       digitalWrite(GND_1, LOW);
       digitalWrite(GND_2, LOW);
       digitalWrite(Green, HIGH);
       digitalWrite(Red, LOW);
       delay(500);  
}

and not all my leds are lighting up (I put the polarity right) planing to use Serial communications are well.

Its just generally good idea not to use the Serial pins

int gndPin0 = 0;
int gndPin1 = 1;
int gndPin2 = 2;
int redPin = 4;
int greenPin = 3;


void setup() {                

  pinMode(gndPin0, OUTPUT);  
  pinMode(gndPin1, OUTPUT);   
  pinMode(gndPin2, OUTPUT);  
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(gndPin0, HIGH);
  digitalWrite(gndPin1, HIGH);
  digitalWrite(gndPin2, HIGH);
}

void loop() {
       
       digitalWrite(gndPin0, LOW); //connect the ground for row0
       digitalWrite(greenPin, HIGH); //turn row0 green LED on
       digitalWrite(redPin, LOW); //turn row0 red LED off
       delay(500);
       
       //digitalWrite(gndPin0, LOW); //connect the ground for row0
       //^^^its still connected until you turn it HIGH
       digitalWrite(greenPin, LOW); //turn row0 green LED off
       digitalWrite(redPin, HIGH); //turn row0 red LED on
       delay(500);
}

The >>

digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(gndPin0, HIGH);
digitalWrite(gndPin1, HIGH);
digitalWrite(gndPin2, HIGH);

in setup they gonna stay like that for ever? I mean even if i use them once they gonna turn back to setup state? I want to use them multiple times so I'm thinking to leave 3 leds and use only the other 3.

       digitalWrite(gndPin0, LOW); //connect the ground for row0
       digitalWrite(greenPin, HIGH); //turn row0 green LED on
       digitalWrite(redPin, LOW); //turn row0 red LED off
       delay(500);
       
       //digitalWrite(gndPin0, LOW); //connect the ground for row0
       //^^^its still connected until you turn it HIGH
       digitalWrite(greenPin, LOW); //turn row0 green LED off
       digitalWrite(redPin, HIGH); //turn row0 red LED on
       delay(500);
}

I done the same thing as you did :smiley: but my point here is to use all leds at once turning ON/OFF which one I want any time i want but looks like i can't do that.

The reason I put that code in the setup function is because I want your program to have a default state, and that state is everything off. If you start your program without a default state, you will most often get unexpected behavior, in your case LEDs will be on when you don't want them to be.

If you want to turn any !!SINGLE!! LED on/off, then one way of doing this is

//would be better if you declared your grounds in an array, like this
byte gndPin[3] = {0, 1, 2};
...
void led(byte row, byte colorPin, boolean state){
  //========
  //You can optimize this code to be more efficient, but I'm just giving you an idea
  //Turn everything off
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(gndPin[0], HIGH);
  digitalWrite(gndPin[1], HIGH);
  digitalWrite(gndPin[2], HIGH);
  //========
  digitalWrite(gndPin[row], LOW); //connect ground to our row
  digitalWrite(colorPin, state); //turn led on/off. Also, in your case colorPin == greenPin or redPin
}

The reason I say a SINGLE led is because you are using a matrix and multiplexing, which has its limitations (you can get around this, but its a little more complicated). For example, you cannot turn on R1 and G2 without R2 and G1 also turning on, that's just the nature of multiplexing a matrix.