Beginner15 min50 XP base

Binary, Hex & Number Systems

Learn how digital circuits count. Convert between decimal, binary, and hexadecimal, then light up a 4-bit binary counter on LEDs.

1Why Digital Circuits Use Binary

Every digital circuit, from a single logic gate to the processor on your Arduino, works with just two voltage levels: HIGH (near the supply voltage) and LOW (near 0V). We write these as 1 and 0. A single 1-or-0 value is called a bit.

Two levels are used because they are easy to tell apart reliably, even with electrical noise. A circuit only has to decide whether a voltage is 'high enough' or 'low enough' rather than measure it precisely.

  • Bit: a single binary digit, 0 or 1
  • Nibble: 4 bits (one hexadecimal digit)
  • Byte: 8 bits (the size of a byte variable on Arduino)

2Counting in Binary

Decimal is base 10: each position is worth 10 times the one to its right (1, 10, 100…). Binary is base 2: each position is worth 2 times the one to its right (1, 2, 4, 8, 16…). To convert binary to decimal, add up the place values wherever there is a 1.

Place value:   8   4   2   1
Binary 1011:   1   0   1   1
               8 + 0 + 2 + 1 = 11 (decimal)

To go the other way, repeatedly divide by 2 and write down the remainders. Reading the remainders from bottom to top gives the binary number.

13 / 2 = 6  remainder 1   <- least significant bit
 6 / 2 = 3  remainder 0
 3 / 2 = 1  remainder 1
 1 / 2 = 0  remainder 1   <- most significant bit

13 (decimal) = 1101 (binary)

Note: With n bits you can represent 2ⁿ different values: 0 through 2ⁿ − 1. Four bits count 0–15, and eight bits count 0–255.

3Hexadecimal: Binary's Shorthand

Long binary numbers are hard to read. Hexadecimal (base 16) fixes this: each hex digit stands for exactly 4 bits, so you can convert by splitting the binary number into groups of four. Hex uses the digits 0–9 and then A–F for the values 10–15.

Binary   Hex   Decimal
0000     0     0
0101     5     5
1001     9     9
1010     A     10
1100     C     12
1111     F     15

11010110 -> 1101 0110 -> D 6 -> 0xD6 (= 214 decimal)

In Arduino code you can write numbers in any base. 0b prefixes a binary literal and 0x prefixes a hex literal, so 0b11111111, 0xFF, and 255 are all the same value.

4Build a 4-Bit Binary Counter

1×Arduino Uno
4×LEDs
4×220Ω resistors
1×Breadboard + jumper wires
  • Connect pins 2, 3, 4, and 5 each through a 220Ω resistor to an LED's long leg
  • Connect all LED short legs to GND
  • Line the LEDs up with pin 5 on the left, so they read like a binary number (bit 3 … bit 0)
const int ledPins[4] = {2, 3, 4, 5};  // bit 0 .. bit 3

void setup() {
  for (int i = 0; i < 4; i++) pinMode(ledPins[i], OUTPUT);
  Serial.begin(9600);
}

void loop() {
  for (int n = 0; n < 16; n++) {
    for (int bit = 0; bit < 4; bit++) {
      digitalWrite(ledPins[bit], bitRead(n, bit));  // 1 = LED on
    }
    Serial.print(n);
    Serial.print(" = 0b");
    Serial.print(n, BIN);
    Serial.print(" = 0x");
    Serial.println(n, HEX);
    delay(1000);
  }
}

bitRead(n, bit) returns the value (0 or 1) of one bit of n, where bit 0 is the rightmost. Open the Serial Monitor to see each number printed in all three bases while the LEDs count.

Note: Serial.print(n, BIN) does not print leading zeros. 5 prints as 101, not 0101.

5Challenges

  • Extend the counter to 8 LEDs and count 0–255.
  • Make the counter count down from 15 to 0 instead of up.
  • Read a number typed into the Serial Monitor and display it on the LEDs in binary.

Skill Check

8 questions · Up to 180 XP

Q1.What is binary 1011 in decimal?

Q2.How many different values can 8 bits represent?

Q3.What is 0xFF in decimal?

Q4.How many bits does one hexadecimal digit represent?

Q5.What is binary 11010110 in hexadecimal?

Q6.What is decimal 13 in binary?

Q7.What does bitRead(6, 1) return?

Q8.Why do engineers often write binary values in hex?

Sign in to earn XP for your answers.