Arduino Calculator

Intro:
Arduino calculator using 4*4 calculator.

Project description:
This project guides you through the process of integrating a 16×2 LCD Display and a 4×4 Keypad with an Arduino Uno. You will learn how to program these components effectively using readily available libraries and how to accomplish specific tasks using Arduino programming.

Components and Supplies:


Description and Pin Connections:

  1. Keypad to Arduino Connections:
    • D0: Connect to 1st pin of the keypad
    • D1: Connect to 2nd pin of the keypad
    • D2: Connect to 3rd pin of the keypad
    • D3: Connect to 4th pin of the keypad
    • D4: Connect to 5th pin of the keypad
    • D5: Connect to 6th pin of the keypad
    • D6: Connect to 7th pin of the keypad
    • D7: Connect to 8th pin of the keypad
  2. LCD to Arduino Connections:
    • D8: Register select pin (LCD pin 4)
    • D9: Enable pin (LCD pin 6)
    • D10-D13: Data pins (LCD pins 11-14)
    • +5V: Connected to Vdd pin (LCD pin 2)
    • Ground: Connected to Vss, Vee, and RW pins (LCD pins 1, 3, and 5)

Key Considerations:

  • Ensure proper connections between Arduino and components to avoid errors.
  • Disconnect the keypad when uploading code to Arduino to prevent issues with pins D0 and D1.
  • Double-check connections to the LCD, as incorrect wiring may lead to display errors.

Additional Tips:

  • Libraries like Keypad.h and LiquidCrystal.h simplify the code for interfacing the keypad and LCD.
  • Test each component individually to ensure proper functionality before assembling the complete circuit.

Arduino Code:1

include
include
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{‘7’, ‘8’, ‘9’, ‘D’}, // D = Divide
{‘4’, ‘5’, ‘6’, ‘C’}, // C = Multiply
{‘1’, ‘2’, ‘3’, ‘B’}, // B = Subtract
{‘*’, ‘0’, ‘#’, ‘A’} // A = Add, * = Clear, # = Equal
};
byte rowPins[ROWS] = {0, 1, 2, 3};
byte colPins[COLS] = {4, 5, 6, 7};
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
long firstNumber = 0, secondNumber = 0, currentNumber = 0;
char key, operation;
bool calculateResult = false;
void setup() {
lcd.begin(16, 2);
lcd.print(“DIY Calculator”);
lcd.setCursor(0, 1);
lcd.print(“-CircuitDigest”);
delay(3000);
lcd.clear();
}
void loop() {
key = kpd.getKey();
if (key != NO_KEY) {
processKeyPress();
}
if (calculateResult) {
performCalculation();
}
displayCalculation();
}
void processKeyPress() {
lcd.clear();
if (key == ‘*’) {
resetCalculator();
} else if (isdigit(key)) {
currentNumber = (currentNumber * 10) + (key – ‘0’);
} else if (key == ‘A’ || key == ‘B’ || key == ‘C’ || key == ‘D’) {
firstNumber = currentNumber;
currentNumber = 0;
operation = key;
} else if (key == ‘#’) {
secondNumber = currentNumber;
calculateResult = true;
}
}
void resetCalculator() {
firstNumber = secondNumber = currentNumber = 0;
calculateResult = false;
lcd.print(“Cleared”);
delay(500);
lcd.clear();
}
void performCalculation() {
switch (operation) {
case ‘A’: currentNumber = firstNumber + secondNumber; break;
case ‘B’: currentNumber = firstNumber – secondNumber; break;
case ‘C’: currentNumber = firstNumber * secondNumber; break;
case ‘D’:
if (secondNumber == 0) {
lcd.print(“Err: Div by 0”);
delay(2000);
resetCalculator();
return;
}
currentNumber = firstNumber / secondNumber;
break;
}
calculateResult = false;
}
void displayCalculation() {
lcd.setCursor(0, 0);
lcd.print(firstNumber);
lcd.print(operation);
lcd.print(secondNumber);
if (calculateResult == false) {
lcd.setCursor(0, 1);
lcd.print(“= “);
lcd.print(currentNumber);
}
}

Leave a Reply