The objective of this project is to create a basic calculator using Arduino and buttons for input. The calculator will perform arithmetic operations like addition, subtraction, multiplication, and division based on user input. This project introduces the use of arithmetic operators, if-else conditions, and switch case structures in Arduino programming.
Fundamental Programming Concepts
- Arithmetic Operators: Use operators such as +, –, *, and / to perform mathematical calculations.
- if-else Conditions: Control the logic to perform different actions based on button presses.
- Digital Inputs: Use buttons to input numbers and operations into the calculator.
- Switch Case: Select the arithmetic operation (addition, subtraction, multiplication, or division) based on user input.
Requirement Components
To build this simple Arduino calculator, you’ll need the following components:
- Arduino Uno Board
- Push Buttons (at least 6) (to input numbers and operations)
- 10kΩ Resistors (for pull-down resistors)
- Breadboard
- Jumper Wires
- USB Cable (to connect Arduino to your computer)
Circuit Diagram
Circuit Connection
Component | Arduino Pin |
Button 1 (Number 1) | Pin 2 |
Button 2 (Number 2) | Pin 3 |
Button 3 (Addition) | Pin 4 |
Button 4 (Subtraction) | Pin 5 |
Button 5 (Multiplication) | Pin 6 |
Button 6 (Division) | Pin 7 |
Ground (for all buttons) | GND |
How to Connect the Circuit
- Insert each button into the breadboard.
- Connect one side of each button to the specified Arduino pin.
- Connect the other side of each button to GND using a 10kΩ pull-down resistor.
- Ensure that the circuit is connected correctly before proceeding.
Explanation of Circuit
- Each button serves as an input for numbers and arithmetic operations. When pressed, the buttons send a HIGH signal to the corresponding Arduino pin.
- The Arduino uses digitalRead() to check the button presses and stores the numbers and operation in variables. The switch case structure handles the arithmetic operation based on user input.
Programming Section
Arduino Syntax
Topic Name | Syntax | Explanation |
Digital Read | digitalRead(pin) | Reads the state of a digital input pin (HIGH or LOW). |
If-else Condition | if (condition) { } else { } | Executes code based on a condition being true or false. |
Switch Case | switch (variable) { case … } | Executes a block of code based on the value of a variable. |
Arithmetic Operators | +, –, *, / | Used for performing basic mathematical operations. |
Arduino Code for basic calculator using Arduino
Here is the Arduino code to implement the simple calculator with buttons:
// Define pins for buttons
const int button1 = 2;
const int button2 = 3;
const int addButton = 4;
const int subButton = 5;
const int mulButton = 6;
const int divButton = 7;
// Variables to store input values and result
int number1 = 0;
int number2 = 0;
char operation;
int result = 0;
void setup() {
// Set button pins as input
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(addButton, INPUT);
pinMode(subButton, INPUT);
pinMode(mulButton, INPUT);
pinMode(divButton, INPUT);
// Start Serial Communication
Serial.begin(9600);
}
void loop() {
// Check if number buttons are pressed
if (digitalRead(button1) == HIGH) {
number1 = 1; // Number 1
}
if (digitalRead(button2) == HIGH) {
number2 = 2; // Number 2
}
// Check if operation buttons are pressed
if (digitalRead(addButton) == HIGH) {
operation = '+';
}
if (digitalRead(subButton) == HIGH) {
operation = '-';
}
if (digitalRead(mulButton) == HIGH) {
operation = '*';
}
if (digitalRead(divButton) == HIGH) {
operation = '/';
}
// Perform calculation using switch case
switch (operation) {
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '*':
result = number1 * number2;
break;
case '/':
if (number2 != 0) {
result = number1 / number2;
} else {
Serial.println("Error: Division by zero");
}
break;
}
// Print the result to the Serial Monitor
Serial.print("Result: ");
Serial.println(result);
delay(1000); // Add a small delay before next input
}
Steps to Upload Code:
- Connect your Arduino to your computer using a USB cable.
- Open the Arduino IDE and select the correct Board and Port.
- Copy and paste the code into a new sketch.
- Click the Upload button to transfer the code to your Arduino.
- Open the Serial Monitor to view the result of your calculations.
Check Output
After uploading the code, press the buttons corresponding to numbers and operations. You should see the result of the operation displayed on the Serial Monitor. For example, pressing the button for 1, followed by the addition button, then pressing 2, will give the result 3.
Troubleshooting Tips
- Button not registering input? Ensure the buttons are properly connected to their respective pins and that pull-down resistors are in place.
- Division by zero error? The code already checks for division by zero. If you attempt it, the message “Error: Division by zero” will be displayed on the Serial Monitor.
- No output on the Serial Monitor? Ensure the baud rate in the Serial Monitor is set to 9600, and verify the correct COM port is selected.
Further Exploration
- Add More Numbers: Expand the project to include buttons for numbers 0-9 to perform more complex calculations.
- Add an LCD Display: Display the result on an LCD screen instead of using the Serial Monitor.
- Add a Clear Button: Add a button to reset the numbers and start over.
Note
This project introduces important concepts such as digital inputs, if-else conditions, and switch cases. By creating this Arduino calculator, you’ll learn how to manage user input, perform arithmetic operations, and output results via the Serial Monitor.
FAQ
Q1: How do the pull-down resistors work?
The pull-down resistors ensure that the pins connected to the buttons are pulled to LOW when the buttons are not pressed, preventing floating values.
Q2: What is the role of switch case in this project?
The switch case statement allows the Arduino to perform different arithmetic operations based on the operation selected by the user.
Q3: Can I use more numbers for calculation?
Yes, you can modify the project to include more buttons for numbers 0-9, making the calculator capable of handling more complex operations.
Q4: How can I display the result on an LCD?
To display the result on an LCD screen, you can connect an LCD to the Arduino and use the LiquidCrystal library to show the calculation result.