Sub topic 5
What the USB Cable Provides
Assume you know the Arduino IDE and the setup() and loop() functions. You should also know that voltage is electrical pressure and current is the flow of charge. This chapter focuses on the USB connection on an Arduino Uno R3.
A USB cable has two jobs:
- It supplies power to the board.
- It carries data between your computer and the Arduino.
The board receives about 5 V from the computer's USB port. The Uno's power circuitry distributes this voltage to the board and connected circuits. A resettable fuse helps protect the computer's USB port if the board draws too much current.
The USB connection does not connect your computer directly to the ATmega328P microcontroller. On a classic Uno R3, a separate ATmega16U2 chip translates USB data into serial data. This chip is called a USB-to-serial bridge.
Uploading a sketch over USB
When you click Upload in the Arduino IDE, the computer opens the Uno's serial port. The USB-to-serial bridge passes the upload data to the ATmega328P's hardware UART, which uses digital pins 0 and 1 internally.
Before the upload begins, the bridge signals the Uno's reset circuit. The ATmega328P resets and starts its bootloader. A bootloader is a small program that waits briefly for a new sketch. It receives the compiled program, writes it into flash memory, and then starts the sketch.
The small TX and RX LEDs usually blink during communication. TX means the board is transmitting data to the computer. RX means it is receiving data from the computer.
Serial communication after upload
The same USB path carries serial data while your sketch runs. For example, this sketch prints a message every second:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Hello from the Uno");
delay(1000);
}Serial.begin(9600) sets the communication speed to 9600 bits per second. In the Arduino IDE, open the Serial Monitor and select the same speed. If the speeds do not match, the text may be unreadable.
The USB cable itself does not determine the serial speed. The sketch and the computer must use matching serial settings. Opening the Serial Monitor often resets the Uno because the USB-to-serial bridge toggles the reset control line.
| USB connection function | What happens |
|---|---|
| Power | The computer supplies approximately 5 V to the Uno |
| Sketch upload | USB data reaches the bootloader through the bridge |
| Serial Monitor | Runtime serial data travels between the sketch and computer |
| Reset control | The computer can trigger an automatic reset before uploading |
USB power is convenient for development, but it is limited. Motors, relays, and many bright LEDs can draw more current than the computer's USB port or the Uno's regulators should provide. Use a suitable external power source for larger loads, and connect grounds correctly when required.
Exercise
Why can the Uno run a sketch from USB, and what two chips handle the USB-to-sketch path?
Show answer
USB supplies power, and the ATmega16U2 translates USB data into serial data for the ATmega328P. The ATmega328P bootloader receives uploads, then starts the sketch.