Blogroll

Powered by Blogger.

Saturday, 16 August 2014

Demonstration of Interfacing 4*4 Button Pad to Arduino Mega, Circuit and Program

by realfinetime  |  in Arduino Uno at  11:49


          We had already seen the internal circuit and pinout of sealed membrane 4*4 button pad in previous post. Here we will design a circuit to interface this button pad to arduino mega. Interfacing 4*4 button pad to arduino is simple. Circuit is done as shown in the diagram.

Connections can be summarized as given below

K pin of  4*4 button pad is connected to the 4th digital pin of Arduino mega.
L pin of  4*4 button pad is connected to the 5th digital pin of Arduino mega.
M pin of  4*4 button pad is connected to the 6th digital pin of Arduino mega.
N pin of  4*4 button pad is connected to the 7th digital pin of Arduino mega.
O pin of  4*4 button pad is connected to the 8th digital pin of Arduino mega.
P pin of  4*4 button pad is connected to the 9th digital pin of Arduino mega.
Q pin of  4*4 button pad is connected to the 10th digital pin of Arduino mega.
R pin of  4*4 button pad is connected to the 11th digital pin of Arduino mega.

          In addition to these, all the pins of 4*4 button pad are connected to individual pull up resistors of 1K each. Other end of all the pull up resistors are shorted and connected to 5V pin of arduino mega. Pull up resistors helps to pull the voltage level to HIGH always. Read this blog to get a clear idea about the use of pull up resistors.

          Above circuit can be redrawn using press button switches as shown below. R1, R2, R3, R4, R5, R6, R7 and R8 are the pull up resistors.
R1 = R2 = R3 = R4 = R5 = R6 = R7 = R8 = 1K

Working of Circuit

Working of circuit can be easily illustrated using an example. HIGH = 1 = 5V and LOW = 0 = 0V

1. Declare digital pins 4, 5, 6 and 7 of arduino as output pins.
2. Declare digital pins 8, 9, 10 and 11 of arduino as input pins.
3. Send LOW ( 0 ) through digital pins 4, 5, 6 and 7.

4. All the switches will be off by default. That is, digital pins 8, 9, 10 and 11 will be pulled to 5V through the pull up resistors ( R1, R2, R3 and R4 ) and these digital pins will read 1 1 1 1 since it is an input port.

5. When any switch is pressed, corresponding column ( O, P, Q or R ) get grounded. For example, if switch 5 is pressed as shown in the following image, P get grounded through the switch 5 and L, since K, L, M and N are at LOW voltage ( 0V ). Then digital pins O, P, Q and R will read 1 0 1 1 ( O P Q R ). Since P reads 0, microcontroller will assume that the switch pressed will be either one of  2, 5, 8 or 0. Next is to find the pressed switch from switches 2, 5, 8 and 0. Read the following procedures to select the pressed switch from these switches.



          Send LOW through K and HIGH through L, M and N. If P reads 0, then pressed switch will be 2. Because, if the pressed switch is 2, P will read LOW from K through switch 2 since K is LOW. If switch 2 is not pressed, P will read HIGH through pull up resistor R2. In our circuit P will read HIGH, because we have already mentioned that pressed switch is 5. Eliminate switch 2. Remaining switches are 5, 8 and 0.

          Send LOW through L and HIGH through K, M and N. If P reads 0, then pressed switch will  be 5. Because, if the pressed switch is 5, P will read LOW from L through switch 5 since L is LOW. If switch 5 is not pressed, P will read HIGH through pull up resistor R2. Since switch 5 is pressed in this circuit as we have already mentioned, P will read 0 and microcontroller will conclude the pressed switch as switch 5.

          Send LOW through M and HIGH through K, L and N. If P reads 0, then pressed switch will be 8. Because, if the pressed switch is 8, P will read LOW from M through switch 8 since M is LOW. If switch 8 is not pressed, P will read HIGH through pull up resistor R2. In our circuit P will read HIGH, because we have already mentioned that pressed switch is 5. Eliminate switch 8.

          Send LOW through N and HIGH through K, L and M. If P reads 0, then pressed switch will be 0. Because, if the pressed switch is 0, P will read LOW from N through switch 0 since N is LOW. If switch 0 is not pressed, P will read HIGH through pull up resistor R2. In our circuit P will read HIGH, because we have already mentioned that pressed switch is 5. Eliminate switch 8.

From these analysis, microcontroller will conclude that pressed switch is 5.

Now upload the following program to your arduino board

int K = 4;  // K pin of  4*4 button pad is connected to the 4th digital pin of Arduino mega.
int L = 5;  // L pin of  4*4 button pad is connected to the 5th digital pin of Arduino mega.
int M = 6;  // M pin of  4*4 button pad is connected to the 6th digital pin of Arduino mega.
int N = 7;  // N pin of  4*4 button pad is connected to the 7th digital pin of Arduino mega.

int O = 8;  // O pin of  4*4 button pad is connected to the 8th digital pin of Arduino mega.
int P = 9;  // P pin of  4*4 button pad is connected to the 9th digital pin of Arduino mega.
int Q = 10; // Q pin of  4*4 button pad is connected to the 10th digital pin of Arduino mega.
int R = 11; // R pin of  4*4 button pad is connected to the 11th digital pin of Arduino mega.

int O_value;
int P_value;
int Q_value;
int R_value;

int read_value;


// the setup routine runs once when you press reset:
void setup() {          

  pinMode(K, OUTPUT);    // Declare K pin as OUTPUT
  pinMode(L, OUTPUT);    // Declare L pin as OUTPUT
  pinMode(M, OUTPUT);    // Declare M pin as OUTPUT
  pinMode(N, OUTPUT);    // Declare N pin as OUTPUT

  pinMode(O, INPUT);     // Declare O pin as INPUT
  pinMode(P, INPUT);     // Declare P pin as INPUT
  pinMode(Q, INPUT);     // Declare Q pin as INPUT
  pinMode(R, INPUT);     // Declare R pin as INPUT


// INITIALLY SEND LOW TO OUTPUT PINS (K, L, M and N)

 digitalWrite(K, LOW);
 digitalWrite(L, LOW);
 digitalWrite(M, LOW);
 digitalWrite(N, LOW);

 Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {

  label:
  
  // INITIALLY SEND LOW TO OUTPUT PINS (K, L, M and N) IN THE WHILE LOOP

  digitalWrite(K, LOW);
  digitalWrite(L, LOW);
  digitalWrite(M, LOW);
  digitalWrite(N, LOW);

  O_value=digitalRead(O);  // READ VALUE FROM O
  P_value=digitalRead(P);  // READ VALUE FROM P
  Q_value=digitalRead(Q);  // READ VALUE FROM Q
  R_value=digitalRead(R);  // READ VALUE FROM R

  if(O_value==0) //IF ANY SWITCH IN 1, 4, 7 and '*' IS PRESSED O_value BECOME 0
  {

    //  SEND LOW TO K AND HIGH TO L, M AND N
    digitalWrite(K, LOW);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);
 
    // IF VALUE READ FROM O IS 0, SWITCH PRESSED WILL BE 1
    read_value=digitalRead(O);  
    if(read_value==0)
    {
       Serial.print(1);
       delay(500);
       goto label;
    }
    
    //  SEND LOW TO L AND HIGH TO K, M AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, LOW);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM O IS 0, SWITCH PRESSED WILL BE 4
    read_value=digitalRead(O);
    if(read_value==0)
    {
       Serial.print(4);
       delay(500);
       goto label;
    }

    //  SEND LOW TO M AND HIGH TO K, L AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, LOW);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM O IS 0, SWITCH PRESSED WILL BE 7
    read_value=digitalRead(O);
    if(read_value==0)
    {
      Serial.print(7);
      delay(500);
      goto label;
    }

    //  SEND LOW TO N AND HIGH TO K, L AND M
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, LOW);

    // IF VALUE READ FROM O IS 0, SWITCH PRESSED WILL BE '*'
    read_value=digitalRead(O);
    if(read_value==0)
    {
      Serial.print('*');
      delay(500);
      goto label;
    }

  }


  else if(P_value==0)//IF ANY SWITCH IN 2, 5, 8 and 0 IS PRESSED P_value BECOME 0
  {
    
    //  SEND LOW TO K AND HIGH TO L, M AND N
    digitalWrite(K, LOW);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM P IS 0, SWITCH PRESSED WILL BE 2 
    read_value=digitalRead(P);
    if(read_value==0)
    {
       Serial.print(2);
       delay(500);
       goto label;
    }


    //  SEND LOW TO L AND HIGH TO K, M AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, LOW);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);


    // IF VALUE READ FROM P IS 0, SWITCH PRESSED WILL BE 5 
    read_value=digitalRead(P);
    if(read_value==0)
    {
       Serial.print(5);
       delay(500);
       goto label;
    }

    //  SEND LOW TO M AND HIGH TO K, L AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, LOW);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM P IS 0, SWITCH PRESSED WILL BE 8 
    read_value=digitalRead(P);
    if(read_value==0)
    {
      Serial.print(8);
      delay(500);
      goto label;
    }

    //  SEND LOW TO N AND HIGH TO K, L AND M
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, LOW);


    // IF VALUE READ FROM P IS 0, SWITCH PRESSED WILL BE 0 
    read_value=digitalRead(P);
    if(read_value==0)
    {
      Serial.print(0);
      delay(500);
      goto label;
    }

  }


  else if(Q_value==0) //IF ANY SWITCH IN 3, 6, 9 and '#' IS PRESSED Q_value BECOME 0
  {

    //  SEND LOW TO K AND HIGH TO L, M AND N
    digitalWrite(K, LOW);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM Q IS 0, SWITCH PRESSED WILL BE 3  
    read_value=digitalRead(Q);
    if(read_value==0)
    {
       Serial.print(3);
       delay(500);
       goto label;
    }

    //  SEND LOW TO L AND HIGH TO K, M AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, LOW);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM Q IS 0, SWITCH PRESSED WILL BE 6
    read_value=digitalRead(Q);
    if(read_value==0)
    {
       Serial.print(6);
       delay(500);
       goto label;
    }

    //  SEND LOW TO M AND HIGH TO K, L AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, LOW);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM Q IS 0, SWITCH PRESSED WILL BE 9
    read_value=digitalRead(Q);
    if(read_value==0)
    {
      Serial.print(9);
      delay(500);
      goto label;
    }

    //  SEND LOW TO N AND HIGH TO K, L AND M
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, LOW);

    // IF VALUE READ FROM Q IS 0, SWITCH PRESSED WILL BE #
    read_value=digitalRead(Q);
    if(read_value==0)
    {
      Serial.print('#');
      delay(500);
      goto label;
    }

  }



  else if(R_value==0)  //IF ANY SWITCH IN A, B, C and D IS PRESSED R_value BECOME 0
  {
    //  SEND LOW TO K AND HIGH TO L, M AND N
    digitalWrite(K, LOW);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM R IS 0, SWITCH PRESSED WILL BE 'A' 
    read_value=digitalRead(R);
    if(read_value==0)
    {
       Serial.print('A');
       delay(500);
       goto label;
    }

    //  SEND LOW TO L AND HIGH TO K, M AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, LOW);
    digitalWrite(M, HIGH);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM R IS 0, SWITCH PRESSED WILL BE 'B' 
    read_value=digitalRead(R);
    if(read_value==0)
    {
       Serial.print('B');
       delay(500);
       goto label;
    }

    //  SEND LOW TO M AND HIGH TO K, L AND N
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, LOW);
    digitalWrite(N, HIGH);

    // IF VALUE READ FROM R IS 0, SWITCH PRESSED WILL BE 'C' 
    read_value=digitalRead(R);
    if(read_value==0)
    {
      Serial.print('C');
      delay(500);
      goto label;
    }

    //  SEND LOW TO N AND HIGH TO K, L AND M
    digitalWrite(K, HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(M, HIGH);
    digitalWrite(N, LOW);

    // IF VALUE READ FROM R IS 0, SWITCH PRESSED WILL BE 'D'
    read_value=digitalRead(R);
    if(read_value==0)
    {
      Serial.print('D');
      delay(500);
      goto label;
    }
  }
}


If uploading is successfull, open your serial port. Now press the switches (one at a time). Pressed switch name will be displayed in the serial monitor. Don't forget to change the baud rate to 9600. Otherwise serial monitor will give unexpected result.

0 comments:

IMPORTANT NOTICE

All the circuits, published in this blog is only after testing and getting proper results in my private lab. When you try these circuits, you should check the supply voltage, polarity of components, presence of childrens nearby and shorts in the circuits. This website will not be responsible for any harm happened to you or your components caused by your carelessness.

For More Electronic Tips

Blog Archive

Proudly Powered by Blogger.