Build Your Own Arduino Calculator With 4x4 Keypad and LCD Display

by AustinS89 in Circuits > Arduino

274 Views, 1 Favorites, 0 Comments

Build Your Own Arduino Calculator With 4x4 Keypad and LCD Display

circuit_image (31).png

In this project, we'll create a simple calculator using an Arduino UNO, a 4x4 membrane keypad, and a 16x2 LCD display. The calculator can perform basic arithmetic operations like addition, subtraction, multiplication, and division. This project is an excellent way to learn about interfacing keypads and LCDs with an Arduino, capturing user input, and displaying results.

This project was created by biswaspremjit23 using Cirkit Designer. Full credit goes to biswaspremjit23 for designing and sharing this innovative project with the community.

Project Link: Arduino Calculator Project

Documentation: Project Documentation

Supplies

  1. 1 x Arduino UNO
  2. 1 x 4x4 Membrane Matrix Keypad
  3. 1 x 16x2 LCD Display
  4. 1 x Resistor (220 Ω)
  5. Jumper wires
  6. Breadboard or perfboard

Circuit Design

The complete circuit diagram and code are available in the Cirkit Designer IDE. You can view, edit, and interact with the project directly through the provided link.

View the Circuit Diagram and Code: Open the project link in Cirkit Designer to access the detailed schematic and code.

Assemble the Circuit

FCLKDRNM1RVOHJX.png

Follow these detailed instructions to assemble the circuit. Refer to the circuit diagram in the Cirkit Designer IDE for visual guidance.

1. Setting Up the Arduino UNO

Place your Arduino UNO on the workspace or attach it to a breadboard if preferred.

2. Connecting the 4x4 Membrane Keypad

The keypad has 8 pins labeled R1, R2, R3, R4 (rows) and C1, C2, C3, C4 (columns).

Row Connections:

  1. Connect R1 to digital pin D5 on the Arduino.
  2. Connect R2 to digital pin D4 on the Arduino.
  3. Connect R3 to digital pin D3 on the Arduino.
  4. Connect R4 to digital pin D2 on the Arduino.

Column Connections:

  1. Connect C1 to analog pin A3 on the Arduino.
  2. Connect C2 to analog pin A2 on the Arduino.
  3. Connect C3 to analog pin A1 on the Arduino.
  4. Connect C4 to analog pin A0 on the Arduino.

3. Connecting the 16x2 LCD Display

The LCD has multiple pins, but we'll use it in 4-bit mode to save on I/O pins.

Power Connections:

  1. Connect VSS (LCD pin 1) to GND on the Arduino.
  2. Connect VDD (LCD pin 2) to 5V on the Arduino through a 220 Ω resistor.
  3. Connect V0 (LCD pin 3) to the middle pin of a 10 kΩ potentiometer (if you wish to adjust contrast), or connect it to GND for default contrast.
  4. Connect RW (LCD pin 5) to GND on the Arduino.

Control Pins:

  1. Connect RS (LCD pin 4) to digital pin D12 on the Arduino.
  2. Connect E (LCD pin 6) to digital pin D11 on the Arduino.

Data Pins (4-bit mode):

  1. Connect D4 (LCD pin 11) to digital pin D10 on the Arduino.
  2. Connect D5 (LCD pin 12) to digital pin D9 on the Arduino.
  3. Connect D6 (LCD pin 13) to digital pin D8 on the Arduino.
  4. Connect D7 (LCD pin 14) to digital pin D7 on the Arduino.

Backlight Connections:

  1. Connect A (Anode, LCD pin 15) to 5V on the Arduino through the 220 Ω resistor (shared with VDD).
  2. Connect K (Cathode, LCD pin 16) to GND on the Arduino.

4. Connecting the Resistor

  1. The 220 Ω resistor is connected between the Arduino's 5V pin and the VDD pin of the LCD. This resistor limits the current to the LCD and its backlight.

5. Ensuring Common Ground

Make sure all GND connections are connected to the same ground rail or point on the Arduino to ensure proper circuit operation.

Upload the Code Using Cirkit Designer

The code for this project is included with the Cirkit Designer project link. You can upload it directly to your Arduino UNO without the need for any additional software.

Steps to Upload the Code:

  1. Connect Your Arduino:
  2. Use a USB cable to connect your Arduino UNO to your computer.
  3. Open the Code Tab:
  4. In the Cirkit Designer IDE, navigate to the Code tab.
  5. Select the USB Port:
  6. Click on the Port button within the IDE.
  7. Select the USB port that corresponds to your connected Arduino UNO.
  8. Upload the Code:
  9. Click the Upload button.
  10. The Cirkit Designer IDE will compile and upload the code directly to your Arduino UNO.
  11. Wait for the upload to complete. A confirmation message will appear once it's done.

Note: Cirkit Designer automatically manages library installations and configurations, simplifying the upload process.

Test the Calculator

Now that your circuit is assembled and the code is uploaded, it's time to test your Arduino calculator.

1. Power Up the Arduino

  1. Ensure your Arduino UNO is connected to your computer or a suitable power source.

2. Initial Display

  1. The LCD should display a splash screen saying "GoodArduinoCode" followed by "Calculator".
  2. After a few moments, the display will clear, and the cursor will appear.

3. Using the Calculator

  1. Input Numbers:
  2. Use the keypad to input numbers. The numbers will appear on the LCD display.
  3. Perform Operations:
  4. Press one of the operation keys: +, -, *, or /.
  5. Enter the second number.
  6. Calculate Result:
  7. Press the = key.
  8. The result will be displayed on the LCD.
  9. Example Calculation:
  10. To calculate 5 + 3:
  11. Press 5
  12. Press +
  13. Press 3
  14. Press =
  15. The LCD will display 8.

4. Clearing and Restarting

  1. To perform a new calculation, simply start entering new numbers, or reset the Arduino to clear all previous data.


Additional Tips

Debugging Connections:

  1. If the LCD displays garbled text or nothing at all, double-check all your connections.
  2. Ensure the contrast pin (V0) is set correctly. If using a potentiometer, adjust it until the text is visible.
  3. Verify that the keypad connections match the pin assignments in the code.

Library Dependencies:

  1. The project uses the LiquidCrystal and Keypad libraries.
  2. Cirkit Designer handles library installations automatically. If uploading code manually, ensure these libraries are installed in your Arduino IDE.

Power Supply:

  1. The Arduino UNO can be powered via the USB connection during testing.
  2. If using an external power supply, ensure it provides a stable 5V.


Code

Below is the Arduino code for the calculator. You can also access and edit this code directly in the Cirkit Designer IDE.

/**
Arduino Calculator

This code implements a simple calculator that can perform basic arithmetic operations.

Created by xboxusr666.
Released under the MIT License.
*/

#include <LiquidCrystal.h>
#include <Keypad.h>

/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2}; // Connect to the row pins of the keypad
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0}; // Connect to the column pins of the keypad
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);

String memory = "";
String current = "";
char operation = 0;

void showSplashScreen() {
lcd.print("GoodArduinoCode");
lcd.setCursor(3, 1);
String message = "Calculator";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(500);
}

void updateCursor() {
if (millis() / 250 % 2 == 0 ) {
lcd.cursor();
} else {
lcd.noCursor();
}
}

void setup() {
lcd.begin(16, 2);
showSplashScreen();
lcd.clear();
lcd.cursor();
lcd.setCursor(1, 0);
}

double calculate(char operation, double left, double right) {
switch (operation) {
case '+': return left + right;
case '-': return left - right;
case '*': return left * right;
case '/': return left / right;
default: return 0;
}
}

void processInput(char key) {
if ('-' == key && current == "") {
current = "-";
lcd.print("-");
return;
}

switch (key) {
case '+':
case '-':
case '*':
case '/':
if (!operation) {
memory = current;
current = "";
}
operation = key;
lcd.setCursor(0, 1);
lcd.print(key);
lcd.setCursor(current.length() + 1, 1);
return;

case '=':
if (memory != "" && current != "" && operation) {
double leftNum = memory.toDouble();
double rightNum = current.toDouble();
memory = String(calculate(operation, leftNum, rightNum));
current = "";
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(memory);
lcd.setCursor(0, 1);
lcd.print(operation);
}
return;
}

if ('.' == key && current.indexOf('.') >= 0) {
return;
}

if ('.' != key && current == "0") {
current = String(key);
} else if (key) {
current += String(key);
}

lcd.print(key);
}

void loop() {
updateCursor();
char key = keypad.getKey();
if (key) {
processInput(key);
}
}


Notes:

  1. Libraries Used:
  2. LiquidCrystal.h
  3. Keypad.h
  4. These libraries are automatically managed within the Cirkit Designer IDE.

How It Works

Keypad Input:

  1. The 4x4 membrane keypad captures user input.
  2. The keypad is interfaced using the Keypad library, which scans the rows and columns to detect key presses.
  3. Each key corresponds to a number or an operation symbol.

LCD Output:

  1. The 16x2 LCD display shows the input numbers, operation, and results.
  2. It is interfaced using the LiquidCrystal library in 4-bit mode to save on I/O pins.
  3. The display provides immediate feedback as keys are pressed.

Calculator Logic:

  1. Variables:
  2. memory: Stores the first number entered.
  3. current: Stores the current number being entered.
  4. operation: Stores the selected arithmetic operation.
  5. Process:
  6. When a number key is pressed, it is appended to the current string.
  7. When an operation key is pressed (+, -, *, /), the current number is moved to memory, and current is reset.
  8. When the equals key (=) is pressed, the calculator performs the operation on memory and current and displays the result.
  9. The calculator supports decimal numbers and negative numbers.

Acknowledgments

All credit for this project goes to biswaspremjit23, who designed and shared this project using Cirkit Designer. We appreciate the opportunity to showcase this innovative design and thank biswaspremjit23 for contributing to the Cirkit Designer community.

Conclusion

You've successfully built a functional calculator using an Arduino, a keypad, and an LCD display. This project serves as an excellent introduction to interfacing input devices and displays with microcontrollers, as well as handling user input and performing calculations in code. Feel free to explore the project further in Cirkit Designer, modify it, and expand upon it to add more features or improve its functionality.