A microprocessor is just a CPU — it needs external memory, storage and I/O circuitry to do anything. A microcontroller puts the CPU, the memory, the storage and the input/output hardware on a single chip. That makes it far less powerful than a desktop processor and far better suited to its job: sitting inside a microwave, a thermostat or a robot, running one program forever, using almost no power, and costing a few dollars.
| Desktop CPU | Microcontroller (ATmega328P) | |
|---|---|---|
| Clock speed | 3–5 GHz | 16 MHz |
| RAM | 8–32 GB | 2 KB |
| Storage | 500 GB+ | 32 KB flash |
| Operating system | Windows, Linux, macOS | None — your code IS the software |
| Power | 65–250 W | About 0.2 W |
| Cost | $200–800 | A few dollars |
| Analog inputs | None | Six, built in |
The structure of a sketch
| Arduino C | What it does |
|---|---|
const int LED_PIN = 9; | A named constant, declared outside both functions so both can see it |
void setup() { | Runs ONCE when the board powers up or resets |
pinMode(LED_PIN, OUTPUT); | Configure the pin's direction — this belongs in setup() |
} | End of setup |
void loop() { | Runs forever, over and over, as fast as it can |
digitalWrite(LED_PIN, HIGH); | Set the pin to 5 V |
delay(500); | Wait 500 milliseconds |
digitalWrite(LED_PIN, LOW); | Set the pin to 0 V |
delay(500); | Wait again — this makes a 1 Hz blink |
} | End of loop; execution jumps straight back to the top |
pinMode() inside loop(). It works, but it re-configures the pin thousands of times per second for no reason. Worse is the reverse error: putting the actual behaviour in setup(), where it runs exactly once and then appears to do nothing. Configuration goes in setup; behaviour goes in loop.Pins and their limits
- Digital pins 0–13 — each can be an input or an output, reading or writing only HIGH (5 V) or LOW (0 V).
- PWM pins (3, 5, 6, 9, 10, 11) — marked with a tilde. These can also produce a variable-duty square wave with
analogWrite(). - Analog inputs A0–A5 — read a voltage between 0 and 5 V and report it as a number from 0 to 1023.
- Power pins — 5V and 3.3V outputs, GND, and VIN for an external supply.
- Pins 0 and 1 — shared with the USB serial connection. Avoid them unless you need the extra pins, or uploading will fail.
Serial: your only window in
There is no screen and no error output, so the serial connection over USB is how you find out what your program thinks it is doing. Call Serial.begin(9600); in setup(), then Serial.println(value); anywhere you want to see a value, and open the Serial Monitor at the same baud rate. This is the microcontroller equivalent of a debug print, and it is the single most useful debugging tool you have.
| Arduino C | What it does |
|---|---|
Serial.begin(9600); | Start serial at 9600 baud — put this in setup() |
Serial.println(sensorValue); | Print a value and move to a new line |
Serial.print("Temp: "); | Print without a newline, for building a line in pieces |
if (Serial.available() > 0) { | Check whether the computer has sent anything |
Serial.begin(), Serial.begin() was never called, or the print is inside an if that is never true. Check them in that order.