Maker.io main logo

Build a Basic Arduino-Based Calculator

2017-03-15 | By All About Circuits

License: See Original Project Arduino

Courtesy of All About Circuits

Calculators have come a long way since their invention. Over the past 4000 years, they have gone from the simple abacus, to Bitcoin mining calculators and quantum computers. The most common calculators these days are the programs built into smartphones and computers.

So it’s perfectly reasonable to ask, why would somebody make a calculator for Arduino with less functionality than the one that’s already in their pocket? Aleksandar, the guy who made this one, said he wanted to pay tribute to the calculators of old. Like many DIY projects, you could probably go out and buy a better one for cheaper than the cost of the materials to make it, but where’s the fun in that? So much of being a maker is in the journey, not the destination. This is also a good project for students. Let’s see how this thing works!

How Does it Work?

The calculator starts with a cleared LCD screen and waits for input from the keypad. When the user presses an operation button (plus, minus, multiply, or divide) it memorizes the first number the user entered and the desired operation. It then continues with the acquisition of the second number. When the user finished punching in the second number, the program performs the requested operation and prints the result when the equals button is pressed. At any point in time, you can press the CLEAR button (’C’) to start the sequence over... just like the calculator you probably used in elementary school.

Arduino Uno

Bill of Materials

Before Starting

It’s important to have both the  Keypad.h and LiquidCrystal.h libraries installed in your Arduino IDE. A few people who attempted this project got all the way through building theirs only to get this error code:

Calculator_Code.ino:2:20: fatal error: Keypad.h: No such file or directory

compilation terminated.

There are two ways to install these libraries. If your Arduino IDE is version 1.6.2 or newer, you can just use Library Manager. If you’re using an earlier version, you will need to copy them into the libraries folder in your Arduino IDE install location. If you’re brand new to Arduino, here's a tutorial on the Arduino website about how to install libraries.

LCD 1602a

The LiquidCrystal.h library supports LCDs based on the Hitachi HD44780 chipset. It’s responsible for displaying text on the LCD and prints floating point numbers to a specified number of decimal places. Take a look at the example below:

Copy Code
double Pi = 3.1415926535;

Lcd.print (Pi,4);

This prints 3.1415 on the LCD. The (Pi,4) represents Pi to the 4th decimal space.

The LCD used for the project

The LCD used for the project

Keypad

The keypad in this project requires the library for Arduino Keypad.h. This library eliminates the need for external pull-up resistors because it enables the on-chip integrated pull-up resistors and handles/sets high impedance on all unused column pins. It basically scans column by column. It also handles button debouncing by using a built-in Arduino millis() function and determines how long the button has been pressed or if there was a transition on a specific key. This allows the code to execute more efficiently and reduce the amount of processing power consumed.

Below is a wiring diagram for the keypad Aleksandar made:

wiring diagram

Here’s what you’ll need if you want to make your own:

  • 16 buttons
  • Small PCB proto-board
  • 8 wires (4 for row and 4 for column leads
  • Some solder and a soldering iron

A keyboard on a breadboard!

A keyboard on a breadboard!

Schematic

Below is a wiring diagram for the calculator. You can also find step by step instructions on All About Circuits.

 

 

Software/Code

The last step is the code, which consists of three loops. The first loop scans the keypad to print the buttons you push. With each number pressed, it shifts the previous number up by a tens place (For example: 1 would become 10, then 100). This continues until the user hits clear or an operation key symbols.

The second loop does the same thing as the first one, but it moves to the next loop when you hit “=” instead of an operation key. When the equals key is pressed, the calculator will calculate the total.

The third loop just resets the program when you hit the CLEAR button. You can find a more in depth explanation of the code and a downloadable version on All About Circuits.

The finished project!

The finished project!

You can watch this calculator in action in the video below. If you decide to try making your own Arduino calculator let us know in the comments, bonus points if you add extra functionality!

 

制造商零件编号 A000066
ARDUINO UNO R3 ATMEGA328P BOARD
Arduino
¥190.97
Details
制造商零件编号 NHD-0216BZ-FL-YBW
LCD MOD 32DIG 16X2 TRANS YLW/GRN
Newhaven Display Intl
¥94.26
Details
制造商零件编号 96BB2-006-R
SWITCH KEYPAD 16KEY 0.005A 12V
Grayhill Inc.
¥420.78
Details
制造商零件编号 CF14JT220R
RES 220 OHM 5% 1/4W AXIAL
Stackpole Electronics Inc
¥0.81
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum