Introduction
In this article, I have explained about creating a simple calculator with Arduino Mega 2560. It can be used to calculate for Addition, Multiplication, Division, and Subtraction. The result will be displayed in the LCD.
Parts Of Lists
- Arduino Mega 2560
- LEDs
- Push Button
- Potentiometer
- LCD Display
- Bread Board
- Hookup Wires
Potentiometer
- It can have three terminals.
- It can have sliding or rotating contact that forms an adjustable voltage divider.
- It acts as a variable resistor (or) rheostat.

Figure 1: Potentiometer
LCD Display
- LCD is Liquid Crystal Display (LCD).
- It can have flat-panel or electronic display
- It is used in hospital, showrooms, buses, railway stations, airports, etc.

Figure 2: LCD Display
Push Button
- The push-button connects the two points.
- The first pin in the push button is connected to 5Vc.
- The second pin in the push button is connected to gnd.

Figure 3: Push Button
Connection
Step 1: Connection from Arduino Mega 2560 to LCD Display.
- Fix the LCD in the 16 to 2 in the bread board.
- The 16 pin to the Gnd
- The 15 pin to the Vcc
- The 14 pin to the Digital pin 12
- The 13 pin to the Digital pin 11
- The 12 pin to the Digital pin 05
- The 11 pin to the Digital pin 04
- The 01 pin to the Gnd.
- The 02 pin to the Vcc.
- The 03 pin to the potentiometer.
- The 04 pin to the Digital pin 03.
- The 05 pin to the Gnd.
- The 06 pin to the Digital pin 02.

Figure 4: LCD Display connection
Step 2: Connection from Push Button to ArduinoMega2560
Push-button 1:
- The first pin in the push button is connected to 5Vc and 12 of board
- The second pin in the push button is connected to gnd.
Push-button 2:
- The first pin in the push button is connected to 5Vc and 13 of board
- The second pin in the push button is connected to gnd.
Push-button 3:
- The first pin in the push button is connected to 5Vc and 14 of board
- The second pin in the push button is connected to gnd.
Push-button 4:
- The first pin in the push button is connected to 5Vc and 15 of board
- The second pin in the push button is connected to gnd.
Push-button 5:
- The first pin in the push button is connected to 5Vc and 16 of board
- The second pin in the push button is connected to gnd.
Push-button 6:
- The first pin in the push button is connected to 5Vc and 17 of board
- The second pin in the push button is connected to gnd.
Push-button 7:
- The first pin in the push button is connected to 5Vc and 18 of board
- The second pin in the push button is connected to gnd.
Push-button 8:
- The first pin in the push button is connected to 5Vc and 19 of board
- The second pin in the push button is connected to gnd.
Push-button 9:
- The first pin in the push button is connected to 5Vc and 20 of board
- The second pin in the push button is connected to gnd.
Push-button 10:
- The first pin in the push button is connected to 5Vc and 21 of board
- The second pin in the push button is connected to gnd.
Push-button 11:
- The first pin in the push button is connected to 5Vc and 22 of board
- The second pin in the push button is connected to gnd.
Push-button 12:
- The first pin in the push button is connected to 5Vc and 23 of board
- The second pin in the push button is connected to gnd.
Programming:
Explanation
- // include the library code:
- #include <LiquidCrystal.h>
- #define key0 12
- #define key1 13
- #define key2 14
- #define key3 15
- #define key4 16
- #define key5 17
- #define key6 18
- #define key7 19
- #define key8 20
- #define key9 21
- #define DEL 22
- #define ENTER 23
- #define MAXVALUE 99999
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- long number_1 = 0;
- long number_2 = 0;
- int control = 0;
- boolean error = false;
- long result = 0;
- float result_div = 0.0;
- void insertNumb () {
- for (int i = 0; i <= 9; i++) {
- //whithout this delay, the selection button pressed could become the first digit
- delay (200);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("FIRST NUMBER");
- control = readKey ();
- //statement needed to catch the first digit
- if ( i == 0 ) {
- //statement needed to check if the pressed button is the special key OK
- //if yes "i" becomes 5 and the algorithm goes to the next for loop
- if ( control == 11 ) {
- i = 5;
- }
- //statement needed to check if the pressed button is the special key DEL
- //if yes "i" becomes 10 and the algorithm goes from both for loops
- //resetting the calculator
- else if ( control == 10) {
- i = 10;
- error = true;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("CANCELLED!");
- delay(2000);
- }
- else {
- //stores the most significant digit
- number_1 = control;
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print(control);
- delay(500);
- }
- }
- //statement needed to catch the remaining four digits
- //the following conditional statements have the same purpose of the ones described above
- else {
- if ( control == 11 ) {
- i = 5;
- }
- else if ( control == 10) {
- i = 10;
- error = true;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("CANCELLED!");
- delay(2000);
- }
- else {
- number_1 = (number_1 * 10) + control;
- //check if number_1 is negative or is greater than MAXVALUE printing an error if yes
- if ((number_1 < 0) || (number_1 > MAXVALUE)) {
- error = true;
- i = 10;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(2000);
- }
- else {
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print(control);
- lcd.setCursor(0, 0);
- lcd.print(number_1);
- delay(500);
- }
- }
- }
- if (i >= 4 && i <= 9) {
- //the following for loop catches the digits of the second number
- //and basically does the same thing as the previous one
- for (int j = 0; j <= 4; j++) {
- delay (200);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("SECOND NUMBER");
- control = readKey ();
- i++;
- if ( j == 0 ) {
- if ( control == 11 ) {
- i = 10;
- j = 5;
- }
- else if ( control == 10 ) {
- error = true;
- j = 5;
- i = 10;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("CANCELLED!");
- delay(2000);
- }
- else {
- number_2 = control;
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print(control);
- delay(500);
- }
- }
- else {
- if ( control == 11 ) {
- j = 5;
- i = 10;
- }
- else if ( control == 10 ) {
- error = true;
- j = 5;
- i = 10;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("CANCELLED!");
- delay(2000);
- }
- else {
- number_2 = (number_2 * 10) + control;
- //check if number_2 is negative or is greater than MAXVALUE printing an error if yes
- if ((number_2 < 0) || (number_2 > MAXVALUE)) {
- error = true;
- j = 5;
- i = 10;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(2000);
- }
- else {
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print(control);
- lcd.setCursor(0, 0);
- lcd.print(number_2);
- delay(500);
- }
- }
- }
- }
- }
- }
- }
- void wait () {
- while (digitalRead(key0) == LOW &&
- digitalRead(key1) == LOW &&
- digitalRead(key2) == LOW &&
- digitalRead(key3) == LOW &&
- digitalRead(key4) == LOW &&
- digitalRead(key5) == LOW &&
- digitalRead(key6) == LOW &&
- digitalRead(key7) == LOW &&
- digitalRead(key8) == LOW &&
- digitalRead(key9) == LOW &&
- digitalRead(DEL) == LOW &&
- digitalRead(ENTER) == LOW
- );
- }
- void setup() {
- //initializes push-buttons
- pinMode(key0, INPUT);
- pinMode(key1, INPUT);
- pinMode(key2, INPUT);
- pinMode(key3, INPUT);
- pinMode(key4, INPUT);
- pinMode(key5, INPUT);
- pinMode(key6, INPUT);
- pinMode(key7, INPUT);
- pinMode(key8, INPUT);
- pinMode(key9, INPUT);
- pinMode(DEL, INPUT);
- pinMode(ENTER, INPUT);
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- }
- int readKey () {
- wait();
- int operand = 0;
- if (digitalRead(key0) == HIGH) {
- operand = 0;
- }
- else if (digitalRead(key1) == HIGH) {
- operand = 1;
- }
- else if (digitalRead(key2) == HIGH) {
- operand = 2;
- }
- else if (digitalRead(key3) == HIGH) {
- operand = 3;
- }
- else if (digitalRead(key4) == HIGH) {
- operand = 4;
- }
- else if (digitalRead(key5) == HIGH) {
- operand = 5;
- }
- else if (digitalRead(key6) == HIGH) {
- operand = 6;
- }
- else if (digitalRead(key7) == HIGH) {
- operand = 7;
- }
- else if (digitalRead(key8) == HIGH) {
- operand = 8;
- }
- else if (digitalRead(key9) == HIGH) {
- operand = 9;
- }
- else if (digitalRead(DEL) == HIGH) {
- operand = 10;
- }
- else if (digitalRead(ENTER) == HIGH) {
- operand = 11;
- }
- delay(100);
- return operand;
- }
- //SUM function
- long sum (long number_1, long number_2) {
- return number_1 + number_2;
- }
- //SUBtraction function
- long subtraction (long number_1, long number_2) {
- return number_1 - number_2;
- }
- //MULtiplication function
- long multiplication (long number_1, long number_2) {
- return number_1 * number_2;
- }
- //DIVsion FUNCTION
- float division (long number_1, long number_2) {
- return (float)number_1 / (float)number_2;
- }
- void loop() {
- number_1 = 0;
- number_2 = 0;
- delay (200);
- //asks which calculation you want to do
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("SUM,SUB,MUL,DIV");
- lcd.setCursor(0, 1);
- lcd.print("0 ,1 ,2 ,3 ");
- //waits into the following loop till the user press a key
- wait();
- //"operation" is the variable needed to control the flow of the sketch
- int operation = readKey();
- //SUM
- if (operation == 0) {
- insertNumb();
- if (error == true) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- }
- else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- lcd.setCursor(0, 1);
- lcd.print("SUM = ");
- lcd.setCursor(7, 1);
- result = sum(number_1, number_2);
- if (result < MAXVALUE) {
- lcd.print(result);
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(4000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- }
- }
- while (readKey() != 10);
- }
- else if (operation == 1) {
- insertNumb();
- if (error == true) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- lcd.setCursor(0, 1);
- lcd.print("SUB = ");
- lcd.setCursor(7, 1);
- result = subtraction(number_1, number_2);
- if (result < MAXVALUE) {
- lcd.print(result);
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(4000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- }
- }
- while (readKey() != 10);
- }
- else if (operation == 2) {
- insertNumb();
- if (error == true) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- lcd.setCursor(0, 1);
- lcd.print("MUL = ");
- lcd.setCursor(7, 1);
- result = multiplication(number_1, number_2);
- if (result < MAXVALUE) {
- lcd.print(result);
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(4000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- }
- }
- while (readKey() != 10);
- }
- else if (operation == 3) {
- insertNumb();
- if (error == true) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- lcd.setCursor(0, 1);
- lcd.print("DIV = ");
- lcd.setCursor(7, 1);
- result_div = division(number_1, number_2);
- if (result < MAXVALUE) {
- lcd.print(result_div, 7);
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(4000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- }
- }
- while (readKey() != 10);
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("PRESS 0,1,2 or 3");
- delay(2000);
- }
- int control = 0;
- }
- This calculator can be used to calculate the for loop() function. It can be used to display one by one condition i.e addition/ subtraction/ multiplexing/ division.
- The result can be displayed in the LCD display screen. If we made any mistake, press del to continue.

Figure 5: Output

Sr KarthigaPosted May 25, 2016, 8:09 AM
thank you santhakumar sir
Santhakumar MunuswamyPosted May 21, 2016, 2:05 AM
Thank you for nice work
Sr KarthigaPosted May 20, 2016, 10:32 AM
thank you prasanna
Sr KarthigaPosted May 20, 2016, 10:32 AM
thank you sonu sir
Sr KarthigaPosted May 20, 2016, 10:32 AM
thank you pankaj sir
Sr KarthigaPosted May 20, 2016, 10:31 AM
Thank you raja sir
Prasanna MuraliPosted May 20, 2016, 9:56 AM
Good one
Sonu ChaudharyPosted May 19, 2016, 11:31 AM
great job
Pankaj Kumar ChoudharyPosted May 18, 2016, 12:45 PM
Nice Article and great Work........
RajaPosted May 18, 2016, 2:56 AM
Great work..
Sr KarthigaPosted May 17, 2016, 9:37 PM
y u cannot try my article
Bhuvanesh MohankumarPosted May 17, 2016, 2:19 PM
We can't try your article :) but good to read