How would I go about coding my Matrix?(SOLVED)

I tried using this sample code but it doesn't work. It ignores the data array and just turns on all the LEDS. My set up is one 595 shift register that controls PNP transistors while the Cathodes of the LEDS are tied to the Arduino. I can turn on a row by setting the pinstate to low so that the Arduino sinks current but I can't control single LEDS. Any help?

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 6;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 7;

int rowArr[8] = {5,4,3,2,8,9,10,11}; // Pins where each Cathode is connected

byte data;
byte dataArray[8];

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  Serial.begin(9600);
  pinMode(5,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);

  //Binary notation as comment
  dataArray[0] = 0xFF;
  dataArray[1] = 0xFE;
  dataArray[2] = 0xFE;
  dataArray[3] = 0xFE;
  dataArray[4] = 0xFE;
  dataArray[5] = 0xFF;
  dataArray[6] = 0xFF;
  dataArray[7] = 0xFE;


  digitalWrite(5,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(2,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
  digitalWrite(11,HIGH);
 
}

void loop() {

  for (int j = 0; j < 8; j++) {
    //load the light sequence you want from array
    data = dataArray[j];
    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, 0);
    //move 'em out
    shiftOut(dataPin, clockPin, data);
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
          digitalWrite(rowArr[j],LOW);
    delay(25);
          digitalWrite(rowArr[j],HIGH);
  }
}



// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {  
      pinState= 0;
      digitalWrite(rowArr[i],HIGH);
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}


//blinks the whole register based on the number of times you want to 
//blink "n" and the pause between them "d"
//starts with a moment of darkness to make sure the first blink
//has its full visual effect.

Bruce, why do you keep starting new threads?

Sorry Im just desperate for help.

BruceBerrios:
Sorry Im just desperate for help.

I get that. But why do you keep starting new threads? What do you think will happen if you start a new thread compared to continuing the old thread?

This sketch is not suitable for your circuit. That is why it does not work. It may be damaging the Arduino pins by forcing them to sink to much current. You should stop running it. It was written for a different circuit, perhaps where the shift register sourced the current for the columns and the Arduino pins sank the current for the rows, perhaps with npn transistors or a uln2803 chip.

Also, the whole shiftOut() function is not needed. Arduino already has a shiftOut() function. Check the Reference page.

How would I sink the current correctly? And why is the Shift Register not outputting the Byte? How I thought this would work is I would send a byte to the shift register, it would then turn on the corresponding LEDS, and I would just sink the current by setting the Cathode pin to LOW and repeat this process 8 times.

This all that happens when I run any code.

BruceBerrios:
8x8 matrix debug - YouTube

We can't view it. Maybe you need to make it public.

You are using the 595 to drive 8 PNP transistors to source current to the rows, one row at a time, and 8 Arduino pins to sink the current from the columns. That is still correct, yes? So up to 8 leds on the same row will be lit at the same instant. Each row is scanned in turn.

Your sketch is for a circuit that does the opposite: scans by column. It sends a pattern to the 595, lighting up to 8 leds in a column and makes one Arduino pin low at a time to sink that current. This is then repeated for each column. The Arduino pin that is low might have to sink the current from 8 leds, which is way more than the maximum it can handle.

You need make some significant changes to the sketch. You should send patterns to the 595 to make only one PNP switch on at once. Remember that a low output from the 595 will switch the pnp on and high will switch it off. And you need to set up the pattern to be displayed on the row on the 8 Arduino pins. Low will switch the led in that column on.

I managed to control the Cathodes. I can't seem to control one PNP transistor at a time. What binary value would I have to write for one PNP at a time?

dataArray[0] = 0x01;
  dataArray[1] = 0x02;
  dataArray[2] = 0x04;
  dataArray[3] = 0x08;
  dataArray[4] = 0x10;
  dataArray[5] = 0x20;
  dataArray[6] = 0x40;
  dataArray[7] = 0x80;

I'm current using this

I have made it public. My array still seems to scan vertically rather than horizontally meaning that the LED is still sinking an insane amount of current.

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 6;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin =7;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 12;

int rowArr[8] = {5,4,3,2,8,9,10,11}; // Pins where each Cathode is connected

// Desired shape
int cathodeArr[8][8] = {{0,1,1,1,1,1,1,1},
{1,0,1,1,1,1,1,1},
{1,1,0,1,1,1,1,1},
{1,1,1,0,1,1,1,1},
{1,1,1,1,0,1,1,1},
{1,1,1,1,1,0,1,1},
{1,1,1,1,1,1,0,1},
{1,1,1,1,1,1,1,0}};

byte data;
byte dataArray[8];

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  Serial.begin(9600);
  pinMode(5,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);

  //PNP Control
  dataArray[0] = B00000001;
  dataArray[1] = B00000010;
  dataArray[2] = B00000100;
  dataArray[3] = B00001000;
  dataArray[4] = B00010000;
  dataArray[5] = B00100000;
  dataArray[6] = B01000000;
  dataArray[7] = B10000000;


// turn off all Pins
  digitalWrite(5,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(2,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
  digitalWrite(11,HIGH);
 
}

void loop() {
  for (int i = 0; i < 8; i++) {

    data = dataArray[i];

    digitalWrite(latchPin, LOW);

    shiftOut(dataPin, clockPin, LSBFIRST,data);
    
    digitalWrite(latchPin, HIGH);
    for(int j = 0; j < 8; j++){
      if(cathodeArr[i][j] == 1){
        digitalWrite(rowArr[j],LOW); // If the value is 1 sink the current
      }else if(cathodeArr[i][j] == 0){
        digitalWrite(rowArr[j],HIGH); // Dont sink
      }

    }

    delay(100);
    digitalWrite(latchPin, LOW);
    
    for(int j = 0; j < 8; j++){
       digitalWrite(rowArr[j],HIGH); // turn themm all off for the next iteration
    }
  }
}

Here is my full code.

I managed to fix it. It works great :smiley: ! However, some LEDs are dimly lit even though they aren't supposed to. Hardware or software issue?

This is what I mean:

Two of my LEDS died

I’m trying to render the letter A but some LEDS that aren’t supposed to be lit are lit.

Here is my full Code:

int DS_pin = 6;
int STCP_pin = 7;
int SHCP_pin = 12;

int rowArr[8] = {5,4,3,2,8,9,10,11}; //Pins that will sink current

int cathodeArr[8][8] = { // Pattern to draw
{0,0,0,1,1,0,0,0},
{0,0,1,0,0,1,0,0},
{0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0}};

void setup(){
  
  pinMode(DS_pin,OUTPUT);
  pinMode(STCP_pin,OUTPUT);
  pinMode(SHCP_pin,OUTPUT);

  void setup(){
  
  pinMode(DS_pin,OUTPUT);
  pinMode(STCP_pin,OUTPUT);
  pinMode(SHCP_pin,OUTPUT);
  
  for(int i = 0; i < 8; i++){
    pinMode(rowArr[i],OUTPUT);
  }

// Turn off all LEDS
  for(int i = 0; i < 8; i++){
    digitalWrite(rowArr[i],HIGH);
  }
}
}

void writeReg(int x){
  
  digitalWrite(STCP_pin, LOW);
  delay(1);
  for(int i = 7; i >= 0; i--){
    digitalWrite(SHCP_pin, LOW);
    if(i == x){
      digitalWrite(DS_pin, LOW); // Turn on the Transistor
    }else{
      digitalWrite(DS_pin, HIGH);
    }
    digitalWrite(SHCP_pin, HIGH);
  }
  digitalWrite(STCP_pin, HIGH);
}

void loop(){
  
  for(int i = 0; i < 8; i++){ 
    delay(1); 
    writeReg(i);
    for(int j = 0; j < 8; j++){
      if(cathodeArr[i][j] == 1){
        digitalWrite(rowArr[j],LOW); // Sink current
      }else if(cathodeArr[i][j] == 0){
        digitalWrite(rowArr[j],HIGH);
      }
    }
  } 

}

Anyone know why they died? The dead LEDS seem to have a bubble in them. Is this the cause?

I fixed the Duds. Any recommendations to avoid this problem in the future?

Does anyone even use this Forum?

BruceBerrios:
Does anyone even use this Forum?

Apparently some guy named Bruce does. But everyone ignores him because he is notoriously impatient.

Yeah man sorry about that. I got everything working. Thank you for helping me a lot. I thin I've given you Karma all these days. Cheers and my apologies!

BruceBerrios:
The dead LEDS seem to have a bubble in them. Is this the cause?

Did the leds have the bubbles in them when you got them? If so I would guess yes, the bubbles indicate some manufacturing fault. None of my leds have bubbles, that I ever noticed. Where did you buy them?

If there were no bubbles when you got them, I would guess they have been caused by overheating due to excessive current. But I would not have expected to see bubbles due to overheating, just a dead led.

I bought them from Ebay. I tested all the LEDs and they were all working. I've also looked at the rest of the LEDS that I bought. No bubbles. I just hope this was a one-time thing. I was looking through forums and I read a post that recommended a 220ohm resistor in the Latch pin. That's what I added and I haven't had any problems since then. I also added a decoupling capacitor to the 595.

BruceBerrios:
I was looking through forums and I read a post that recommended a 220ohm resistor in the Latch pin.

I can't see how that would affect the leds in any way, so I think it must be coincidence. And that resistor is unnecessary. Sometimes resistors are recommended to beginners to help prevent damage in case they make a wrong connection and accidentally cause a short circuit. For example connecting an output to another output or an output to 5V or ground. Can you post a link to that forum thread?

BruceBerrios:
I bought them from Ebay. I tested all the LEDs and they were all working.

Sometimes leds sold on eBay are from batches that failed quality testing in the factory. I have bought leds like this before. I made a 4x4x4 led cube with them and many failed, but not until after several days of running.

Here you go.