Arduino Code and Video for a Four-Key Keypad

Arduino Code and Video for a Four-Key Keypad

In this tutorial, we will learn how to utilize a four-key keypad with an Arduino. This project is particularly useful for applications where you need multiple buttons but want to minimize the number of wires used. By connecting the keypad in a specific way, we can achieve a functional setup with just five wires instead of eight.

4 key flixable keypad

The four-key keypad simplifies the wiring process, as it features a common connection that reduces the total number of wires needed. Each key is connected to the Arduino through designated pins, allowing for easy interaction and control. This guide will cover the wiring, code, and expected behavior of the keypad, so you can replicate the project successfully (in video at 02:30).

Hardware Explained

The main components of this project include a four-key keypad and an Arduino board. The keypad contains four buttons, and each button connects to the Arduino via two wires—one common wire and one dedicated wire for each button. This setup allows for straightforward connections while minimizing clutter.

The Arduino interprets the button presses using digital inputs, relying on internal pull-up resistors to ensure stable readings. When a button is pressed, it connects the corresponding pin to ground, allowing the Arduino to detect the change in state.

Datasheet Details

ManufacturerGeneric
Part number4-Key Keypad
Logic/IO voltage5 V
Supply voltage5 V
Number of keys4
Common pin typeActive-low
Button resistance1 kΩ (typ.)
PackageFlexible PCB
Notes / variantsAvailable in multiple layouts

  • Each button requires two connections: one for the button and one for the common ground.
  • Using internal pull-up resistors simplifies wiring and reduces component count.
  • Ensure the common pin is connected to ground for correct operation.
  • Debounce button presses in software to avoid false readings.
  • Test button functionality with a simple serial printout for feedback.

Wiring Instructions

Arduino wiring for 4 key keypad
Arduino wiring for 4 key keypad

To wire the four-key keypad to the Arduino, follow these steps:

  • Connect the first key wire to pin 2 on the Arduino.
  • Connect the second key wire to pin 3.
  • Connect the third key wire to pin 4.
  • Connect the fourth key wire to pin 5.
  • Connect the common wire from the keypad to the ground pin on the Arduino.

Make sure to securely connect each wire to avoid any loose connections. If you experience issues, double-check each connection against the pin assignments shown in the video (in video at 04:15).

Code Examples & Walkthrough

The following code snippet initializes the pins for the keypad and sets them as inputs:

#define key1 2 //connect wire 1 to pin 2
#define key2 3 //connect wire 2 to pin 3
#define key3 4 //connect wire 3 to pin 4
#define key4 5 //connect wire 4 to pin 5

void setup() {
 Serial.begin(9600);
 pinMode(key1, INPUT_PULLUP); // set pin as input
 pinMode(key2, INPUT_PULLUP); // set pin as input
 pinMode(key3, INPUT_PULLUP); // set pin as input
 pinMode(key4, INPUT_PULLUP); // set pin as input   
}

In the code above, each key is defined with a corresponding pin number. The INPUT_PULLUP mode ensures that the pins read high when not pressed, simplifying the button press detection.

The loop function reads the state of each key and prints the corresponding message when pressed:

void loop() {
  int key1S = digitalRead(key1); // read if key1 is pressed
  int key2S = digitalRead(key2); // read if key2 is pressed
  int key3S = digitalRead(key3); // read if key3 is pressed
  int key4S = digitalRead(key4); // read if key4 is pressed 

  if(!key1S) {
    Serial.println("key 1 is pressed");
  }
  if(!key2S) {
    Serial.println("key 2 is pressed");
  }
  // Additional code for key3 and key4...
  delay(100);
}

This segment of code checks the state of each key and prints a message to the serial monitor when a key is pressed. The delay(100) allows for a debounce effect, ensuring that multiple presses are not registered in quick succession.

Demonstration / What to Expect

Upon successfully wiring and uploading the code to your Arduino, you should expect the serial monitor to display messages indicating which key is pressed. Pressing key one will output "key 1 is pressed" and so on for the other keys. If the messages are not appearing, check your wiring and ensure the common wire is connected to ground.

Be cautious of reversed connections, as this may lead to unexpected behavior. Ensure that the pins used match those defined in your code (in video at 06:45).

Video Timestamps

  • 00:00 - Introduction
  • 02:30 - Wiring Explanation
  • 04:15 - Pin Assignments
  • 06:45 - Testing the Keypad

الصور

4 key flixable keypad
4 key flixable keypad
Arduino wiring for 4 key keypad
Arduino wiring for 4 key keypad
50-Arduino Code for 4 key keypad
اللغة: C++
تم النسخ!

الأشياء التي قد تحتاجها

الموارد والمراجع

لا توجد موارد حتى الآن.

ملفات📁

مكتبات أردوينو (ملف مضغوط)