Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Wednesday!Stream Hackster Holidays, Ep. 4 on Wednesday!
Electronics Champ
Published © GPL3+

Binary to Decimal Converter using Arduino and OLED Display

This project shows how to convert a Binary number (Up to 8 Bits) into a Decimal number using Arduino.

BeginnerShowcase (no instructions)8,922
Binary to Decimal Converter using Arduino and OLED Display

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
8x DIP Switch
×1
Resistor 220 ohm
Resistor 220 ohm
×8
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
OLED Display (I2C)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Code

Code

Arduino
/*

  This sketch converts an 8-Bit Binary number into a Decimal number.
  The Binary number is fed to the Arduino through an 8x DIP Switch.
  A function then converts this Binary number to its Decimal
  equivalent. These numbers are displayed on an OLED Display and Serial   Monitor.

  This program is made by Shreyas for Electronics Champ YouTube Channel.
  Please subscribe to this channel. Thank You.

*/

//Include the libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//Initialize the variables
int bitVal;
String stringBit;
String stringBinary;
long binaryNumber;
int decimalNumber;

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT);

//Prints the EChamp logo on OLED Display
static const uint8_t PROGMEM logo[] = {

  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x07, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x01, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x07, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x1f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x7f, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x03, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x07, 0xfc, 0x11, 0xf1, 0x0f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  0x0f, 0xfc, 0xf1, 0xf1, 0xe7, 0xfe, 0x00, 0x7e, 0x60, 0x00, 0x30, 0x00, 0x00, 0x30, 0x00, 0x00,
  0x1f, 0xfd, 0xfb, 0xf3, 0xe7, 0xff, 0x00, 0x60, 0x60, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x1f, 0xfd, 0xff, 0xff, 0xe7, 0xff, 0x00, 0x60, 0x63, 0x87, 0xbb, 0xc7, 0x1f, 0x33, 0xcf, 0x00,
  0x3f, 0xfd, 0xff, 0xff, 0xe7, 0xff, 0x80, 0x60, 0x66, 0xcd, 0xb3, 0x4d, 0x9b, 0x36, 0xdb, 0x00,
  0x3f, 0xfd, 0xf9, 0xf3, 0xe7, 0xff, 0x80, 0x7e, 0x64, 0x48, 0x33, 0x08, 0x99, 0x34, 0x18, 0x00,
  0x7e, 0x40, 0x11, 0xf1, 0x20, 0x1f, 0xc0, 0x60, 0x67, 0xc8, 0x33, 0x08, 0x99, 0x34, 0x1f, 0x00,
  0x7e, 0x7d, 0xf9, 0xf3, 0xe7, 0xdf, 0xc0, 0x60, 0x64, 0x08, 0x33, 0x08, 0x99, 0x34, 0x01, 0x00,
  0x7e, 0x7d, 0xfb, 0xff, 0xe7, 0xdf, 0xc0, 0x60, 0x66, 0xcd, 0xb3, 0x0d, 0x99, 0x36, 0xdb, 0x00,
  0x7e, 0x7d, 0xff, 0xff, 0xe7, 0xdf, 0xc0, 0x7e, 0x63, 0xc7, 0xbb, 0x07, 0x19, 0x33, 0xde, 0x00,
  0x7e, 0x7d, 0xf9, 0xf3, 0xe7, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7c, 0xf1, 0xf1, 0x67, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7e, 0x11, 0xf1, 0x0f, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xfb, 0xf3, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x3c, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7f, 0xff, 0xff, 0x7f, 0xdf, 0xe0, 0x7e, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7f, 0x27, 0xf8, 0x9f, 0xdf, 0xc0, 0x62, 0x7c, 0x71, 0xf7, 0x3e, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7e, 0xf1, 0xe1, 0xcf, 0xdf, 0xc0, 0x60, 0x6c, 0x59, 0xbb, 0x36, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7e, 0xf8, 0xe3, 0xef, 0xdf, 0xc0, 0x60, 0x64, 0x09, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00,
  0x3e, 0x7c, 0xf9, 0xf3, 0xef, 0xdf, 0x80, 0x60, 0x64, 0xf9, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00,
  0x3f, 0x01, 0xff, 0xff, 0xe0, 0x1f, 0x80, 0x62, 0x64, 0x89, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00,
  0x1f, 0xfc, 0xf9, 0xf3, 0xef, 0xff, 0x80, 0x7e, 0x64, 0x99, 0x91, 0x36, 0x00, 0x00, 0x00, 0x00,
  0x1f, 0xfe, 0xf8, 0xe3, 0xef, 0xff, 0x00, 0x3c, 0x64, 0xf9, 0x91, 0x3e, 0x00, 0x00, 0x00, 0x00,
  0x0f, 0xfe, 0x71, 0xf1, 0xdf, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
  0x0f, 0xff, 0x01, 0xfc, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
  0x07, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
  0x03, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x1f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x07, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x7f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

};

void setup() {

  Serial.begin(9600);

  //Sets pin 3 to pin 10 as input
  for (int x = 3; x < 11; x++) {

    pinMode(x, INPUT);

  }

  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.clearDisplay();

  //Prints the logo on OLED Display
  oled.drawBitmap(0, 0, logo, 128, 64, 1);
  oled.display();
  delay(3000);
  oled.clearDisplay();

}

void loop() {

  //Reads the Binary number from the DIP Switch
  for (int x = 3; x < 11; x++) {

    bitVal = digitalRead(x);
    stringBit = String(bitVal);
    stringBinary = stringBinary + stringBit;
    binaryNumber = stringBinary.toInt();

  }

  //Function to convert Binary to Decimal
  decimalNumber = convertBinaryToDecimal(binaryNumber);

  //Prints the Binary number on the Serial Monitor
  Serial.print("Binary: ");
  Serial.print(stringBinary);
  Serial.print("      ");

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

  //Prints the Binary number on the Serial Monitor
  oled.clearDisplay();
  oled.setTextSize(2);
  oled.setTextColor(WHITE);
  oled.setCursor(5, 10);
  oled.print("B:");
  oled.println(stringBinary);

  //Prints the Decimal number on the Serial Monitor
  oled.setTextSize(2);
  oled.setTextColor(WHITE);
  oled.setCursor(5, 40);
  oled.print("D:");
  oled.println(decimalNumber);
  oled.display();

  //Resets all the variables
  binaryNumber = 0;
  bitVal = 0;
  stringBit = "";
  stringBinary = "";

}

//Function to convert Binary to Decimal
long convertBinaryToDecimal(long binary) {

  long number = binary;
  long decimalVal = 0;
  long baseVal = 1;
  long tempVal = number;
  long previousDigit;

  while (tempVal) {

    //Converts Binary to Decimal
    previousDigit = tempVal % 10;
    tempVal = tempVal / 10;
    decimalVal += previousDigit * baseVal;
    baseVal = baseVal * 2;

  }

  //Returns the Decimal number
  return decimalVal;

}

Credits

Electronics Champ

Electronics Champ

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

Comments