How to build an Arduino Calculator with Keypad and I2C LCD

This project involves creating a simple calculator using an Arduino, a 4×4 keypad, and an I2C LCD to display results. Below are the complete details for the project, including wiring, components, and code.

Hardware Required:

  1. Arduino Board (e.g., Arduino Uno)
  2. 4×4 Keypad (matrix keypad)
  3. I2C LCD (16×2 LCD with I2C interface)
  4. Jumper wires
  5. Breadboard (optional, depending on the connections)

Step 1: Wiring the Components

1. Wiring the 4×4 Keypad:

The 4×4 Keypad has 8 pins: 4 rows and 4 columns.

  • Row Pins: R1, R2, R3, R4
  • Column Pins: C1, C2, C3, C4

Wiring Connections for the Keypad:

  • Row 1 (R1) → Pin 9 on Arduino
  • Row 2 (R2) → Pin 8 on Arduino
  • Row 3 (R3) → Pin 7 on Arduino
  • Row 4 (R4) → Pin 6 on Arduino
  • Column 1 (C1) → Pin 5 on Arduino
  • Column 2 (C2) → Pin 4 on Arduino
  • Column 3 (C3) → Pin 3 on Arduino
  • Column 4 (C4) → Pin 2 on Arduino

2. Wiring the I2C LCD:

The I2C LCD has 4 pins:

  • VCC → 5V on Arduino
  • GND → GND on Arduino
  • SDA → A4 on Arduino (Data line)
  • SCL → A5 on Arduino (Clock line)

Step 2: Installing Libraries

Before uploading the code to the Arduino, you need to install two libraries:

  1. Keypad Library: This library is used to manage the keypad input.
  2. LiquidCrystal_I2C Library: This library is used to control the I2C LCD display.

Installing Libraries:

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. Search for Keypad and click Install.
  4. Search for LiquidCrystal_I2C and click Install.

Step 3: Arduino Code

Here is the complete code to run the Arduino calculator:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

// Set up the I2C LCD (address 0x27, 16 columns and 2 rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Set the LCD address to 0x27 and 16x2 size

// Define the keypad's rows and columns
const byte ROW_NUM    = 4; // Four rows
const byte COL_NUM    = 4; // Four columns

char keys[ROW_NUM][COL_NUM] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', 'C'},
  {'*', '0', '=', '/'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // Connect keypad rows to pins 9, 8, 7, 6
byte pin_cols[COL_NUM] = {5, 4, 3, 2}; // Connect keypad columns to pins 5, 4, 3, 2

Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_cols, ROW_NUM, COL_NUM);

// Variables for storing numbers and the result
float num1 = 0;
float num2 = 0;
char op = '\0';
float result = 0;

void setup() {
  // Initialize LCD and set the cursor
  lcd.init();  // Initialize the LCD
  lcd.backlight();  // Turn on the backlight
  lcd.setCursor(0, 0);
  lcd.print("Calculator");
  delay(2000);  // Display "Calculator" for 2 seconds
  lcd.clear();  // Clear the LCD screen
}

void loop() {
  char key = keypad.getKey();
  
  if (key) {
    lcd.clear();
    lcd.setCursor(0, 0);
    
    if (key == 'C') {
      num1 = num2 = result = 0;
      op = '\0';
      lcd.print("Cleared");
    } 
    else if (key == '=') {
      // Perform the calculation based on the operator
      switch (op) {
        case '+': result = num1 + num2; break;
        case '-': result = num1 - num2; break;
        case '*': result = num1 * num2; break;
        case '/': 
          if (num2 != 0) result = num1 / num2;
          else lcd.print("Error");
          break;
        default: 
          lcd.print("Invalid Op");
          return;
      }
      
      // Display result: Check if it's a whole number or not
      lcd.setCursor(0, 1);
      lcd.print("Result: ");
      
      if (result == (int)result) {
        // If result is an integer, display it as an integer
        lcd.print((int)result);  
      } else {
        // Otherwise, display it as a float
        lcd.print(result);
      }
      
      delay(2000);  // Display result for 2 seconds
      lcd.clear();  // Clear the LCD screen
      num1 = result;
      num2 = 0;
      op = '\0';
    }
    else if (key == '+' || key == '-' || key == '*' || key == '/') {
      op = key;
      lcd.print(op);
    } 
    else {
      // Check if key is a number
      if (op == '\0') {
        num1 = num1 * 10 + (key - '0');
        lcd.print(num1);
      } 
      else {
        num2 = num2 * 10 + (key - '0');
        lcd.print(num2);
      }
    }
  }
}

Step 4: Uploading the Code

  1. Connect your Arduino to your computer using a USB cable.
  2. In the Arduino IDE, select the correct Board and Port from Tools > Board and Tools > Port.
  3. Click the Upload button in the Arduino IDE.

Step 5: Testing the Calculator

  1. Power Up the Arduino: Once the code is uploaded, the LCD should display “Calculator” for 2 seconds, and then the screen should be ready for input.
  2. Input Numbers and Operations:
    • Use the 4×4 keypad to enter numbers and perform calculations.
    • Use the following keys:
      • 0-9: Input numbers
      • +: Addition
      • : Subtraction
      • *: Multiplication
      • /: Division
      • =: Perform calculation and display result
      • C: Clear the input

Step 6: Troubleshooting

  1. LCD Does Not Display Anything:
    • Double-check the I2C connections.
    • Ensure the LCD has the correct I2C address (0x27 is common, but some LCDs may use 0x3F).
    • If unsure, you can run an I2C scanner sketch to find the address of your LCD.
  2. Keypad Not Responding:
    • Double-check the row and column pin connections.
    • Ensure the Keypad library is installed correctly.

Conclusion

You’ve successfully built an Arduino-based calculator that uses a 4×4 keypad for input and an I2C LCD for displaying the results. This project helps you get hands-on experience with reading inputs from a keypad and outputting information to an LCD. You can further expand the functionality by adding more complex operations or improving the user interface.