Electronics Champ
Published © GPL3+

Decimal to Hexadecimal Converter

This project shows how to convert a Decimal number into a Hexadecimal number using Arduino

BeginnerFull instructions provided3,094
Decimal to Hexadecimal Converter

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
4x4 Keypad
×1
Alphanumeric LCD, 16 x 2
Alphanumeric LCD, 16 x 2
×1
I2C Module
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Tinkercad software is used to design the schematic.

Code

Code

Arduino
Code
/*
 
  This sketch converts a Decimal number into a Hexadecimal number.
  The Decimal number is fed to the Arduino through a 4x4 Keypad.
  A function then converts this Decimal number to its Hexadecimal
  equivalent. These numbers are displayed on a Liquid Crystal Display
  and Serial Monitor.
 
  This program is made by Shreyas for Electronics Champ YouTube Channel.
  Please subscribe to this channel. Thank You.
 
*/
 
// including the libraries
#include <LiquidCrystal_I2C.h>
#include <Keypad.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
Keypad myKeypad = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
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() {
 
  key = myKeypad.getKey();
 
  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.print("Hexadecimal: ");
      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;
 
}

Credits

Electronics Champ

Electronics Champ

3 projects • 9 followers
Projects based on breadboard electronics and Arduino with clear step-by-step instructions, circuit diagrams, schematics, and source code.

Comments