The Direct Answer: Decimal 13 to Binary
The decimal number 13 is written as 1101 in binary code. In the base-2 numeral system, each digit represents a power of two, reading from right to left (1s, 2s, 4s, 8s). For 13, the math breaks down as: (1 × 8) + (1 × 4) + (0 × 2) + (1 × 1) = 13.
While converting numbers on paper is straightforward, applying binary states to physical hardware is where embedded engineering gets interesting. Microcontrollers like the ESP32 do not natively "see" the number 13; they see a 32-bit register where the least significant bits are set to ...00001101. In this guide, we will bridge the gap between abstract binary theory and physical hardware by building a 4-bit binary visualizer that isolates and acts upon the specific state of 13.
0b. You can define 13 directly in your code as 0b1101 or B1101 to make your intent explicitly clear to anyone reading your firmware.
Project Spec Sheet & Hardware Requirements
This build uses a 4-LED array to visualize the 4 least significant bits (LSBs) of an incoming serial command. When the parsed integer matches 13 (1101), a 5V relay triggers to simulate a physical lock or high-voltage switch activation.
| Component | Exact Variant / Specification | Quantity | Estimated Cost (2026) |
|---|---|---|---|
| Microcontroller | ESP32-WROOM-32 DevKit V1 (30-pin) | 1 | $6.50 |
| Indicator LEDs | 5mm Diffused Red (2.0V Vf, 20mA max) | 4 | $0.50 |
| Current Limiting Resistors | 220Ω 1/4W Carbon Film (Red-Red-Brown-Gold) | 4 | $0.20 |
| Relay Module | SRD-05VDC-SL-C (Active Low, Optocoupler isolated) | 1 | $1.80 |
| Prototyping | 830-point solderless breadboard & 22 AWG jumper wires | 1 kit | $8.00 |
Pin Mapping & Step-by-Step Wiring
Proper GPIO selection on the ESP32 is critical. We intentionally avoid GPIO 12 (a strapping pin that dictates flash voltage) and GPIO 34-39 (input-only pins) for our outputs. Below is the exact pin mapping for this build.
| Function | ESP32 GPIO | Component Connection | Notes |
|---|---|---|---|
| Bit 3 (MSB / 8s) | GPIO 25 | 220Ω Resistor → LED Anode | LED Cathode to GND |
| Bit 2 (4s) | GPIO 26 | 220Ω Resistor → LED Anode | LED Cathode to GND |
| Bit 1 (2s) | GPIO 27 | 220Ω Resistor → LED Anode | LED Cathode to GND |
| Bit 0 (LSB / 1s) | GPIO 14 | 220Ω Resistor → LED Anode | LED Cathode to GND |
| Relay Control | GPIO 33 | Relay IN pin | Active LOW trigger |
| Relay Power | VIN (5V) | Relay VCC | Do not use 3.3V pin |
Wiring Steps:
- De-energize the board: Ensure the ESP32 is unplugged from your PC before wiring the relay module to prevent accidental short circuits.
- Wire the LED array: Connect GPIO 25, 26, 27, and 14 to the breadboard. Place a 220Ω resistor in series with each LED anode. Connect all LED cathodes to the common GND rail.
- Wire the Relay: Connect the Relay VCC to the ESP32
VINpin (which outputs 5V from the USB regulator). Connect Relay GND to ESP32 GND. Connect Relay IN to GPIO 33. - Verify connections: Use a multimeter in continuity mode to verify no shorts exist between the 5V relay line and the 3.3V logic lines.
Complete ESP32 Code: Bitwise Extraction & Error Handling
The following C++ code targets the ESP32 DevKit V1 using the Arduino IDE (ESP32 Core v3.x). It listens for serial input, parses the integer, extracts the individual bits using bitwise shift (>>) and AND (&) operators, and triggers the relay if the exact value of 13 is received.
#include <Arduino.h>
// --- PIN DEFINITIONS ---
const uint8_t PIN_BIT3 = 25; // 8s place
const uint8_t PIN_BIT2 = 26; // 4s place
const uint8_t PIN_BIT1 = 27; // 2s place
const uint8_t PIN_BIT0 = 14; // 1s place
const uint8_t PIN_RELAY = 33; // Active LOW relay trigger
const uint8_t LED_PINS[] = {PIN_BIT3, PIN_BIT2, PIN_BIT1, PIN_BIT0};
const uint8_t TARGET_VALUE = 13; // 1101 in binary
void setup() {
Serial.begin(115200);
delay(1000); // Allow serial monitor to connect
// Initialize GPIOs
for (int i = 0; i < 4; i++) {
pinMode(LED_PINS[i], OUTPUT);
digitalWrite(LED_PINS[i], LOW);
}
pinMode(PIN_RELAY, OUTPUT);
digitalWrite(PIN_RELAY, HIGH); // HIGH = OFF for active-low relays
Serial.println("[SYS] ESP32 Binary Visualizer Initialized.");
Serial.println("[SYS] Enter a number (0-15) to visualize in binary.");
}
void loop() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
input.trim();
// Error Handling: Validate input is a number
bool isNumeric = true;
for (unsigned int i = 0; i < input.length(); i++) {
if (!isDigit(input[i])) {
isNumeric = false;
break;
}
}
if (!isNumeric || input.length() == 0) {
// Exact error string for debugging malformed UART payloads
Serial.println("[ERR] UART_PARSE_FAIL: Expected integer 0-255, received malformed payload.");
return;
}
int value = input.toInt();
if (value > 15) {
Serial.println("[WARN] Value exceeds 4-bit limit (15). Truncating to lower 4 bits.");
value = value & 0x0F; // Bitwise mask to keep only lowest 4 bits
}
// Bitwise extraction and LED control
for (int i = 0; i < 4; i++) {
// Shift right by (3-i) and mask with 1 to isolate the bit
uint8_t bitState = (value >> (3 - i)) & 1;
digitalWrite(LED_PINS[i], bitState);
}
Serial.printf("[OUT] Decimal: %d | Binary: %d%d%d%d\n",
value,
(value >> 3) & 1,
(value >> 2) & 1,
(value >> 1) & 1,
value & 1);
// Trigger relay if target matches 13
if (value == TARGET_VALUE) {
Serial.println("[ACT] Target 13 (1101) detected! Engaging relay.");
digitalWrite(PIN_RELAY, LOW); // Trigger active-low relay
delay(2000); // Hold relay for 2 seconds
digitalWrite(PIN_RELAY, HIGH); // Release relay
Serial.println("[ACT] Relay disengaged.");
}
}
}
Debugging: First Three Things to Check When It Fails
If your LEDs remain dark or the ESP32 throws an exception upon boot, do not immediately rewrite the code. Hardware and configuration mismatches cause 90% of embedded failures. Check these three items first:
- Check for GPIO Strapping Pin Conflicts:
If you accidentally wired the relay to GPIO 12 instead of GPIO 33, the ESP32 may fail to boot or continuously reset. GPIO 12 is a strapping pin for the VDD_SDIO voltage. If pulled high by the relay module's internal optocoupler during boot, the ESP32 will attempt to run the flash at 1.8V instead of 3.3V, resulting in a brownout. Fix: Move the relay signal wire to GPIO 33 and update the code definition. - Verify Serial Monitor Baud Rate & Line Endings:
If you type "13" and the serial monitor spits out garbage characters or the code throws the[ERR] UART_PARSE_FAILerror constantly, your baud rate is mismatched. The code is hardcoded to115200. Furthermore, ensure your Serial Monitor dropdown is set to "Both NL & CR" or "Newline". If set to "No line ending", thereadStringUntil('\n')function will timeout and fail to parse. - Measure Relay VCC Voltage Under Load:
The SRD-05VDC-SL-C relay coil requires roughly 70mA to pull in the mechanical contact. If you wired the relay VCC to the ESP32's3V3pin instead ofVIN, the AMS1117 voltage regulator will overheat, drop voltage, and trigger aGuru Meditation Error: Core 1 panic'ed (Brownout detector was triggered). Fix: Always power 5V relay modules from the VIN pin or an external 5V supply.
Scaling the Build: Extensions & Simplifications
Once you have verified that 13 in binary code correctly illuminates the 1101 pattern and triggers the relay, you can adapt this circuit for broader applications.
How to Simplify (Shift Registers):
Using four individual GPIO pins for a 4-bit display is inefficient on pin-constrained boards. To simplify the wiring, replace the four direct LED connections with a 74HC595 8-bit shift register. This reduces the hardware requirement to just three ESP32 pins (Data, Clock, Latch) while allowing you to display numbers up to 255 (8-bit binary). You would replace the bitwise digitalWrite loop with the shiftOut() function native to the Arduino core.
How to Extend (Hex Keypad Input):
Instead of relying on the Serial Monitor, extend the build by wiring a 4x4 matrix membrane keypad via an I2C PCF8574 expander. Map the keys '0' through '9' and 'A' through 'F' to allow hexadecimal input. This turns the visualizer into a standalone digital lock where entering 'D' (Hex for 13) triggers the physical relay without needing a PC connection.
FAQ: Mastering 13 in Binary Code
What is 13 in binary code?
13 in decimal is exactly 1101 in standard binary code. In computing, it is often padded to a full byte (8 bits) and written as 00001101, or represented in hexadecimal as 0x0D. In C/C++ programming for microcontrollers, you can write it directly as the binary literal 0b1101.
How do you convert 13 to binary code step-by-step?
Use the division-by-2 method or the subtraction method. For the subtraction method, list the powers of two: 16, 8, 4, 2, 1.
1. Does 16 fit into 13? No (0).
2. Does 8 fit into 13? Yes (1). Remainder is 5.
3. Does 4 fit into 5? Yes (1). Remainder is 1.
4. Does 2 fit into 1? No (0).
5. Does 1 fit into 1? Yes (1). Remainder is 0.
Reading the results from left to right gives you 1101.
Why is 13 in binary code written as 1101 and not 0111?
This confusion stems from endianness and reading direction. In standard mathematical notation and typical microcontroller registers, the Most Significant Bit (MSB) is on the far left, and the Least Significant Bit (LSB) is on the far right. Therefore, the 8s place is on the left. If you incorrectly read the bit positions from left-to-right as 1, 2, 4, 8, you would calculate 0111 as 14. Always anchor your LSB (the 1s place) to the far right when reading standard binary strings.
How to use 13 in binary code for ESP32 bitwise masking?
Bitwise masking allows you to isolate or force specific bits within a larger register without altering the rest of the byte. If you have a sensor reading stored in an 8-bit variable and you want to ensure it exactly matches the lower 4 bits of 13 (1101), you use the bitwise AND operator (&) combined with a mask. For example: if ((sensorData & 0x0F) == 0x0D) checks if the lowest 4 bits equal 13, ignoring the upper 4 bits entirely. This is heavily used in ESP32 GPIO interrupt registers to clear specific interrupt flags without disturbing adjacent pin states.
For a deeper dive into the foundational math behind these operations, review the binary numeral system chapter on All About Circuits.






