Sub topic 1
What the board does and what a digital pin is
An Arduino board runs one program, over and over, and uses that program to read and drive electrical signals on its pins. This chapter covers what is on the board, what a digital pin is electrically, and what the board can safely supply. The later chapters on sketches, uploading and wiring all depend on these ideas.
Prerequisites
This chapter assumes you have the following:
- A basic idea of what a program and a variable are.
- An introductory grasp of voltage, current and resistance. You should know that voltage is measured in volts (V), current in amperes (A, or mA for thousandths), and that a resistor limits current.
- An Arduino-compatible board, a USB cable and the Arduino IDE installed. The examples use an Arduino Uno R3, and notes mark where other boards differ.
You do not need to know C++ yet. The one short sketch below is explained line by line.
What the board actually is
A microcontroller is a small computer on a single chip. It holds a processor, program memory, working memory and input/output pins. An Arduino board is that chip plus the parts that make it practical to use:
- A USB interface to power the board and to receive new programs from your computer.
- A voltage regulator that turns the 5 V from USB, or a higher voltage from the barrel jack, into the steady supply the chip needs.
- A clock source (a crystal or resonator) that sets how fast the processor runs.
- Header sockets that bring the chip's pins out so you can plug wires into them.
- A built-in LED and a reset button.
On an Uno R3 the microcontroller is an ATmega328P. Its main numbers are in the table below.
| Property | Uno R3 value | What it means for you |
|---|---|---|
| Clock speed | 16 MHz | About 16 million basic cycles per second. Simple instructions take one or a few cycles. |
| Flash memory | 32 KB | Stores your compiled program. It keeps its contents with the power off. |
| SRAM | 2 KB | Holds variables while the program runs. It is erased when power is lost. |
| EEPROM | 1 KB | Small storage for settings that must survive a power cycle. |
| Logic voltage | 5 V | The voltage a pin outputs when it is HIGH. |
The board runs exactly one program. There is no operating system and no way to start a second program. When power arrives, the chip starts executing your program from the beginning and keeps going until power is removed. This is why a program on a bare microcontroller usually never "finishes".
What a digital pin is
A digital pin is a metal contact on the chip, brought out to a header socket on the board. At any moment it is in one of two states:
- HIGH: the pin is at, or close to, the supply voltage (5 V on an Uno).
- LOW: the pin is at, or close to, ground (0 V).
The software works with these two states as HIGH and LOW, which are the numbers 1 and 0. A pin carries one bit of information, and a voltage in between is not a state the pin is meant to hold.
Every digital pin also works in one of two directions. You choose the direction in your program.
- Output: the chip drives the pin. Your program decides whether the pin is connected internally to the supply or to ground. The pin can push current out to a load, such as an LED, or pull current in from one.
- Input: the chip only measures the voltage on the pin and reports HIGH or LOW. An input draws almost no current, so it does not disturb the signal it measures.
An input does not report "HIGH" only at exactly 5 V. The chip uses thresholds. For the ATmega328P at 5 V supply, a voltage below about 1.5 V reads as LOW and above about 3.0 V reads as HIGH. Voltages in between give an unreliable result.
Pin numbering on the Uno
The Uno exposes these pins:
| Pins | Label | Notes |
|---|---|---|
| 0 to 13 | D0 to D13 | The main digital pins. |
| 14 to 19 | A0 to A5 | Analog input pins that also work as digital pins. In code you can write `A0` or `14`. |
| 0 and 1 | RX, TX | Shared with the USB serial connection. Avoid them for your own wiring while you upload or use `Serial`. |
| 13 | LED_BUILTIN | Connected to the on-board LED through its own resistor. |
Use the constant LED_BUILTIN instead of writing 13. The constant has the right value for whichever board you selected in the IDE, so the same sketch works across boards where the built-in LED sits on a different pin.
Electrical limits you must respect
A digital pin is not a power outlet. It is a small switch inside a chip, and it has fixed limits.
- Current per pin. The ATmega328P datasheet gives an absolute maximum of 40 mA per I/O pin. Treat 20 mA as the practical ceiling for continuous use, and stay under it by design.
- Current for the whole chip. The total through all pins together is limited to about 200 mA. Many pins driving loads at the same time can exceed this even if each pin is within its own limit.
- Voltage on an input. On a 5 V board, keep input voltages between 0 V and 5 V. A voltage above the supply or below ground can damage the pin.
- Logic level differs by board. The Uno and Nano use 5 V logic. Many newer boards, such as the Arduino Due, the Nano 33 family and most ESP32 boards, use 3.3 V logic. Connecting a 5 V signal to a 3.3 V pin can destroy it.
An output pin that goes HIGH is a source of current, and an output pin that goes LOW is a sink for current. Both directions work, and both are subject to the same current limit.
Pins start as inputs
When the board resets, all digital pins are inputs. An input pin that is not connected to anything is said to be floating. Its voltage is set by stray electrical noise, so reads from it can flip between HIGH and LOW unpredictably. This matters later when you read buttons, and it is also the reason a pin must be told to be an output before you rely on it to drive something.
In practice this has a simple consequence. The program must configure each pin's direction before using the pin. The function that does this is pinMode(), and the function that sets an output's state is digitalWrite(). The next chapters explain where in a sketch they belong and how they work in detail. The short example below uses both so you can see a pin respond.
A first look at a pin in code
This sketch sets the built-in LED pin as an output, switches it on, and reports the pin's state over the serial connection. Upload it using the steps in the chapter on uploading, then open the Serial Monitor at 9600 baud.
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
int state = digitalRead(LED_BUILTIN);
Serial.print("LED_BUILTIN reads: ");
Serial.println(state);
delay(1000);
}Here is what each part does:
Serial.begin(9600)starts the serial connection through the USB cable at 9600 bits per second, so the board can print text to your computer.pinMode(LED_BUILTIN, OUTPUT)makes the built-in LED's pin an output. Before this call the pin is a floating input.digitalWrite(LED_BUILTIN, HIGH)drives the pin to 5 V. The on-board LED lights and stays lit.digitalRead(LED_BUILTIN)returns the pin's current state as 1 or 0. For an output pin on the Uno it returns the level the pin was set to, so you see1.delay(1000)waits one second between messages so the monitor does not fill up too fast.
The LED does not blink here. It stays on, because the program sets the pin once and never changes it. Making the pin change over time is the subject of the chapter on delay().
Common mistakes with pins
These errors come from misunderstanding what a pin is, and they cause most early problems:
- Using the wrong pin number. The number in code is the Arduino pin number, such as
8. It is not the number of the physical chip leg, and it is not a position on the breadboard. Match the number to the label printed on the board. - Forgetting
pinMode(). A pin that is still an input will not drive an LED properly. On many boards it gives a very faint glow, which is confusing because it looks almost like a working circuit. - Wiring to pins 0 and 1. These are shared with USB serial. A wire on them can make uploads fail.
- Assuming 5 V everywhere. Check your board's logic level before connecting any external part.
- Treating a pin as a power source. A pin can power a small LED through a resistor. It cannot power a motor, a relay coil or a long strip of LEDs. Those need a transistor or driver and their own supply.
How this connects to the rest of the tutorial
The path through the tutorial follows from the board's basics. The chapter after this one describes how a sketch is organized into setup() and loop(). After that, the chapters on pinMode() and digitalWrite() build on the output direction introduced here, and the upload chapter covers how your code reaches the flash memory shown in the table above. The electrical limits in this chapter come back when you size a resistor for the LED, and they are the first thing to check when a circuit misbehaves.
Exercise
A pin on a 5 V Uno is set as an output and driven HIGH. Roughly what voltage is on the pin, which direction can current flow, and what limits how much current it should supply?
Show answer
The pin is at about 5 V, close to the supply voltage. In this state the pin acts as a current source, so current flows out of the pin through the load toward ground. The pin's current rating limits it. The datasheet maximum is 40 mA, and a safe design keeps it at or below about 20 mA.
Exercise
You upload a sketch that calls digitalWrite(8, HIGH) but never calls pinMode(8, OUTPUT). You wire an LED and a resistor from pin 8 to ground. The LED is very dim. Why, and what is the fix?
Show answer
Pins start as inputs after a reset. Calling digitalWrite() on an input pin does not drive the pin firmly. On the ATmega328P it only switches on the internal pull-up resistor, which supplies a tiny current, so the LED glows faintly. The fix is to call pinMode(8, OUTPUT) in setup() so the chip actually drives the pin.
Exercise
Why is it better to write LED_BUILTIN than the number 13 in a sketch for the on-board LED?
Show answer
LED_BUILTIN is defined for the board selected in the IDE, so it holds the correct pin number for that board. A sketch that uses it works unchanged on boards where the on-board LED is on a different pin, while a hard-coded 13 would drive the wrong pin there.