 # Arduino based hexadecimal calculator

It is very easy to calculate the hexadecimal equivalent to a decimal number using dividing by 167 method. But this method is very long and sometimes very confusing when comes to large numbers. here I have a Arduino based decimal to hexadecimal converter. But let’s know more about hexadecimal number system and why it was designed to work with popular intel microprocessors in 60’s.

first we will know how to convert decimal into hex manually and then use Arduino based system to make a calculator project.

### Hexadecimal number system:

Hexadecimal uses digits that more closely resemble our usual base-10 counting system. And through this more variation in data can be represented using the same number of digits. In hexadecimal counting goes from 0 to F, and F represents the decimal number 15. So, by using less number or same number of digit variation more data can be stored.

Hexadecimal is the name of the numbering system that is base 16. This system, therefore, has numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15. That means that two-digit decimal numbers 10, 11, 12, 13, 14, and 15 must be represented by a single numeral to exist in this numbering system A, B, C, D, E, F respectively.

### Converting decimal into hexadecimal:

Divide the digital number by 16 and keep the remainder separated. By following multiple division the remainder data gives the hexadecimal number information.

The remainder is calculated from below to upside and then to the base 16 finally give hexadecimal number representation.

### Components Required:

• Arduino UNO
• 16×2 LCD with I2C
• Custom PCB from JLCPCB
• Battery

### Circuit diagram:

The circuit is made using an input 4×4 keyboard, an LCD display and an Arduino. The Arduino do all the calculation in real time and display the values on screen. Keypad has 16 inputs in which 0-9 are used for data input and C for reset. we are not using other buttons of keyboard for now.

Keyboard works on a matrix system only 8 connections are required to do all the wiring. For the screen we are using I2C interface which required 4 wires, two for the power and two for the data. Whole the system can be powered using a 9v battery.

### PCB Shield:

I also prepared a PCB shield to eliminate the wiring connections. Just plug your hardware to the shield and we are ready to go. The power jack is mounted on top right side, it can be powered using same 9v battery. I used Arduino Nano here for better compatibility of pin headers.

You can try JLCPCB PCB prototyping service for your projects. JLCPCB is the one of leading PCB manufacturer from CHINA. Sign-up now to JLCPCB and get free new user coupons of worth \$54. If you want to use these design then download from here. Or you can also try something different the circuit details with diagram are given above.

### Code:

``````// including the libraries
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

// initialize the variables
const byte ROWS = 4;  //Four rows of Keypad
const byte COLS = 4;  //Four columns of Keypad
char key;
String decimalNum;
long decimalNumber;
String hexNumber;

//Define the symbols on the buttons of the keypad
char Keys[ROWS][COLS] = {

{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}

};

byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};

// create keypad and LCD objects
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {

// start Serial communication
Serial.begin(9600);
Serial.println("Electronics Champ");

// initialize the lcd
lcd.init();
lcd.begin(16, 2);
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Electronics");
lcd.setCursor(0, 1);
lcd.print("Champ");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D: ");
lcd.setCursor(3, 0);
lcd.print(0);
lcd.setCursor(0, 1);
lcd.print("H: ");
lcd.setCursor(3, 1);
lcd.print(0);

}

void loop() {

if (key) {

if (key != 'A' and key != 'B' and key != 'C' and key != 'D' and key != '*' and key != '#') {

decimalNum = decimalNum + String(key);
decimalNumber = decimalNum.toInt();

//Converts Decimal to Hexadecimal
hexNumber = convertDecimalToHex(decimalNumber);

//Prints the Decimal and the Hexadecimal numbers on the Display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D: ");
lcd.setCursor(3, 0);
lcd.print(decimalNumber);
lcd.setCursor(0, 1);
lcd.print("H: ");
lcd.setCursor(3, 1);
lcd.print(hexNumber);

//Prints the Decimal number on the Serial Monitor
Serial.print("Decimal: ");
Serial.print(decimalNumber);
Serial.print("      ");

//Prints the Hexadecimal number on the Serial Monitor
Serial.println(hexNumber);

}

}

//Clears the numbers
if (key == 'C') {

decimalNum = "";
decimalNumber = 0;
hexNumber = "0";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D: ");
lcd.setCursor(3, 0);
lcd.print(decimalNumber);
lcd.setCursor(0, 1);
lcd.print("H: ");
lcd.setCursor(3, 1);
lcd.print(hexNumber);

}

}

//This function converts a Decimal number to a Hexadecimal number
String convertDecimalToHex(long n) {

String hexNum;
long remainder;

while (n > 0) {

remainder = n % 16;
n = n / 16;

if (remainder < 10) {
hexNum = String(remainder) + hexNum;
}

else {

switch (remainder) {

case 10:
hexNum = "A" + hexNum;
break;

case 11:
hexNum = "B" + hexNum;
break;

case 12:
hexNum = "C" + hexNum;
break;

case 13:
hexNum = "D" + hexNum;
break;

case 14:
hexNum = "E" + hexNum;
break;

case 15:
hexNum = "F" + hexNum;
break;

}

}

}

return hexNum;

}``````

### Working of code:

I will only explain the working of decimal to hexadecimal number conversion here. In the first section of the code setup of inputs /outputs and pin declaration is done. Which are properly labeled in the comments inside main sketch.

1) If key pressed is not from 0-9 then the string value of key is stored in decimalNum variable and then value is converted into integer and stored in decimalNumber variable. Here decimalNum is string and decimalNumber is long integer variable.

``````if (key) {

if (key != 'A' and key != 'B' and key != 'C' and key != 'D' and key != '*' and key != '#') {

decimalNum = decimalNum + String(key);
decimalNumber = decimalNum.toInt();``````

2) Then the program run an external function named convertDecimalToHex which convert the integral decimal value to hexadecimal.

``hexNumber = convertDecimalToHex(decimalNumber);``

3) And the function says: if the integer value is greater than 0 the modulo of that number is stored as remainder and n stores the value divided by 16. If remainder is less than 10 then the value of hexadecimal is stored as remainder followed by hexnum string.

``````String convertDecimalToHex(long n) {

String hexNum;
long remainder;

while (n > 0) {

remainder = n % 16;
n = n / 16;

if (remainder < 10) {
hexNum = String(remainder) + hexNum;
}``````

4) whenever the value exceed from 9, it will automatically switch between these cases. 10 is A and then goes upto F which is 15.

``````switch (remainder) {

case 10:
hexNum = "A" + hexNum;
break;

case 11:
hexNum = "B" + hexNum;
break;

case 12:
hexNum = "C" + hexNum;
break;

case 13:
hexNum = "D" + hexNum;
break;

case 14:
hexNum = "E" + hexNum;
break;

case 15:
hexNum = "F" + hexNum;
break;``````

5) whenever the C is pressed from the keyboard, it will reset all the data and changed it to zero.

``````if (key == 'C') {

decimalNum = "";
decimalNumber = 0;
hexNumber = "0";``````

### Project Working:

The overall code is working okay with Arduino, the output in hexadecimal is given with no delay. By making this type of calculator might give you knowledge to work with hexadecimal system. I am very thankful to JLCPCB for sponsoring this article.