Introduction: 1x4 Membrane Keypad W/ Arduino
To some configuring this component may come easy, but for me- being new to tinkering with electronics, I found myself confused when trying to tinkering with this specific product. This was partly due to the fact that I couldn't find any information on how to utilize this component via Google, Arduino forums, etc. So I figure that other people might not get it, just as I did..
Product: https://www.adafruit.com/product/1332
Please keep in mind that this is my first instructable.
Step 1: BOM
Arduino (I used an UNO, but other variations would suffice)
1x4 Keypad w/ 5 Pins
5 Jumper Wires
Resistor & LED (if you prefer a visual)
Step 2: Assembly
Insert the 1x4 Keypad Header Pins into your breadboard & place 1 jumper cable directly in front of each pin of the header.
(left to right)
The first wire should be connected to a GRND Pin on your Arduino.
The 2nd, 3rd, 4th, and 5th wire should be connected to 4 Digital Pins on your Arduino. To keep it simple, I chose pins 9-12
- Digital Pin #9, would be the #1 key on your keypad
- Digital Pin #10, would be the #2 key on your keypad
- Digital Pin #11, would be the #3 key on your keypad
- Digital Pin #12, would be the #4 key on your keypad.
Verify your connections.
Step 3: Code
Paste the following into an empty Arduino Sketch & Save it. Continue to Next Step for explanations.
/*
1x4 Keypad from Adafruit.com (Unofficial Source) Original Source: Button Referenced Tutorial on Arduino.com: http://arduino.cc/en/Tutorial/InputPullupSerial Referenced: http://arduino.cc/en/Reference/pinMode
Turns on and off specified Pin or Prints to Serial Monitor when keys are press on a 1x4 keypad from Adafruit.com
The (OLD) circuit: * LED attached from pin 13 to ground * pushbutton attached to pin 2 from +5V * pushbutton attached to pin 3 from +5V * 10K resistor attached to pin 2 from ground
The (NEW) circuit (2014):
* Whichever component you want attached to Pin 13 (e.g- LED) * 1x4 Keypad w/ primary pin attached to Ground and 1-4 keys attached to PINS 9-12 * Serial Monitor used for output (for testing, when not using anything attached to Pin 13)
* Note:
created 2005 by DojoDave modified 30 Aug 2011 by Tom Igoe modified 10 Apr 2012 by mpilchfamily modified 8 Dec 2014 by C Cunningham
*
/ constants won't change. They're used here to // set pin numbers: const int buttonPin[] = {9,10,11,12}; // the number of the pushbutton pins const int ledPin = 13; // the number of the LED pin
// variables will change: int buttonState = 0; // variable for reading the pushbutton status
void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the Serial Monitor @ 9600 Serial.begin(9600); // initialize the keypad pin(s) as an input: for(int x=0; x<2; x++) { pinMode(buttonPin[x], INPUT_PULLUP); } }
void loop(){ // read the state of the keypad value: for(int x=0; x<2; x++) { //signifying the state of which the button is in by reading the appropriate pin # buttonState = digitalRead(buttonPin[x]);
// check if the pushbutton on the keypad is pressed. // if it is, the buttonState is LOW: if (buttonState == LOW && buttonPin[x] == 9) { // turn LED off: Serial.print("OFF * "); // digitalWrite(ledPin, LOW); } if (buttonState == LOW && buttonPin[x] == 10) { // turn LED off: Serial.print("ON *"); //digitalWrite(ledPin, LOW); } } }
Step 4: You're Done!
Verify & Compile, Upload, and Open your Serial Monitor. Press your buttons. I only used buttons 1 & 2 for ON/OFF Functions. Code could be expanded upon to do other cool things with..
4 Comments
3 years ago
hi, bro thanks,,for int es bettter x<4 to get 4 array items
7 years ago
Thanks for clearing this up. I was a little mystified as well.
7 years ago
I need like this when one momentory push button is presed once only one count get incremented and it goes to the respective process
8 years ago on Step 3
Just wondering why you've set an array of 4 items:
const int buttonPin[] = {9,10,11,12}; // the number of the pushbutton pins
yet you only setup and read from 3:
for(int x=0; x<2; x++)
{
pinMode(buttonPin[x], INPUT_PULLUP);
}
If that's intentional then thats fine - just no comment noting why and some people might be confused.