2
\$\begingroup\$

I'm new to electronics and I am working on a 3x3 switch matrix prototype using an Arduino UNO. I created a simple circuit using TinkerCAD but it seems that whenever I press a button, the susbequent rows are activated instead. Example, if I press the button in Row 0, Col 0, then the matrix will activate Row 1, Col 0 and and Row 2, Col 0.

I was watching a tutorial on how switch matrices work, and the way I understand it is that I want to set each row to HIGH one by one, and check each column in that row to determine if it has been pressed or not. I'm not sure where the issue lies, any help would be greatly appreciated!

Video Demonstration

Circuit Diagram

Here is the code:

const byte rowCount = 3;
const byte colCount = 3;

byte rows[rowCount] = {13,12,11};
byte cols[colCount] = {10,9,8};

char keys[3][3] = {
  {'A', 'B', 'C'},
  {'D', 'E', 'F'},
  {'G', 'H', 'I'}
};

void setup() 
{
  Serial.begin(115200);
  
  // setting up outputs
  for(int i = 0; i < rowCount; i++)
  {
    pinMode(rows[i], OUTPUT);
    Serial.print(rows[i]);
    Serial.println(" as OUTPUT");
  }
  
  // setting up inputs
  for(int i = 0; i < colCount; i++)
  {
    pinMode(cols[i], INPUT_PULLUP);
    Serial.print(cols[i]);
    Serial.println(" as INPUT_PULLUP");
  }
}

void loop() 
{
  for(int i = 0; i < rowCount; i++)
  {
    digitalWrite(rows[i], HIGH);
    
    if(digitalRead(cols[0]) == LOW)
    {
      Serial.println(keys[i][0]);
    }
    else if(digitalRead(cols[1]) == LOW)
    {
      Serial.println(keys[i][1]);
    }
    else if(digitalRead(cols[2]) == LOW)
    {
      Serial.println(keys[i][2]);
    }
    
    digitalWrite(rows[i], LOW);
  }
}

UPDATE: Thanks to Frog, I was able to fix the problem. Here is an updated diagram and updated code. I tested this with the Keypad library as well and it seems to be working.

Updated Circuit Diagram

Updated Code Pastebin

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Given that you’re using pull-ups you want to drive the outputs low rather than high to activate them. With no buttons pressed you’d see all high inputs. You’re currently setting an output high but testing for a low input.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You're the real MVP for real. I spent hours on this, thanks a lot! I wish I could upvote you but I don't have enough rep. I'll update my question for future readers! Thanks again \$\endgroup\$ Dec 26, 2022 at 7:28
  • 1
    \$\begingroup\$ @NoobxCamper: (There you are.) Then again, you did accept it (granting 15 reputation where upvoting is 10), as in useful, and not expecting significantly better. \$\endgroup\$
    – greybeard
    Dec 26, 2022 at 8:03
  • 1
    \$\begingroup\$ @NoobxCamper good to see you’ve updated your question with the solution, this is what gives SE it’s value. \$\endgroup\$
    – Frog
    Dec 26, 2022 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.