element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Blog New to arduino and programming, 4x3 analog keypad question about making a passcode
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: e14 Contributor
  • Date Created: 10 Jan 2013 11:57 AM Date Created
  • Views 3624 views
  • Likes 0 likes
  • Comments 21 comments
Related
Recommended

New to arduino and programming, 4x3 analog keypad question about making a passcode

e14 Contributor
e14 Contributor
10 Jan 2013

Hi to you all.I am a 47 year old man who due to an accident am disabled and bought myself an Arduino to keep myself busy. I am  new to arduino and have written some basic sketches. and have made some nice projects. I have an UNO R3 board and I have made an analog keypad using 12 tactile buttons and 7 resistors with a 1nf capacitor to help with bouncing. i did this so that all my digital pins are free to use. My idea is to make an alarm system using 2 PIR sensors, 4 LED's, a buzzer and a siren.(I may want to add more components later) I have no problem in writing the code except for the 4x3 keypad. I want to make a passcode eg 1234 but this is the part I am struggling with. The push buttons return a value when they are pressed so how would I go about that part I have written code to serial print the code to my pc and give them a number. please see the code examples below but I have looked all over the internet and asked on the Arduino forum for some help with the keypad. If anyone can help I would appreciate it.

Thank you for taking the time to read this.

void readkeyboard(){
   keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
   if (keyboardValue <25){keypressed = 0;}
   if ((keyboardValue >25) && (keyboardValue < 67)){keypressed = 1;}
   if ((keyboardValue >67) && (keyboardValue < 108)){keypressed = 2;}
   if ((keyboardValue >108) && (keyboardValue < 162)){keypressed = 3;}
   if ((keyboardValue >162) && (keyboardValue < 253)){keypressed = 4;}
   if ((keyboardValue >253) && (keyboardValue < 361)){keypressed = 5;}
   if ((keyboardValue >361) && (keyboardValue < 479)){keypressed = 6;}
   if ((keyboardValue >479) && (keyboardValue < 619)){keypressed = 7;}
   if ((keyboardValue >619) && (keyboardValue < 765)){keypressed = 8;}
   if ((keyboardValue >765) && (keyboardValue < 819)){keypressed = 9;}
   if ((keyboardValue >819) && (keyboardValue < 899)){keypressed = 0;}// this is the * key
   if ((keyboardValue >889) && (keyboardValue < 955)){keypressed = 0;}
   if (keyboardValue >965){keypressed = 0;}  // this is the # key

}

when the keys are pressed they return these values

 

1=58, 2=96, 3=145,   4=228,  5=335,  6=447, 7=598,  8=720,  9=809,  *=890,  0=941, #=970

 

I have Written to the buttons like this just to test the keypad works as it should

   if (keyboardValue >25 && keyboardValue < 67) digitalWrite (12,HIGH);
   if (keyboardValue >67 && keyboardValue < 130) digitalWrite (11,HIGH);
   if (keyboardValue >162 && keyboardValue < 253) digitalWrite (9,HIGH);
   if (keyboardValue >108 && keyboardValue < 162) digitalWrite (10,HIGH);
   if (keyboardValue >253 && keyboardValue < 361) digitalWrite (8,HIGH);

  • Sign in to reply

Top Comments

  • billabott
    billabott over 13 years ago in reply to e14 Contributor +1
    Get some sleep and check out http://www.oomlout.com/a/products/ardx/ tomorrow.
  • e14 Contributor
    e14 Contributor over 13 years ago

    Hi again I think I am getting there with this. I have been working with a 7 pin password sketch and trying to adapt it for my analog keypad. I have beenscratching my head and searching for a solution to this problem. When I run this code the alarm keeps on arming every few seconds. I think it is that the analog keypad keeps throwing out zero readings and resetting the system. The code is a bit of a mess at the moment but please throw me some comments as to how to solve my issue.

    Thank you

     

    int NOKEY=255;
    int i, k, pwcount=0;
    char password[]="1234",entry[10];
    int buttonPin=A0;
    int val=A0;
    int ldr=12;
    int senseValue=0;
    int led=13;
    int sensePin=A1;
    int buzzerPin=2;
    int led2=5;
    int led3=3;
    int led4=4;

     

     

     

    void setup() {
      // put your setup code here, to run once:
    pinMode (buttonPin,INPUT);
    pinMode (sensePin,INPUT);
    pinMode(ldr,OUTPUT);
    pinMode (led,OUTPUT);
    pinMode (led2,OUTPUT);
    pinMode (led3,OUTPUT);
    pinMode (led4,OUTPUT);
    pinMode (buzzerPin,OUTPUT);
    Serial.begin(9600);

     

    }

     

    void loop() {
      // put your main code here, to run repeatedly:
       for(i=6; i<=8; i++){
      reset();
     
     
      val=analogRead(buttonPin);
      val = map(val, 0, 1023, 0, 255);
    // Serial.print("Button Value   ");//used for gebugging
    //Serial.print(val);
     
    senseValue=analogRead(sensePin);
    senseValue = map(senseValue, 0, 1023, 0, 255);
    //Serial.print("     Sensor Value   ");
    // Serial.println(senseValue);
        if (val <10,val=255);//I cannot stop the program looping
     
      if ((val >10)&&(val<18)){
       entry[pwcount]='1' ;
       pwcount++;
       delay(250);
    Serial.println('1');
      }
    if ((val >19)&&(val<28)){
       entry[pwcount]='2' ;
       pwcount++;
       delay(250);
    Serial.println('2');
      }
     
       if ((val >30)&&(val<40)){
       entry[pwcount]='3' ;
       pwcount++;
       delay(250);
    Serial.println('3');
      }
    if ((val >50)&&(val<60)){
       entry[pwcount]='4' ;
       pwcount++;
       delay(250);
    Serial.println('4');
      } 
     
       if ((val >75)&&(val<90)){
       entry[pwcount]='5' ;
       pwcount++;
       delay(250);
    Serial.println('5');
      }
    if ((val >100)&&(val<120)){
       entry[pwcount]='6' ;
       pwcount++;
       delay(250);
    Serial.println('6');
      }
     
       if ((val >135)&&(val<155)){
       entry[pwcount]='7' ;
       pwcount++;
       delay(250);
    Serial.println('7');
      }
    if ((val >170)&&(val<180)){
       entry[pwcount]='8' ;
       pwcount++;
       delay(250);
    Serial.println('8');
      } 
        if ((val >190)&&(val<205)){
       entry[pwcount]='9' ;
       pwcount++;
       delay(250);
    Serial.println('9');
      }
    if ((val >218)&&(val<227)){
       entry[pwcount]='*' ;
       pwcount++;
       delay(250);
    Serial.println('*');
      }
     
       if ((val >227)&&(val<236)){
       entry[pwcount]='0' ;
       pwcount++;
       delay(250);
    Serial.println('0');
      }
    if ((val >237)&&(val<250)){
       entry[pwcount]='#' ;
       pwcount++;
       delay(250);
    Serial.println('#');
      } 
     
     
     
       pwcount=0;
      for(k=0; k<=5; k++){
       if(k==5){
       Serial.println("Alarm Disabled");
       digitalWrite(3, HIGH);
       digitalWrite (4, LOW);
       digitalWrite (13,LOW);
       digitalWrite(ldr,LOW);
      
      
       break;
       }
       if(password[k] != entry[k]){
         digitalWrite (4, HIGH);
         Serial.println("Alarm ON 2 Seconds Until Armed");
         digitalWrite(3,LOW);
         armed();
         //delay(2000);
         //digitalWrite (13,HIGH);
         Serial.println("Alarm Enabled");
         printing();
         Serial.println("Callibrating System Please wait..");
         printing();
         Serial.println("System Activating...");
         printing();
         Serial.println("All Systems Functioning Correctly");
          printing();
           delay(500);
         Serial.println("Alarm On");
         break;
       }
      }
      // k<10
      for(k=0; k<10; k++)
      entry[k]=0;
      } 
    }

     

    void reset(){
       int j;
       for(j=6; j<=8; j++)
       digitalWrite(j, LOW);
    }

     

    void blinker(){
      digitalWrite(3, HIGH);
      delay(15);
      digitalWrite(3, LOW);
     
     
    }
    void printing(){
    delay(200);

     

     

     

    }
    void armed (){
      delay(100);
      digitalWrite(13,HIGH);
     
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • e14 Contributor
    e14 Contributor over 13 years ago

    Hi and thank you for your reply David. I am sure this will help me out. I have been in hospital so I appologise for not getting back to you sooner. I am home now and will have a look and see if I can figure it out. I have a 7 wire keypad that I made and have had this working fine in a basic sketch retuning the button press to the serial monitor . But I want to use an analog keypad which just uses 1 analog pin to free up some digital pins and am falling down here. i hope this helps me.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • e14 Contributor
    e14 Contributor over 13 years ago

    Jeremy,

    I am assuming that the values less than 25 are when no key is pressed. You set your variable "keypressed" to zero all the time, even when no key is pressed. . You need a different value for NOKEY, pound or star keys.  I would set 3 constants for NOKEY, STARKEY, and POUNDKEY. You already have the 0 through 9 values for those keys. Set the STARKEY to 10, the POUNDKEY to 11 and maybe the NOKEY to 99. Then your code loop could react only when keypressed != NOKEY. I don't have my Arduino editor on this pc and I can't recall the exact syntax. But change your lines somthing like this. Note I changed your routine from void to int and returned the value of the key.

    int readkeyboard(){
       keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
       if (keyboardValue <25){return NOKEY;} //No key pressed
       if ((keyboardValue >25) && (keyboardValue < 67)){return 1;}
       if ((keyboardValue >67) && (keyboardValue < 108)){return 2;}
       if ((keyboardValue >108) && (keyboardValue < 162)){return 3;}
       if ((keyboardValue >162) && (keyboardValue < 253)){return 4;}
       if ((keyboardValue >253) && (keyboardValue < 361)){return 5;}
       if ((keyboardValue >361) && (keyboardValue < 479)){return 6;}
       if ((keyboardValue >479) && (keyboardValue < 619)){return 7;}
       if ((keyboardValue >619) && (keyboardValue < 765)){return 8;}
       if ((keyboardValue >765) && (keyboardValue < 819)){return 9;}
       if ((keyboardValue >819) && (keyboardValue < 899)){return STARKEY;}// this is the * key
       if ((keyboardValue >889) && (keyboardValue < 955)){return 0;}
       if (keyboardValue >965){return POUNDKEY;}  // this is the # key

    }

    Your code inside loop might be something like:

    CurrentKey = readkeyboard();

    If (Currentkey != NOKEY) {

       // Do something with the value here

    }

     

    I hope this helps some.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • e14 Contributor
    e14 Contributor over 13 years ago

    Hi I got a book from the library today so I can start from the bottom. The only way is up from here. Thanks billabott and if I seemed rude in any way I appologise. It is just been so frustrating trying to sort out my keypad. I have been trying so long and getting nowhere and it is getting to me.

    I have had a good look at Oomlout there is quite a bit on there too. Thanks Guys.. Now where do I start? Well I suppose chapter one :-)

    By the way I don't think I will be slinging my Arduino in the drawer now. The analog keypad, well that's another matter I think I will put that in the drawer before I end up stamping on it lol.

    Thanks

    Jeremy

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • e14 Contributor
    e14 Contributor over 13 years ago

    Hi Nico teWinkel. Thank you for pointing this out to me. I will take a look then. I have been into electronics for a few years now. The trouble is I have never tried programming before October when I got my Arduino and have messed around with led's and Battery chargers and little things like that.

    What I am really trying to do is build my own Alarm system I need the digital pins for my 2 PIR sensors, bleeper, siren and some LED's All I am stuck with is this bloody keypad. It is driving me crazy. I did not get to the library today but I will and maybe this site could be the help I am looking for. I have been looking for well over a month and am getting so frustrated that I want to throw the lot in a drawer and forget about it. But I will keep trying for a while.

    Thanks again

    Jeremy

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
<>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube