Calculator Using Matrix Keypad 4x4

by 731306 in Circuits > Arduino

121 Views, 0 Favorites, 0 Comments

Calculator Using Matrix Keypad 4x4

Screenshot (536).png
Screenshot (536).png

Project No.2(Room Automation)

My inspiration for this project was my room automation where I automate my room making things more efficient, and useful aspects for safety.

This project involves creating a calculator using Arduino, a 4x4 keypad, and an OLED display. The calculator is programmed to execute four fundamental operations: addition, subtraction, multiplication, and division. The implementation details, including the code and circuit diagram, are provided. The project requires three essential libraries, the specifics of which are disclosed during the tutorial. The tutorial covers the process of downloading the OLED display Library and obtaining the keypad Library through the Arduino IDE. The overall objective is to guide users in constructing a functional calculator with the specified components and Arduino programming.




Supplies

Arduino_Uno_-_R3.jpg
7126fXBitBL._AC_UF894,1000_QL80_.jpg
apiwdqmuo__78734.jpg
71Ee7XkJ7nL._AC_UF1000,1000_QL80_.jpg

1. Arduino UNO

2. Arduino UNO cable

3. 4X4 Keypad

4. OLED Display

5. Connecting Wires

6. Breadboard small

Connections

Screenshot (534).png
Arduino-Keypad-Tutorial-4X4-Keypad-Schematic-610x494.png
Arduino-Keypad-Tutorial-How-the-Keypad-Works-STEP-1.png

OLED Display to Arduino:


SCL (Serial Clock) on OLED to Analog Pin A5 on Arduino: Connect the SCL pin of the OLED display to analog pin A5 on the Arduino.


SDA (Serial Data) on OLED to Analog Pin A4 on Arduino: Connect the SDA pin of the OLED display to analog pin A4 on the Arduino.


VCC on OLED to Power (5V) on Arduino: Connect the VCC pin of the OLED display to the 5V power output on the Arduino.


GND on OLED to Ground (GND) on Arduino: Connect the ground (GND) pin of the OLED display to the ground pin on the Arduino.

--------------------------------------------------------------------------------------------------------------------------------------------------------------

OLED Display:

Role: Displays the numerical input, mathematical operations, and results of calculations.

Why it's there: Provides a visual interface for users to input numbers and see the results of their calculations in a clear and organized manner.


Arduino Microcontroller:

Role: Acts as the control unit, processing keypad inputs and managing the display.

Why it's there: Coordinates the functions of the keypad and OLED display, interprets user inputs, performs mathematical operations, and sends the corresponding information to the display for user interaction in the calculator.

Keypad:


Role: Accepts user input in the form of numeric digits and mathematical operators.

Why it's there: Allows users to input numbers and operators for performing calculations, serving as the primary input device for the calculator.


Keypad Explanation:

The 4x4 matrix keypad constitutes a straightforward interface reminiscent of the numeric input section on a conventional computer keyboard. Distinguished by the inclusion of additional symbols such as '*', '#', and four auxiliary buttons, this keypad proves versatile for diverse applications within an interface. Constructed typically from cost-effective plastic materials, the 4x4 matrix keypad provides an economical alternative to more sophisticated touchscreen displays.


The integration of a 4x4 matrix keypad can be achieved as a standalone input device or as an integral component within a physical product, exemplified by its utilization in security access controllers for PIN identification purposes. Irrespective of its application, the fundamental operational principles governing the mechanical keypad's hardware and firmware design remain consistent.


For those unacquainted with the intricacies of a 4x4 mechanical keypad, envisioning its internal structure is best accomplished by visualizing a matrix of push-button switches. Comprising a total of eight connections, four of which are designated for the columns and the remaining for the rows of the switch matrix, the keypad establishes a connection between a specific row and column when a button is pressed.For those unacquainted with the intricacies of a 4x4


Upon activation, a connection is formed between a selected row and column. Subsequently, the microcontroller decodes the pressed button based on the intersecting indices of the activated row and column, thereby facilitating seamless integration of the keypad within various electronic systems. Upon activation, a connection is formed between a selected row a

This input mechanism holds particular significance in applications requiring user authentication or secure access control. In scenarios such as security access controllers, the 4x4 matrix keypad's capability to register unique combinations of button presses enhances its utility for personal identification number (PIN) verification.


The structural composition of the keypad, featuring a grid arrangement of buttons, ensures a compact and efficient design. Each button's position within the matrix is uniquely defined by its corresponding row and column, contributing to the systematic interpretation of user inputs. As a user engages with the keypad, pressing a specific button establishes an electrical connection between the associated row and column, thereby conveying a distinct signal to the embedded microcontroller. The structural composition of the keypad, featuring a grid arrangement of


The microcontroller, a pivotal component in the keypad's functionality, translates the received signals into actionable data. By discerning the activated row and column indices, the microcontroller precisely identifies the pressed button. This process of deciphering user inputs is integral to the keypad's role as a reliable and responsive interface.


Whether employed as an external input device or seamlessly integrated into the design of electronic systems, the 4x4 matrix keypad proves to be a versatile and cost-effective solution. Its fundamental design principles, combining simplicity with practicality, make it an invaluable component in diverse applications, ranging from security systems to industrial control interfaces.

Setup the Arduino

What-is-arduino-software-IDE-and-how-use-it.png

Prepare Arduino as Programmer:


Ensure USB Connection: Leave the Arduino board connected to the computer via USB.

Verify Arduino Connections:


Physical Wiring Check: Double-check the physical wiring between the Arduino and connected components to guarantee proper connectivity.


Board and Port Verification: In the Arduino IDE, navigate to "Tools" > "Board" and confirm the accurate selection of the board model. Additionally, verify the appropriate port under "Tools" > "Port" to ensure compatibility and successful communication.


Power Supply Inspection: Confirm the power supply to the Arduino and connected components, ensuring stable and adequate power levels for proper functionality.


Component Integrity Check: Inspect the integrity of connected components, including sensors, actuators, or any additional modules, to prevent potential issues during the project execution.


Firmware Compatibility: Verify that the firmware or sketches uploaded to the Arduino are compatible with the selected board and components, avoiding conflicts that may arise during the operation.


Driver Installation: Ensure that the necessary drivers for the Arduino board and any additional modules are correctly installed on the computer to facilitate seamless communication.


Grounding and Shielding: Pay attention to grounding and shielding practices to minimize electromagnetic interference and ensure signal stability throughout the connected components.


Serial Monitor Setup: If utilizing the Serial Monitor, configure the settings appropriately to monitor and debug communication between the Arduino and external devices.


Advanced Configuration (if applicable): For advanced projects, review and configure specific settings such as interrupts, timers, or communication protocols according to the project requirements.


Maintain a thorough and systematic approach to these preparatory steps to optimize the performance and reliability of your Arduino-based project.

Code and Test Video

Calculator Test Run

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#include <Keypad.h>

These are the required libraries for using SPI, Wire, Adafruit's graphics library, SSD1306 OLED display, and the Keypad library.


Constants and Variables

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

#define OLED_RESET 4


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


Constants defining the OLED display dimensions and reset pin. An instance of the Adafruit_SSD1306 class is created for controlling the OLED display.

const byte ROWS = 4;

const byte COLS = 4;


char keys[ROWS][COLS] = {

 {'1','2','3','A'},

 {'4','5','6','B'},

 {'7','8','9','C'},

 {'*','0','#','D'}

};

Constants defining the number of rows and columns in the keypad matrix. A 4x4 matrix of characters representing the keys on the keypad.

byte rowPins[ROWS] = { 8, 9, 2, 3 };

byte colPins[COLS] = { 4, 5, 6, 7 };


Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

Arrays defining the connections of keypad rows and columns to Arduino pins. An instance of the Keypad class is created using this information.


long Num1, Num2, Number;

char key, action;

boolean result = false;

Variables for storing the first number (Num1), second number (Num2), current number being entered (Number), pressed key (key), the current operation (action), and a boolean flag for indicating if the result should be calculated (result).

Setup Function

void setup() {

 // Initialization code for Serial and OLED display

 // ...

}

Loop Function

void loop() {

 key = kpd.getKey();

  

 if (key != NO_KEY)

  DetectButtons();


 if (result == true)

  CalculateResult();


 DisplayResult();

}

The main loop continuously checks for key presses on the keypad. If a key is pressed, it calls the DetectButtons() function to handle button presses, then checks if the result should be calculated (CalculateResult()) and finally displays the result (DisplayResult()).

DetectButtons Function

void DetectButtons() {

 // Handle different button presses and update Number, action, and result flags

 // ...

}

This function handles different button presses on the keypad. It updates the Number variable based on the pressed keys and sets the action variable when an operation key is pressed. The result flag is also set when the equal key is pressed.


CalculateResult Function

void CalculateResult() {

 // Perform the calculation based on the selected operation

 // ...

}

This function performs the calculation based on the selected operation (addition, subtraction, multiplication, or division) and updates the Number variable with the result.

void DisplayResult() {

 // Display Num1, action, Num2, and the result on the OLED screen

 // ...

}

This function displays Num1, the selected operation, Num2, and the result on the OLED screen using the Adafruit SSD1306 library.


Summary

In summary, this code implements a simple calculator using a 4x4 keypad for input and a 128x64 OLED display for output. The calculator supports basic arithmetic operations, and the input and output are handled through the Keypad and Adafruit SSD1306 libraries, respectively. The code is structured to continuously read key presses, update the calculator state, calculate results, and display them on the OLED screen.