Build A Calculator with Arduino UNO
Arduino Uno is a widely used microcontroller board featuring the Atmega328P chip. With 14 digital I/O pins, 6 analog inputs, and a 16 MHz clock, it offers versatile connectivity. The board can be powered via USB or an external supply (7–12V). It supports a user-friendly IDE for programming, simplifying code development. As part of the open-source community, Uno encourages modification and sharing.
In this article, we are going to build a basic arithmetic calculator using Arduino Uno, LCD (Liquid Crystal Display) for output, and keypad for input. The calculation expression can be sent using the keypad, and the LCD will be used to display it, as well as the result.
Components:
- Arduino UNO x 1
- LCD x 1
- 4x4 Keypad x 1
- Potentiometer x 1
- Jumper cables
You need your Arduino IDE to write the firmware code. If you haven’t installed it, you can download the setup through this link.
Schematic:
Libraries
When writing the firmware code, we are going to need the libraries of the LCD and keypad which include:
- LiquidCrystal
- Keypad
The next thing is to write the firmware code:
#include <Keypad.h>
#include <LiquidCrystal.h>
#define ROWS 4 // The Keypad has 4 rows
#define COLS 4 // The Keypad has 4 Columns
// Initialization of the LiquidCrystal object
// rs, en, d4, d5, d6, and d7 are the lcd pins connected to the arduino uno
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd = LiquidCrystal(rs, en, d4, d5, d6, d7);
// Initializing the keys of the keypad
// This will be mapped to our keypad such that the rows and cols
// of the keypad will match the characters here
char keys[ROWS][COLS] = {
{ '7', '8', '9', '/' },
{ '4', '5', '6', '*' },
{ '1', '2', '3', '-' },
{ 'C', '0', '=', '+' }
};
// Corresponding row and column pins of the keypad connected to arduino uno
const byte rowPins[4] = { 6, 5, 4, 3 };
const byte colPins[4] = { A0, A1, A2, A3 };
// Initializaion of the keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// This is the variable that will store our expressions
String expression;
// This variable is a logical variable to know if an expression has been answered
bool answered = false;
void setup() {
// Begin the lcd process
// 16,2 here shows that the lcd is a 16x2 lcd
lcd.begin(16, 2);
lcd.print("Calculator");
delay(2000);
lcd.clear();
}
void loop() {
char _key = keypad.getKey(); // Function to get a pressed key on the keypad
if (_key) { // If a key is pressed
if (_key == 'C') { // Check if the 'clear' key is pressed
// If the clear key is pressed, it will reset the values in expression
// and reset the value of the answered variable
resetValues();
answered = false;
} else if (_key == '=') {
// To get the answer to the expression, the eval() function is used
// The result was converted to string using the String() built-in function
double result = eval(expression);
String resString = String(result);
// I converted the result to string because I want to get the length
// in order to display the answer at the right side on the second line of the lcd
lcd.setCursor(0, 1);
lcd.print(" ");
int pos = 15 - resString.length();
lcd.setCursor(pos, 1);
lcd.print(resString);
answered = true; // Sets the answered variable to true after the result is gotten
} else {
// This automatically reset the values in the expression and clears the screen
// If another key is pressed after the previous answer has been generated
if (answered) {
resetValues();
}
// Append the keys to the expression
expression += _key;
lcd.setCursor(0, 0);
lcd.print(expression);
}
}
}
void resetValues() {
lcd.clear();
expression = "";
answered = false;
}
// Function to get the solution
double eval(String expression) {
// This is a simple evaluator, for more complex calculations
// you might need a more advanced algorithm
int operand1 = 0;
int operand2 = 0;
char opSymbol = '+';
// sscanf function is used to get the operands and operator symbol from the expression
sscanf(expression.c_str(), "%d%c%d", &operand1, &opSymbol, &operand2);
// Based on the operator, the operation will be performed
switch (opSymbol) {
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
case '*':
return operand1 * operand2;
case '/':
// This checks for zero division error
if (operand2 == 0) {
lcd.setCursor(0, 1);
lcd.print("Math Error");
delay(1000);
resetValues();
return;
}
return operand1 / operand2;
default:
lcd.print("Error");
delay(1000);
resetValues();
return;
}
}
Here’s an example of how the output will be displayed:
Overall, the project is fun, and you can try developing your own algorithm to solve more complex expressions. Have fun!