Friday, 3 November 2017

Build a Simple Arduino Calculator

A Calculator is a device that is used to perform simple arithmetic operations to complex mathematical calculations. The rise of calculators can be seen from devices like Abacus to credit card sized complex electronic solid state devices in the modern day usage.
Apart from small calculators, which can be used to perform simple arithmetic calculations, calculators also come in complex scientific outlook that can perform various mathematical and statistical operations like trigonometry, algebra, calculus, etc. But such complex scientific calculators can be very costly.
In this project, we will design a simple arithmetic calculator using Arduino UNO, a 16 x 2 LCD display and 4 x 4 Matrix Keypad.

Circuit Diagram






Components


  • Arduino UNO
  • 16 x 2 LCD Display
  • 4 x 4 Matrix Keypad Module or 16 Push buttons
  • 10 KΩ Potentiometer
  • Bread board ( Prototyping board )
  • Connecting wires

Component Description

Arduino UNO:
The Microcontroller part of the project is the Arduino UNO. It controls the LCD and matrix keypad and also performs the necessary calculations as per the user input.
16 x 2 LCD Display:
It is a simple alpha – numeric display module that is used to display the welcome messages, user inputs and finally the output of the calculations.
4 x 4 Matrix Keypad:
Keypads are a part of Human Machine Interface and play really important role in embedded systems where human input is needed. Matrix Keypads are commonly used in calculators, telephones etc. where a number of input switches are required.
Construction of a 4X4 keypad is really simple. It consists of 16 buttons or switches arranged in the form of an array containing four rows and four columns. One end of the pushbutton is connected to one row, and the other end is connected to one column.

Circuit Design

The design of the circuit is simple to understand but a little bit messy to implement as it involves a lot of connections and wires. The design of the circuit is explained here.
First, the connection of LCD display to Arduino UNO. LCD is used in 4 – bit mode and hence, only 4 data connections are needed. The data pins D4 through D7 (Pins 11 – 14) are connected to digital I/O pins 2 through 5 of the Arduino.
Then, we need to connect the three control pins of LCD i.e. RS, RW and E. RS (Pin 4) is connected to Pin 0 of the Arduino. E (Pin 6) is connected to Pin 1 of the Arduino. RW (Pin 5) is connected to ground.
Note:
RS and E are connected to Pins 0 and 1 of Arduino which are usually associated with serial communication. We must be careful while programming the device and hence we must disconnect these two connections while we are programming Arduino UNO.
The next connections are with respect to power supply of LCD. Pin 1 (VSS) is connected to ground and Pin 2 (VDD) is connected to 5V supply. Pin 3 (VEE) is connected to a POT for adjusting the contrast of display.
Pin 15 and Pin 16 are supply pins for backlight LED. They must be connected to 5V and ground supply.
The next connection is with respect to matrix keypad. The 4 row pins of the keypad (row 1 to row 4) are connected to pins 13 through 10 of Arduino UNO and the 4 column pins (col 1 to col 4) are connected to pins 9 through 6 of Arduino UNO.

Working Process

A simple calculator is designed in this project using Arduino UNO board and a keypad matrix. The working of the project is explained here.
The keypad in the project consists of 4 rows and 4 columns and the structure is similar to the image shown below.

A special library called “Keypad.h” is used in order to find out which key is pressed. This library must be downloaded separately and must be added to Arduino IDE.
When the key is determined, relevant operations like addition, subtraction, multiplication and division can be performed. A clear screen (of LCD display) button is also included.
Even though the library does the most of the work, we can understand the processing of determining the key.
As mentioned in the circuit design, the rows of the keypad are connected to pins 13 – 10 of Arduino and columns of keypad are connected to pins 9 – 6 of Arduino.
All the row pins are pulled high and all the column pins are pulled low. From now onwards, the Arduino waits for the column pin to become HIGH, which happens if a key is pressed.
Consider, if a key is pressed, the switch closes the connection between the corresponding row and column. Due to the flow of current from high to low potential, the column will become high.
This change in potential at the column, makes the Arduino to understand that the key corresponding to the particular column is pressed.
Now it’s time to find the row. Instead of giving HIGH signal to all the rows at once, Arduino will enable HIGH to one row at a time and check whether there is a HIGH signal detected on the column. If there is no HIGH signal detected on the corresponding column, Arduino will scan for the next row.
This process will repeat until there is a HIGH signal detected on the corresponding column. Once if the HIGH signal is detected on the corresponding column, thus the row is identified.
From the outside, this key detection process seems to be taking long time but really the time taken to complete all the above mentioned process will be in micro seconds. But the average time a human takes for pushing a button is in milli seconds. That’s why, the key detection process will start when the key is pressed and completes before the key is released.

Advantages

  • A simple arithmetic calculator is implemented in this project using Arduino UNO, matrix keypad and LCD.
  • Can be extended to perform complex calculations, but the logic must be carefully designed in order to solve those calculations. This may even result in increased number of switches.

Project Images








Code


#include <LiquidCrystal.h>
#include <Keypad.h>
LiquidCrystal lcd(0, 1, 2, 3, 4, 5);
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS] [COLS] = {
{'7', '8', '9', '/'},
{'4', '5', '6', '*'},
{'1', '2', '3', '-'},
{'C', '0', '=', '+'}
};
byte rowPins[ROWS] = {13 ,12 ,11 ,10};
byte colPins[COLS] = {9, 8, 7, 6};
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
boolean presentValue = false;
boolean next = false;
boolean final = false;
String num1, num2;
int answer;
char op;
void setup()
{
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Electronics Hub ");
lcd.setCursor(0,1);
lcd.print(" Presents ");
delay(5000);
lcd.setCursor(0,0);
lcd.print(" Arduino based ");
lcd.setCursor(0,1);
lcd.print(" Calculator" );
delay(5000);
lcd.clear();
}
void loop(){
char key = myKeypad.getKey();
if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'))
{
if (presentValue != true)
{
num1 = num1 + key;
int numLength = num1.length();
lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
lcd.print(num1);
}
else
{
num2 = num2 + key;
int numLength = num2.length();
lcd.setCursor(15 - numLength, 1);
lcd.print(num2);
final = true;
}
}
else if (presentValue == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+'))
{
if (presentValue == false)
{
presentValue = true;
op = key;
lcd.setCursor(15,0);
lcd.print(op);
}
}
else if (final == true && key != NO_KEY && key == '='){
if (op == '+'){
answer = num1.toInt() + num2.toInt();
}
else if (op == '-'){
answer = num1.toInt() - num2.toInt();
}
else if (op == '*'){
answer = num1.toInt() * num2.toInt();
}
else if (op == '/'){
answer = num1.toInt() / num2.toInt();
}
lcd.clear();
lcd.setCursor(15,0);
lcd.autoscroll();
lcd.print(answer);
lcd.noAutoscroll();
}
else if (key != NO_KEY && key == 'C'){
lcd.clear();
presentValue = false;
final = false;
num1 = "";
num2 = "";
answer = 0;
op = ' ';
}
}

0 comments:

Post a Comment