Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Based I2C LCD Calculator with 4x4 Keypad Interface

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based I2C LCD Calculator with 4x4 Keypad Interface

Circuit Documentation

Summary of the Circuit

This circuit consists of an Arduino UNO microcontroller, a 4x4 keypad, and a 16x2 I2C LCD display. The Arduino UNO serves as the central processing unit, interfacing with the keypad for user input and controlling the LCD display to provide visual feedback. The keypad allows for user interaction, enabling the input of numbers and commands. The LCD display shows information to the user, such as the numbers entered on the keypad and the results of any calculations or commands processed by the Arduino.

Component List

Arduino UNO

4x4 Keypad

16x2 I2C LCD

Wiring Details

Arduino UNO

4x4 Keypad

16x2 I2C LCD

Documented Code

#include <Wire.h>               // Include the I2C library
#include <LiquidCrystal_I2C.h>   // Include the I2C LCD library

/* Display */
LiquidCrystal_I2C lcd(0x27, 16, 2);  // I2C address of the LCD (0x27 is common), and 16 columns and 2 rows

/* 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.clear();
  lcd.print("GoodArduinoCode");
  lcd.setCursor(0, 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);           // Initialize the LCD with 16 columns and 2 rows
  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);
  }
}

This code is designed to run on the Arduino UNO and interfaces with both the 4x4 keypad and the 16x2 I2C LCD. It includes a simple calculator application that allows the user to input numbers and basic arithmetic operations. The LCD displays the input and the result of the calculations. The code is structured with initialization in the setup() function, continuous input processing in the loop() function, and helper functions to handle specific tasks such as displaying the splash screen, updating the cursor, and performing calculations.