Sub topic 2
The USB-to-Serial Bridge and the Serial Monitor
Prerequisites
This chapter assumes you know the setup() and loop() structure of a sketch, basic voltage and resistance, and how to select a board and port in the Arduino IDE.
The USB-to-serial bridge
The Uno's main chip, an ATmega328P, has no USB hardware. A second chip, an ATmega16U2, sits between the USB port and the main chip. It translates USB traffic into a plain serial (UART) signal on the TX and RX pins. The USB cable therefore carries both power and data.
Your sketch sends text with Serial.begin() and Serial.println(). The Serial Monitor in the IDE shows that text. The baud rate in the monitor must match the value in Serial.begin(), or you will see garbage characters.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Hello from the Uno");
delay(1000);
}How a sketch reaches the board
The bootloader is a small program in the chip's memory that stays resident after the first upload. It waits briefly after reset for new code. If the upload fails, check the selected port and board first.
> Note: Pins 0 and 1 are the RX and TX lines of the serial bridge. Avoid using them as ordinary digital pins while the Serial Monitor is active.
Digital pins
Digital pins 0 through 13 read or drive one of two levels. Call pinMode(pin, OUTPUT) before digitalWrite(). Call pinMode(pin, INPUT) before digitalRead(). A HIGH output is about 5V, and a LOW output is about 0V.
Floating inputs and INPUT_PULLUP
An input pin with nothing connected is floating. It picks up electrical noise and reads randomly. A pull-up resistor holds the pin HIGH until something pulls it LOW. INPUT_PULLUP enables an internal pull-up of roughly 20 to 50 kΩ, so a button wired between the pin and GND needs no external resistor. A pressed button reads LOW.
Analog inputs
Pins A0 through A5 feed a 10-bit analog-to-digital converter (ADC). analogRead() returns an integer from 0 to 1023. With the default 5V reference, each step is about 4.9 mV. The analog pins also work as digital pins 14 through 19.
Exercise
Q: Why does a pressed button read LOW when the pin uses INPUT_PULLUP, and what would change if you removed the INPUT_PULLUP line?
A: The internal pull-up holds the pin HIGH when the button is open. Pressing the button connects the pin to GND, which pulls it LOW. Without INPUT_PULLUP, the pin floats and its reading is unreliable.