Arduino Based Calculator Using Keyboard and LCD
by Lisleapex Blog in Circuits > Arduino
176 Views, 0 Favorites, 0 Comments
Arduino Based Calculator Using Keyboard and LCD
A simple Arduino based calculator can be easily implemented using Arduino development board, LCD and keyboard to solve mathematical calculations using keyboard and LCD. Simple mathematical calculations such as addition, subtraction, multiplication and division can be easily done using this project.
1602 LCD is able to solve mathematical calculations within 16 bits without delay and fast processing. The Arduino program is also very simple, almost like a simple C program, based only on simple formulas of addition, subtraction, multiplication and division. So, let's implement this simple little project.
Supplies
● Arduino Uno development board
● 1602 LCD screen
● 4 * 4 film matrix keyboard
● Reset switch
● 9V battery
● Breadboard and connecting wires
Circuit Diagram
The circuit diagram is very simple. You can assemble the circuit directly on the Arduino development board or by making a PCB.
Source Code
The program is based on the Arduino UNO development board and is completed using the Arduino language. You need to add an additional Arduino library file for the keyboard, such as #include <Keypad.h>. Therefore, download this file from the adafruit library. After assembly, just upload the source code to the Arduino Uno development board and you can enjoy the Arduino-based calculator.
Coding
- #include <Keypad.h>
- #include <LiquidCrystal.h>
- // 1. LCD Pins
- LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
- // 2. Keypad Pins
- const byte Rows = 4;
- const byte Cols = 4;
- char keys[Rows][Cols] =
- {
- {'1', '2', '3', '+'},
- {'4', '5', '6', '-'},
- {'7', '8', '9', '*'},
- {'C', '0', '=', '/'}
- };
- byte rowPins[Rows] = {A2, A3, A4, A5};
- byte colPins[Cols] = {2, 3, 4, 5};
- Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, Rows, Cols);
- // 3. Dot Button
- int dot = A0;
- int dotFlag = 0;
- int dotButton = 0;
- // 4. Calculator Operators
- float num1, num2, fraction;
- float total;
- char operation, button;
- // 5. Loading Setup
- char input[16];
- int n = 1750;
- void setup()
- {
- // Initialize dot button as input to Arduino
- pinMode(dot, INPUT);
- // Initialize LCD Size
- lcd.begin(16, 2);
- // LCD Loading Setup Begin
- lcd.clear();
- lcd.setCursor(3, 0);
- lcd.print("LOADING...");
- for (int i = 0; i < 16; i++)
- {
- lcd.setCursor(i, 1);
- lcd.write(255);
- delay(50);
- }
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print("Simple Arduino");
- lcd.setCursor(3, 1);
- lcd.print(" Calculator");
- delay(n);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Designed By");
- lcd.setCursor(2, 1);
- lcd.print("Alex Newton");
- delay(n);
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print("Alex Newton");
- lcd.setCursor(2, 1);
- lcd.print("how2electronics.com");
- delay(n);
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print("Alex Newton");
- lcd.setCursor(2, 1);
- lcd.print("how2electronics.com");
- delay(n);
- lcd.clear();
- // LCD Loading Setup End
- }
- void loop()
- {
- // First while loop for num1.
- while (1)
- {
- dotButton = digitalRead(dot);
- button = customKeypad.getKey();
- if (button == 'C')
- {
- dotFlag = 0;
- num1 = 0;
- num2 = 0;
- fraction = 0;
- total = 0;
- operation = 0;
- lcd.clear();
- }
- else if (dotButton == LOW)
- {
- dotFlag = 1;
- }
- else if (button >= '0' && button <= '9')
- {
- if (dotFlag == 0)
- {
- num1 = num1 * 10 + (button - '0');
- lcd.setCursor(0, 0);
- lcd.print(num1);
- }
- else if (dotFlag == 1)
- {
- fraction = (button - '0');
- num1 = num1 + (fraction / 10);
- lcd.setCursor(0, 0);
- lcd.print(num1);
- dotFlag++;
- }
- else if (dotFlag == 2)
- {
- fraction = (button - '0');
- num1 = num1 + (fraction / 100);
- lcd.setCursor(0, 0);
- lcd.print(num1);
- dotFlag++;
- }
- }
- else if (button == '-' || button == '+' || button == '*' || button == '/')
- {
- operation = button;
- dotFlag = 0;
- lcd.setCursor(0, 1);
- lcd.print(operation);
- break;
- }
- }
- // Second while loop for num2.
- while (1)
- {
- dotButton = digitalRead(dot);
- button = customKeypad.getKey();
- if (button == 'C')
- {
- dotFlag = 0;
- num1 = 0;
- num2 = 0;
- fraction = 0;
- total = 0;
- operation = 0;
- lcd.clear();
- break;
- }
- else if (dotButton == LOW)
- {
- dotFlag = 1;
- }
- else if (button >= '0' && button <= '9')
- {
- if (dotFlag == 0)
- {
- num2 = num2 * 10 + (button - '0');
- lcd.setCursor(1, 1);
- lcd.print(num2);
- }
- else if (dotFlag == 1)
- {
- fraction = (button - '0');
- num2 = num2 + (fraction / 10);
- lcd.setCursor(1, 1);
- lcd.print(num2);
- dotFlag++;
- }
- else if (dotFlag == 2)
- {
- fraction = (button - '0');
- num2 = num2 + (fraction / 100);
- lcd.setCursor(1, 1);
- lcd.print(num2);
- dotFlag++;
- }
- }
- if (button == '=')
- {
- domath();
- break;
- }
- }
- // Third while loop for ensuring C button is executed after while loop 2.
- while (1)
- {
- button = customKeypad.getKey();
- if (button == 'C')
- {
- dotFlag = 0;
- num1 = 0;
- num2 = 0;
- fraction = 0;
- total = 0;
- operation = 0;
- lcd.clear();
- break;
- }
- }
- }
- void domath()
- {
- switch (operation)
- {
- case '+':
- total = num1 + num2;
- break;
- case '-':
- total = num1 - num2;
- break;
- case '/':
- total = num1 / num2;
- break;
- case '*':
- total = num1 * num2;
- break;
- }
- lcd.print('=');
- if (operation == '/' && num2 == 0)
- {
- lcd.print("ERROR 0 DIV");
- }
- else
- {
- lcd.print(total);
- }
- }
Work It Out Finally
Make the connections as per the circuit diagram and upload the code below. If it shows an error, make sure that you have added the library as per the instructions given above. You can also try simulation to check if the issue is with your hardware. If everything is done as expected, then your hardware will look like this with the LCD showing this