DIY Arduino Calculator Using OLED Display

Introduction: DIY Arduino Calculator Using OLED Display

About: i make stuff, like to take projects, ideas, motivation to next level. if you love D.I.Y. and electronics, click on the link in description to get amazed

in one of my previous Instructable tutorial, we learnt how to interface an OLED with Arduino and basics of it, now its time to step up the game and make some project based on OLED, so in this tutorial, we will learn how to make a simple yet slinky calculator based on Arduino!

Supplies:

A.Components

  1. OLED Display
  2. Arduino
  3. Arduino Keypad
  4. 3.7v, 200mAH Lithium Polymer Battery
  5. Switch

B. Tools

  1. Soldering Iron
  2. hot glue gun
  3. Soldering Wire
  4. Wire Stripper

C. Software

1. Arduino IDE

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Watch the Video!

Step 2: Choice of Material!

to make a calculator, we will need some kind of Processor, for which we can use Arduino, since this is a small project and needs large memory space for complicated code, we will use Arduino NANO.

for taking input number, we can use a 4x4 keypad with membrane switches, which has 4 rows and 4 columns, if we open up this sticker from behind, the flexible membrane PCB is visible, which has connections going from ROWS and COLUMNS to connecting pins via flexible PCB. When any membrane switch is pressed, the rows and column pins process the data as a Matrix and identify the location of key pressed.(Remember, left side pins are row pins and right side pins are column pins.)

for display we can use either OLED or LCD display, but I would like to use OLED, since the resolution of OLED is far better, we can have longer strings of numbers to operate, if we choose OLED.

Step 3: Complete the Connections

since we discussed the choice of material, connections should be very straight forward, so follow the following steps to complete the connections:

  1. connect Keypad to any digital pins on Arduino Nano ( preferably D2 to D9).
  2. connect OLEDs SCL to Arduinos A5.
  3. connect OLEDs SDA to Arduinos A4.

  4. connect OLEDs VCC to Arduinos 5v.

  5. connect OLEDs GND to Arduinos GND.

  6. Connect a Switch between Positive of Lithium Polymer Battery.

  7. Connect Lipo to 5V and Gnd pins of Arduino.

and that all for connections!

Step 4: Upload the Code

in this code, we will need the following Libraries.

Steps:

  1. First we included Keypad library,
  2. next we will add SPI wire,Adafruit GFX and Adafruit SSD1306 libraries for OLED.
  3. Then we will initialize the OLED.
  4. Define Rows and Columns,
  5. Define Variables.
  6. Map out the Keypad in form of matrix, where in our A key is replaced by + operator, B with minus, c with multiplier, D with Divisor, hash with equals and finally star with clear.
  7. in byte rowPins and colPins we define the pin out of keypad and initialize the Keypad,
  8. next we have a startup animation Screen set up in this sub-code.
  9. In the loop section, we will use cases for each operator and do the calculation keeping in mind the operators.

you can download the code here!

<p style="margin-left: 20px;">#include <spi.h><keypad.h><br>#include <keypad.h><spi.h>
#include <wire.h><wire.h>
#include <Adafruit_GFX.h><adafruit_gfx.h>
#include <Adafruit_SSD1306.h><adafruit_ssd1306.h></adafruit_ssd1306.h></adafruit_gfx.h></wire.h></spi.h></keypad.h></p><p>#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);</p><p>#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif</p><p>const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad</p><p>//keymap defines the key pressed according to the row and columns just as appears on the keypad
char customKey;
double first = 0;
long second = 0;
double total = 0;
char Operator;
bool equalPressed=false;
bool showFirst = false;</p><p>char keymap[numRows][numCols]= 
{
{'1', '2', '3', '+'}, 
{'4', '5', '6', '-'}, 
{'7', '8', '9', 'x'},
{'C', '0', '=', '/'}
};</p><p>//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3</p><p>//initializes an instance of the Keypad class
Keypad customKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);</p><p>void showSplash() {
  String splashString="Arduino Calculator by";
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.setCursor(64-(splashString.length()*3),0);
  display.print(splashString);
  display.setTextSize(2);
  splashString="Mission";
  display.setCursor(64-(splashString.length()*6),16);
  display.print(splashString);
  display.setTextSize(2);
  splashString="Critical";
  display.setCursor(64-(splashString.length()*6),40);
  display.print(splashString);
  display.display();
  delay(3000);
}</p><p>void setup()
{
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  showSplash();
  display.print("Arduino Calulator By Mission Critical");
  display.setTextSize(2);
  display.clearDisplay();
  display.display();
}</p><p>void loop(){
customKey = customKeypad.getKey();
  switch(customKey) 
  {
  case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
    showFirst=true;
    first = first * 10 + (customKey - '0');
    showDisplay();  
    break;</p><p>  case '+':
    showFirst=true;
    Operator='+';
    showDisplay();
    second = SecondNumber(); // get the collected the second number
    total = first + second;
    showDisplay(); 
    first = total,  // reset values back to zero for next use
    second = 0;   
    break;</p><p>  case '-':
    showFirst=true;
    Operator='-';
    showDisplay();
    second = SecondNumber();
    total = first - second;
    showDisplay();
    first = total, second = 0;
    break;</p><p>  case 'x':
    showFirst=true;
    Operator='x';
    showDisplay();
    second = SecondNumber();
    total = first * second;
    showDisplay();
    first = total, second = 0;
    break;</p><p>  case '/':
    showFirst=true;
    Operator='/';
    showDisplay();
    second = SecondNumber();
    second == 0 ? display.print("Invalid") : total = (float)first / (float)second;
    showDisplay();
    first = total, second = 0;    
    break;</p><p>  case 'C':
    total = 0;
    first=0;
    second=0;
    Operator='\0';
    showFirst=false;
    equalPressed=false;
    display.clearDisplay();
    display.display();    
    break;
  }
}</p><p>void showDisplay()
{
  display.clearDisplay();
  display.setCursor(110,0);
  display.println(Operator);
  if (showFirst) {
    Serial.print(first);
    display.println(first);
  } else
  {
    display.println("");
  }
  if (second>0) {
    display.println(second);
  } else
  {
    display.println("");
  }
  if (equalPressed) {
    display.println(total);
    equalPressed=false;
  }
  display.display();
}
long SecondNumber()
{
  while( 1 )
  {
    customKey = customKeypad.getKey();
    if(customKey >= '0' && customKey <= '9')
    {
      second = second * 10 + (customKey - '0');
      showDisplay();
    }</p><p>    if(customKey == '=') {
      equalPressed=true;
      break;  //return second;
    }
  }
 return second; 
}</p>

Step 5: Test the Project

Once this code is uploaded to Arduino, we can see a startup animation screen saying Arduino Calculator by Mission Critical, followed by blank screen, which we will use for operations, So we can now add numbers, subtract act them, multiply them, divide them, we can also use obtained answer and perform operations on it!

Step 6: Create an Enclosure

But let’s leave this project hanging and make a simple enclosure for it. I had this small box with me, which I used as base for this enclosure. I want this project to be portable, so I used a Lithium Ion Battery to power Arduino, which I soldered along with a little switch to Arduino’s 5v pin and ground pin, and stuffed everything inside, finally used a little hot glue to place OLED and Keypad at required location.

Step 7: Thank You!

After this coding and wiring mess, the end result looks absolutely charming and of course functional.

So I hope you guys enjoyed this simple Arduino project tutorial, and I hope you guys learnt something from this tutorial, and if you liked it, make sure to check out our other work on youtube, you can also support us for following Mission Critical on social media or becoming our patreon.

Patreon - https://www.patreon.com/missioncritical
Twitter @ akshaymomaya1 Instagram @officialmissioncritical Facebook https://www.facebook.com/officialmissioncritical/

Reuse Contest

Participated in the
Reuse Contest

Be the First to Share

    Recommendations

    • Trash to Treasure Contest

      Trash to Treasure Contest
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    Discussions