# How USB Works

USB stands for Universal Serial Bus. It is the technology that lets you connect devices — like a mouse, keyboard, phone, or pen drive — to your computer, without restarting it. You just plug it in and it works.

**Think of USB like a road between your computer and a device.** Data travels on that road in small parcels called packets — just like how letters are sent in envelopes through the post

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/3f667f0a-e76d-4ba7-81cb-50d2e831b42a.png align="center")

### **2\. The USB Cable — What is Inside?**

A standard USB 2.0 cable has exactly 4 wires inside. Each wire has a different job:

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/64410eba-ad68-4820-8a34-1b2cbfe88c84.png align="center")

**Why two data wires (D+ and D-)?** This is called differential signaling. The two wires carry opposite versions of the same signal. If electrical noise interferes with the cable, it affects both wires equally — and the receiver cancels it out by looking at the difference between them. This is why USB works reliably even in noisy environments.

USB 3.0 cables add 5 more wires — for the extra-fast SuperSpeed lanes — but the same 4 original wires are still there for backward compatibility.

### **3.How USB is Organized — Host and Devices**

**USB uses a master-slave model.** Your computer is the Master (called the Host). Every device you plug in is the Slave. The Host controls all communication — a device never speaks unless the Host asks it to.

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/c3bab23c-2232-439f-83cd-9979fcbb945e.png align="center")

### **4.Data Packets — How Data Actually Travels**

**A packet is a small bundle of data with a specific structure.** Think of it like a letter in an envelope: the envelope has a 'To' address, a 'From' address, the letter inside, and maybe a wax seal to check it wasn't tampered with. USB packets work the same way.

5.1 The Basic Structure of Every USB Packet

Every USB packet — no matter what type — follows this layout from left to right:

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/807fabe5-de8a-47cd-87c4-30ebe416e4aa.png align="center")

### SYNC  (8 bits)

The very first thing in every packet. It is always the bit pattern 00000001. It tells the receiver: 'A packet is starting — synchronize your clock now!' Without this, the receiver would not know when bits begin.

### PID — Packet Identifier  (8 bits)

**PID is the most important field.** It tells the receiver what kind of packet this is and what to do with it.

The 8 bits are split into two halves: bits \[3:0\] = the actual 4-bit type code, and bits \[7:4\] = the bitwise complement of the type code. This lets the receiver instantly check if the PID itself was corrupted — if the two halves don't match, something went wrong.

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/f46f74f4-b462-44ca-a02a-6b5daf32c02a.png align="center")

<table style="min-width: 100px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Group</strong></p></td><td colspan="1" rowspan="1"><p><strong>PID Name</strong></p></td><td colspan="1" rowspan="1"><p><strong>4-bit Code</strong></p></td><td colspan="1" rowspan="1"><p><strong>Simple Meaning</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p>TOKEN</p></td><td colspan="1" rowspan="1"><p>OUT</p></td><td colspan="1" rowspan="1"><p>0001</p></td><td colspan="1" rowspan="1"><p>Computer wants to SEND data to device</p></td></tr><tr><td colspan="1" rowspan="1"><p>TOKEN</p></td><td colspan="1" rowspan="1"><p>IN</p></td><td colspan="1" rowspan="1"><p>1001</p></td><td colspan="1" rowspan="1"><p>Computer wants to RECEIVE data from device</p></td></tr><tr><td colspan="1" rowspan="1"><p>TOKEN</p></td><td colspan="1" rowspan="1"><p>SETUP</p></td><td colspan="1" rowspan="1"><p>1101</p></td><td colspan="1" rowspan="1"><p>Computer is about to configure the device</p></td></tr><tr><td colspan="1" rowspan="1"><p>TOKEN</p></td><td colspan="1" rowspan="1"><p>SOF</p></td><td colspan="1" rowspan="1"><p>0101</p></td><td colspan="1" rowspan="1"><p>Start of Frame — a timing heartbeat every 1 ms</p></td></tr><tr><td colspan="1" rowspan="1"><p>DATA</p></td><td colspan="1" rowspan="1"><p>DATA0</p></td><td colspan="1" rowspan="1"><p>0011</p></td><td colspan="1" rowspan="1"><p>Data packet — even toggle</p></td></tr><tr><td colspan="1" rowspan="1"><p>DATA</p></td><td colspan="1" rowspan="1"><p>DATA1</p></td><td colspan="1" rowspan="1"><p>1011</p></td><td colspan="1" rowspan="1"><p>Data packet — odd toggle</p></td></tr><tr><td colspan="1" rowspan="1"><p>HAND</p></td><td colspan="1" rowspan="1"><p>ACK</p></td><td colspan="1" rowspan="1"><p>0010</p></td><td colspan="1" rowspan="1"><p>Received OK — thank you!</p></td></tr><tr><td colspan="1" rowspan="1"><p>HAND</p></td><td colspan="1" rowspan="1"><p>NAK</p></td><td colspan="1" rowspan="1"><p>1010</p></td><td colspan="1" rowspan="1"><p>Device busy — please try again</p></td></tr><tr><td colspan="1" rowspan="1"><p>HAND</p></td><td colspan="1" rowspan="1"><p>STALL</p></td><td colspan="1" rowspan="1"><p>1110</p></td><td colspan="1" rowspan="1"><p>Error — device cannot handle this request</p></td></tr></tbody></table>

### Address Field  (7 bits) + Endpoint  (4 bits)

These appear in TOKEN packets only. Address is the device's unique number on the bus (1 to 127). Endpoint is like a door number on the device — each device can have up to 16 endpoints, each used for a different purpose.

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/614ed363-2863-417e-a4fc-af4b809c6408.png align="center")

### Data Field  (0 to 1024 bytes)

This is the actual content being transferred — the payload. Its maximum size depends on the transfer type and USB version:

<table style="min-width: 75px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>USB Speed</strong></p></td><td colspan="1" rowspan="1"><p><strong>Max Data per Packet</strong></p></td><td colspan="1" rowspan="1"><p><strong>Example Use</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p>Low Speed (1.5 Mbps)</p></td><td colspan="1" rowspan="1"><p>8 bytes</p></td><td colspan="1" rowspan="1"><p>Keyboard, old mouse</p></td></tr><tr><td colspan="1" rowspan="1"><p>Full Speed (12 Mbps)</p></td><td colspan="1" rowspan="1"><p>64 bytes</p></td><td colspan="1" rowspan="1"><p>Game controllers</p></td></tr><tr><td colspan="1" rowspan="1"><p>Hi-Speed (480 Mbps)</p></td><td colspan="1" rowspan="1"><p>512 bytes</p></td><td colspan="1" rowspan="1"><p>Pen drives (USB 2.0)</p></td></tr><tr><td colspan="1" rowspan="1"><p>SuperSpeed (5+ Gbps)</p></td><td colspan="1" rowspan="1"><p>1024 bytes</p></td><td colspan="1" rowspan="1"><p>USB 3.0 drives</p></td></tr></tbody></table>

### CRC — Error Check  (5 or 16 bits)

CRC stands for Cyclic Redundancy Check. Before sending, the sender runs a maths formula over the data and attaches the result as the CRC. When the receiver gets the packet, it runs the same formula. If the results match — data is good. If not — data was corrupted in transit, and the packet must be sent again.

TOKEN packets use a 5-bit CRC. DATA packets use a 16-bit CRC (stronger protection because data payloads are larger).

### EOP — End of Packet

A special electrical signal (both D+ and D- pulled low, called Single-Ended Zero) that tells the receiver the packet is finished.

### 5\. The Four Packet Types — Full Diagrams

**5.1 TOKEN Packet  (sent by Host only)**

Token packets start every transaction. The Host sends one to say what it wants to do next.

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/446b2006-5d7b-404e-a1da-e7d072e8ce16.png align="center")

**5.2 DATA Packet  (sent by Host or Device)**

Data packets carry the actual information. DATA0 and DATA1 alternate back and forth — this is called data toggling and helps detect if a packet was accidentally duplicated.

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/75ed3f40-64c7-41d3-b716-66ba5014ccca.png align="center")

**5.3 HANDSHAKE Packet  (ACK / NAK / STALL)**

After data is received, a handshake packet is sent back to say what happened. These are the shortest packets — just SYNC and PID.

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/f56bb118-57bc-4da9-889b-f379671ffe4e.png align="center")

**5.4 SOF Packet  (Start of Frame — timing heartbeat)**

Every 1 millisecond (for Full Speed USB) or every 125 microseconds (for Hi-Speed), the Host sends a special SOF packet. This acts like a clock tick that keeps all devices synchronized.

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/e0f88ba6-8bbb-4dce-b2e0-4ee7478fdd0b.png align="center")

### **6\. Transactions — A Complete Action**

A transaction = 2 or 3 packets grouped together to complete one action. Every transaction follows the same pattern: (1) Host sends a TOKEN, (2) someone sends DATA, (3) receiver sends a HANDSHAKE.

**6.1 IN Transaction  (Device sends data to Host)**

Example: Reading which key was pressed on a keyboard.

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/6e10291d-e3a0-4c4a-87d8-7888dc8647f9.png align="center")

**6.2 OUT Transaction  (Host sends data to Device)**

Example: Sending a document to a printer

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/acf07d0b-96d5-4221-9f01-975cc43578c0.png align="center")

**6.3 SETUP Transaction  (Configuring a Device)**

This is a special transaction used only when first configuring a device. The data payload is always exactly 8 bytes and follows a fixed format.

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/4b939506-7df8-4a80-a5f2-d0cb31fe859b.png align="center")

### **7\. The Four Transfer Types**

USB uses four different styles of data transfer, each suited to a different need. Think of them as four different postal services — each with different speed, reliability, and cost.

<table style="min-width: 125px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Transfer Type</strong></p></td><td colspan="1" rowspan="1"><p><strong>Best For</strong></p></td><td colspan="1" rowspan="1"><p><strong>Is it Reliable?</strong></p></td><td colspan="1" rowspan="1"><p><strong>Is Timing Guaranteed?</strong></p></td><td colspan="1" rowspan="1"><p><strong>Packet Size</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p>Control</p></td><td colspan="1" rowspan="1"><p>Configure device at startup</p></td><td colspan="1" rowspan="1"><p>Yes, with retries</p></td><td colspan="1" rowspan="1"><p>No</p></td><td colspan="1" rowspan="1"><p>8–64 bytes</p></td></tr><tr><td colspan="1" rowspan="1"><p>Bulk</p></td><td colspan="1" rowspan="1"><p>Pen drives, printers — big data</p></td><td colspan="1" rowspan="1"><p>Yes, with retries</p></td><td colspan="1" rowspan="1"><p>No (waits for free bus)</p></td><td colspan="1" rowspan="1"><p>64–1024 bytes</p></td></tr><tr><td colspan="1" rowspan="1"><p>Interrupt</p></td><td colspan="1" rowspan="1"><p>Mouse, keyboard — small frequent data</p></td><td colspan="1" rowspan="1"><p>Yes, with retries</p></td><td colspan="1" rowspan="1"><p>Yes (polling interval)</p></td><td colspan="1" rowspan="1"><p>1–64 bytes</p></td></tr><tr><td colspan="1" rowspan="1"><p>Isochronous</p></td><td colspan="1" rowspan="1"><p>Audio, video — real-time streaming</p></td><td colspan="1" rowspan="1"><p>No retries</p></td><td colspan="1" rowspan="1"><p>Yes (fixed time slots)</p></td><td colspan="1" rowspan="1"><p>Up to 1024 bytes</p></td></tr></tbody></table>

 **Bulk Transfer — Pen Drive Example**

When you copy a 1 MB file to a pen drive, here is exactly what happens at the packet level:

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/5f00de34-8f05-48c3-8d7d-5a3d874f8401.png align="center")

### **Interrupt Transfer — Mouse Example**

Your mouse sends its position every 8 milliseconds. The Host polls it (asks it) at that interval:

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/b757b3d3-ff47-44a7-a4d3-82f2389ea924.png align="center")

### **Isochronous Transfer — Microphone Example**

A USB microphone sends audio continuously. No ACK/NAK — it just flows in real time:

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/8dba5290-71e1-456d-a990-7d5b98551902.png align="center")

### **8\. What Happens When You Plug In a Device?**

**Enumeration** is the automatic process that happens in the first few hundred milliseconds after you plug in a USB device. Here is every step:

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/8bc4696e-cd23-45df-a14c-9b67e7004e21.png align="center")

### **9\. Descriptors — A Device's ID Card**

**Descriptors are data structures that tell the Host everything about a device.** They are stored inside the device's firmware. The Host reads them during enumeration.

 **Device Descriptor  (always exactly 18 bytes)**

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/774a9b18-da9f-4226-a29c-dde2e3a3f1e0.png align="center")

**Descriptor Hierarchy**

![](https://cdn.hashnode.com/uploads/covers/69afc2b9af06a097c3ff5c26/51e99bfa-888c-49f3-ae19-978c088ad017.png align="center")

## Conclusion

In this article, we explored the fundamentals of USB communication and discussed how USB devices interact with the host during enumeration. Understanding descriptors, endpoint configuration, and control transfers is essential when debugging embedded USB firmware.

### Connect With Me

If you are interested in embedded systems, firmware development, or USB debugging, feel free to connect with me.

LinkedIn: [https://www.linkedin.com/in/rocky-patel-eee/](https://www.linkedin.com/in/rocky-patel-eee/)
