Arduino Based Calculator Using Keyboard and LCD

by Lisleapex Blog in Circuits > Arduino

176 Views, 0 Favorites, 0 Comments

Arduino Based Calculator Using Keyboard and LCD

Arduino based calculator using keyboard and LCD.png

A simple Arduino based calculator can be easily implemented using Arduino development board, LCD and keyboard to solve mathematical calculations using keyboard and LCD. Simple mathematical calculations such as addition, subtraction, multiplication and division can be easily done using this project.


1602 LCD is able to solve mathematical calculations within 16 bits without delay and fast processing. The Arduino program is also very simple, almost like a simple C program, based only on simple formulas of addition, subtraction, multiplication and division. So, let's implement this simple little project.

Supplies

1602 LCD screen.png

● Arduino Uno development board


● 1602 LCD screen


● 4 * 4 film matrix keyboard


● Reset switch


Potentiometer


● 9V battery


● Breadboard and connecting wires

Circuit Diagram

Circuit Diagram.png

The circuit diagram is very simple. You can assemble the circuit directly on the Arduino development board or by making a PCB.

Source Code

The program is based on the Arduino UNO development board and is completed using the Arduino language. You need to add an additional Arduino library file for the keyboard, such as #include <Keypad.h>. Therefore, download this file from the adafruit library. After assembly, just upload the source code to the Arduino Uno development board and you can enjoy the Arduino-based calculator.

Coding

  1. #include <Keypad.h>
  2. #include <LiquidCrystal.h>

  3. // 1. LCD Pins
  4. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

  5. // 2. Keypad Pins
  6. const byte Rows = 4;
  7. const byte Cols = 4;
  8. char keys[Rows][Cols] =
  9. {
  10. {'1', '2', '3', '+'},
  11. {'4', '5', '6', '-'},
  12. {'7', '8', '9', '*'},
  13. {'C', '0', '=', '/'}
  14. };
  15. byte rowPins[Rows] = {A2, A3, A4, A5};
  16. byte colPins[Cols] = {2, 3, 4, 5};
  17. Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, Rows, Cols);

  18. // 3. Dot Button
  19. int dot = A0;
  20. int dotFlag = 0;
  21. int dotButton = 0;

  22. // 4.  Calculator Operators
  23. float num1, num2, fraction;
  24. float total;
  25. char operation, button;

  26. // 5. Loading Setup
  27. char input[16];
  28. int n = 1750;

  29. void setup()
  30. {
  31. // Initialize dot button as input to  Arduino
  32. pinMode(dot, INPUT);

  33. // Initialize LCD Size
  34. lcd.begin(16, 2);

  35. // LCD Loading Setup Begin
  36. lcd.clear();
  37. lcd.setCursor(3, 0);
  38. lcd.print("LOADING...");
  39. for (int i = 0; i < 16; i++)
  40. {
  41. lcd.setCursor(i, 1);
  42. lcd.write(255);
  43. delay(50);
  44. }
  45. lcd.clear();
  46. lcd.setCursor(1, 0);
  47. lcd.print("Simple  Arduino");
  48. lcd.setCursor(3, 1);
  49. lcd.print(" Calculator");
  50. delay(n);
  51. lcd.clear();
  52. lcd.setCursor(0, 0);
  53. lcd.print("Designed By");
  54. lcd.setCursor(2, 1);
  55. lcd.print("Alex Newton");
  56. delay(n);
  57. lcd.clear();
  58. lcd.setCursor(1, 0);
  59. lcd.print("Alex Newton");
  60. lcd.setCursor(2, 1);
  61. lcd.print("how2electronics.com");
  62. delay(n);
  63. lcd.clear();
  64. lcd.setCursor(1, 0);
  65. lcd.print("Alex Newton");
  66. lcd.setCursor(2, 1);
  67. lcd.print("how2electronics.com");
  68. delay(n);
  69. lcd.clear();
  70. // LCD Loading Setup End
  71. }
  72. void loop()
  73. {
  74. // First while loop for num1.
  75. while (1)
  76. {
  77. dotButton = digitalRead(dot);
  78. button = customKeypad.getKey();
  79. if (button == 'C')
  80. {
  81. dotFlag = 0;
  82. num1 = 0;
  83. num2 = 0;
  84. fraction = 0;
  85. total = 0;
  86. operation = 0;
  87. lcd.clear();
  88. }
  89. else if (dotButton == LOW)
  90. {
  91. dotFlag = 1;
  92. }
  93. else if (button >= '0' && button <= '9')
  94. {
  95. if (dotFlag == 0)
  96. {
  97. num1 = num1 * 10 + (button - '0');
  98. lcd.setCursor(0, 0);
  99. lcd.print(num1);
  100. }
  101. else if (dotFlag == 1)
  102. {
  103. fraction = (button - '0');
  104. num1 = num1 + (fraction / 10);
  105. lcd.setCursor(0, 0);
  106. lcd.print(num1);
  107. dotFlag++;
  108. }
  109. else if (dotFlag == 2)
  110. {
  111. fraction = (button - '0');
  112. num1 = num1 + (fraction / 100);
  113. lcd.setCursor(0, 0);
  114. lcd.print(num1);
  115. dotFlag++;
  116. }
  117. }
  118. else if (button == '-' || button == '+' || button == '*' || button == '/')
  119. {
  120. operation = button;
  121. dotFlag = 0;
  122. lcd.setCursor(0, 1);
  123. lcd.print(operation);
  124. break;
  125. }
  126. }
  127. // Second while loop for num2.
  128. while (1)
  129. {
  130. dotButton = digitalRead(dot);
  131. button = customKeypad.getKey();
  132. if (button == 'C')
  133. {
  134. dotFlag = 0;
  135. num1 = 0;
  136. num2 = 0;
  137. fraction = 0;
  138. total = 0;
  139. operation = 0;
  140. lcd.clear();
  141. break;
  142. }
  143. else if (dotButton == LOW)
  144. {
  145. dotFlag = 1;
  146. }
  147. else if (button >= '0' && button <= '9')
  148. {
  149. if (dotFlag == 0)
  150. {
  151. num2 = num2 * 10 + (button - '0');
  152. lcd.setCursor(1, 1);
  153. lcd.print(num2);
  154. }
  155. else if (dotFlag == 1)
  156. {
  157. fraction = (button - '0');
  158. num2 = num2 + (fraction / 10);
  159. lcd.setCursor(1, 1);
  160. lcd.print(num2);
  161. dotFlag++;
  162. }
  163. else if (dotFlag == 2)
  164. {
  165. fraction = (button - '0');
  166. num2 = num2 + (fraction / 100);
  167. lcd.setCursor(1, 1);
  168. lcd.print(num2);
  169. dotFlag++;
  170. }
  171. }
  172. if (button == '=')
  173. {
  174. domath();
  175. break;
  176. }
  177. }
  178. // Third while loop for ensuring C button is executed after while loop 2.
  179. while (1)
  180. {
  181. button = customKeypad.getKey();
  182. if (button == 'C')
  183. {
  184. dotFlag = 0;
  185. num1 = 0;
  186. num2 = 0;
  187. fraction = 0;
  188. total = 0;
  189. operation = 0;
  190. lcd.clear();
  191. break;
  192. }
  193. }
  194. }
  195. void domath()
  196. {
  197. switch (operation)
  198. {
  199. case '+':
  200. total = num1 + num2;
  201. break;
  202. case '-':
  203. total = num1 - num2;
  204. break;
  205. case '/':
  206. total = num1 / num2;
  207. break;
  208. case '*':
  209. total = num1 * num2;
  210. break;
  211. }
  212. lcd.print('=');
  213. if (operation == '/' && num2 == 0)
  214. {
  215. lcd.print("ERROR 0 DIV");
  216. }
  217. else
  218. {
  219. lcd.print(total);
  220. }
  221. }

Work It Out Finally

Work It Out Finally.png

Make the connections as per the circuit diagram and upload the code below. If it shows an error, make sure that you have added the library as per the instructions given above. You can also try simulation to check if the issue is with your hardware. If everything is done as expected, then your hardware will look like this with the LCD showing this