How USB Works
Rocky Patel

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
2. The USB Cable — What is Inside?
A standard USB 2.0 cable has exactly 4 wires inside. Each wire has a different job:
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.
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:
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.
Group | PID Name | 4-bit Code | Simple Meaning |
TOKEN | OUT | 0001 | Computer wants to SEND data to device |
TOKEN | IN | 1001 | Computer wants to RECEIVE data from device |
TOKEN | SETUP | 1101 | Computer is about to configure the device |
TOKEN | SOF | 0101 | Start of Frame — a timing heartbeat every 1 ms |
DATA | DATA0 | 0011 | Data packet — even toggle |
DATA | DATA1 | 1011 | Data packet — odd toggle |
HAND | ACK | 0010 | Received OK — thank you! |
HAND | NAK | 1010 | Device busy — please try again |
HAND | STALL | 1110 | Error — device cannot handle this request |
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.
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:
USB Speed | Max Data per Packet | Example Use |
Low Speed (1.5 Mbps) | 8 bytes | Keyboard, old mouse |
Full Speed (12 Mbps) | 64 bytes | Game controllers |
Hi-Speed (480 Mbps) | 512 bytes | Pen drives (USB 2.0) |
SuperSpeed (5+ Gbps) | 1024 bytes | USB 3.0 drives |
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.
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.
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.
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.
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.
6.2 OUT Transaction (Host sends data to Device)
Example: Sending a document to a printer
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.
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.
Transfer Type | Best For | Is it Reliable? | Is Timing Guaranteed? | Packet Size |
Control | Configure device at startup | Yes, with retries | No | 8–64 bytes |
Bulk | Pen drives, printers — big data | Yes, with retries | No (waits for free bus) | 64–1024 bytes |
Interrupt | Mouse, keyboard — small frequent data | Yes, with retries | Yes (polling interval) | 1–64 bytes |
Isochronous | Audio, video — real-time streaming | No retries | Yes (fixed time slots) | Up to 1024 bytes |
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:
Interrupt Transfer — Mouse Example
Your mouse sends its position every 8 milliseconds. The Host polls it (asks it) at that interval:
Isochronous Transfer — Microphone Example
A USB microphone sends audio continuously. No ACK/NAK — it just flows in real time:
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:
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)
Descriptor Hierarchy
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.
