In this project, you will build a simpleBasic Arithmetic Calculator in MicroPython for ESP32 and ESP8266. The calculator will perform the four basic arithmetic operations: addition, subtraction, multiplication, and division, displaying the result of each operation. By working through this project, you will gain hands-on experience with arithmetic operators in MicroPython, making it an ideal starting point for beginners to understand fundamental mathematical operations in programming.
Data Types and Variable Table for Basic Arithmetic Calculator:
Data Type | Variable Name | Description | Example Value |
Integer/Float | num1 | The first number for arithmetic operations | 5 or 5.0 |
Integer/Float | num2 | The second number for arithmetic operations | 3 or 3.0 |
Float | result | Stores the result of the arithmetic operations | 8.0 or 2.5 |
In this project, you’ll see how arithmetic operators interact with different data types such as integers and floats.
Syntax Table for Basic Arithmetic Calculator in MicroPython:
Operation | Syntax | Example |
Addition | result = num1 + num2 | result = 5 + 3 |
Subtraction | result = num1 – num2 | result = 5 – 3 |
Multiplication | result = num1 * num2 | result = 5 * 3 |
Division | result = num1 / num2 | result = 5 / 2 |
Each of these operations will be performed by the calculator, and the result will be printed to the console.
Required Components:
- ESP32 or ESP8266 (For running the MicroPython code)
- Computer (For writing and uploading the MicroPython script)
Since this project focuses on arithmetic operations and does not involve external hardware, no additional components are needed.
Circuit Diagram:
(No circuit diagram is required as this project is purely programming-based.)
Circuit Connection Table:
(No circuit connections are needed for this project.)
Warnings:
- Division by zero is not allowed and will result in a runtime error. Ensure that you handle this case properly in the code.
Installing MicroPython and Libraries (If Needed):
Install MicroPython on ESP32/ESP8266: If you haven’t installed MicroPython on your ESP32 or ESP8266, follow these steps:
esptool.py –chip esp32 erase_flash
esptool.py –chip esp32 write_flash -z 0x1000 esp32-20210902-v1.17.bin
- No Additional Libraries Needed: The built-in operators in MicroPython are enough to perform basic arithmetic operations.
Writing the MicroPython Code for Basic Arithmetic Calculator:
Here’s the code that performs addition, subtraction, multiplication, and division using input numbers:
# Function for basic arithmetic calculator
def basic_calculator(num1, num2):
# Addition
result = num1 + num2
print(“Addition: “, num1, “+”, num2, “=”, result)
# Subtraction
result = num1 – num2
print(“Subtraction: “, num1, “-“, num2, “=”, result)
# Multiplication
result = num1 * num2
print(“Multiplication: “, num1, “*”, num2, “=”, result)
# Division (handling division by zero)
if num2 != 0:
result = num1 / num2
print(“Division: “, num1, “/”, num2, “=”, result)
else:
print(“Error: Division by zero is not allowed.”)
# Test the calculator with example values
num1 = float(input(“Enter the first number: “))
num2 = float(input(“Enter the second number: “))
basic_calculator(num1, num2)
Explanation of the Code:
- The function basic_calculator(num1, num2) performs the four arithmetic operations using the input values num1 and num2.
- The addition, subtraction, and multiplication results are printed directly.
- Division includes a check to ensure that division by zero does not occur. If num2 is zero, an error message is displayed.
- The input function allows the user to provide two numbers (which are converted to floats), making this calculator dynamic for various numbers.
Running the Code and Checking the Output:
- Upload the code to your ESP32/ESP8266 using Thonny or another MicroPython IDE.
- Enter the values for num1 and num2 when prompted.
Observe the results printed on the serial monitor, displaying the outcomes of the arithmetic operations:
Enter the first number: 5
Enter the second number: 3
Addition: 5.0 + 3.0 = 8.0
Subtraction: 5.0 – 3.0 = 2.0
Multiplication: 5.0 * 3.0 = 15.0
Division: 5.0 / 3.0 = 1.6666666666666667
Expanding the Project:
- Add more operations: You can add other operations like modulus (%) or exponentiation (**) to extend the calculator.
- Handle invalid input: Add input validation to ensure the user enters numeric values only.
- Create an interactive loop: Allow the user to continuously enter numbers and perform operations until they choose to exit.
Common Problems and Solutions:
- Problem: Division by zero causes the program to crash.
- Solution: The program already includes a check for division by zero, which ensures the user is notified when they attempt to divide by zero. Always add such checks in real-world programs.
- Problem: Invalid input results in an error.
- Solution: You can wrap the input section in a try-except block to handle invalid input (such as non-numeric values). Use try: float(input()) to safely parse the input.
- Problem: Floating-point precision issues.
- Solution: Floating-point arithmetic can sometimes lead to precision errors. Use round() if necessary to format the result to a specified number of decimal places.
FAQ:
Q: Can I extend the calculator to handle other mathematical operations?
A: Yes, you can easily add more operations like modulus (%) or exponentiation (**). Simply extend the basic_calculator function to include more operators.
Q: How do I handle invalid inputs (like text) in the calculator?
A: You can use try-except blocks to catch errors when non-numeric values are entered, ensuring the program doesn’t crash.
Q: Why does division by zero cause an error?
A: Division by zero is mathematically undefined, and MicroPython (like other programming languages) will raise an error when attempting this. Always check for division by zero before performing the division.
Conclusion:
In this project, you successfully built a Basic Arithmetic Calculator in MicroPython for ESP32 and ESP8266 that performs addition, subtraction, multiplication, and division. By using arithmetic operators and handling division by zero, you gained hands-on experience with fundamental mathematical operations in MicroPython. This project can be expanded to include more complex calculations, providing a strong foundation for future programming projects.