Skip to content
Study Hall
Lesson 1 of 4Computer Interfacing
Unit 6 · Lesson 1 of 4

Microcontrollers and the Arduino

A whole computer on one chip, the board it lives on, and the structure of every sketch you will write.

4 min read8 practice questionsAsk AI about this →

Key things to remember

  1. 1A microcontroller has CPU, memory and I/O on one chip and runs with no operating system.
  2. 2setup() runs once at power-up; loop() runs forever.
  3. 3Pin configuration goes in setup(); behaviour goes in loop().
  4. 4An Uno pin can supply at most 40 mA, and all pins together at most 200 mA.
  5. 5PWM is available only on pins 3, 5, 6, 9, 10 and 11.
  6. 6Analog inputs A0–A5 report 0–1023 for 0–5 V.
  7. 7Serial.begin(9600) plus Serial.println() is how you debug a sketch.

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.

Digital pins 0–13 (~ marks the 6 PWM pins: 3, 5, 6, 9, 10, 11)ATmega328P16 MHz · 32 KB flash · 2 KB RAMPower: 5V, 3.3V, GND, VINAnalog in A0–A5 (10-bit)USB7–12 Vpin limits40 mA max200 mA totalA microcontroller is a whole computer — CPU, memory and I/O — on one chip.Exceed the current limit and you destroy the pin, not the LED. Anything hungrier than an LED needs a transistor.
Fig 1An Arduino Uno: where each kind of pin lives, and the limits you must respect.
Desktop CPUMicrocontroller (ATmega328P)
Clock speed3–5 GHz16 MHz
RAM8–32 GB2 KB
Storage500 GB+32 KB flash
Operating systemWindows, Linux, macOSNone — your code IS the software
Power65–250 WAbout 0.2 W
Cost$200–800A few dollars
Analog inputsNoneSix, built in
No operating system
There is no scheduler, no file system, and nothing else running. Your program starts when power is applied and runs until power is removed. That means nothing can interrupt your timing — but it also means nothing will rescue you from an infinite loop, and there is no error message waiting anywhere.

The structure of a sketch

Arduino CWhat 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
The most common beginner mistake
Putting 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.
40 mA per pin, 200 mA total
An Uno pin can source or sink at most 40 mA, and all pins together at most 200 mA. An LED with a resistor draws about 20 mA and is fine. A small motor draws 200–500 mA and will destroy the pin — the pin, not the motor. Anything hungrier than an LED needs a transistor or a driver chip between it and the microcontroller.

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 CWhat 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
Blank Serial Monitor?
Three usual causes: the baud rate in the monitor does not match Serial.begin(), Serial.begin() was never called, or the print is inside an if that is never true. Check them in that order.
Powering the board
USB supplies 5 V and up to about 500 mA, which is fine for LEDs and sensors. The barrel jack accepts 7–12 V through an on-board regulator. Anything with a motor should get its own supply, sharing only a common ground with the Arduino — a motor's current spikes will otherwise brown out the board and reset it mid-program.

Worked examples

Reveal one step at a time and try to beat it to the next line.

Example 1
Write a sketch that blinks an LED on pin 8 with 200 ms on and 800 ms off, and prints the state to the serial monitor each time it changes.
Example 2
A student connects a 6 V DC motor that draws 300 mA directly between pin 9 and ground. Explain what happens and give the correct circuit.

Where marks get lost

  • Putting pinMode() in loop() instead of setup().
  • Putting the program's actual behaviour in setup(), so it runs once and appears dead.
  • Connecting a motor, relay or long LED strip directly to a pin.
  • Using pins 0 and 1 for a project, which conflicts with USB uploading.
  • Forgetting Serial.begin() and then wondering why the Serial Monitor is blank.
  • Setting the Serial Monitor to a different baud rate than the sketch, which produces garbage characters.
  • Powering a motor from the Arduino's 5 V pin, which browns out the board and resets it mid-program.

Flashcards 9

Microprocessor vs. microcontroller
What runs in setup()?
What runs in loop()?
Arduino Uno current limits
+ 5 more

Practice

1
Which function runs only once when an Arduino is powered on?
2
What is the maximum current a single Arduino Uno pin should source, in milliamps?
mA
3
Where should pinMode(7, OUTPUT); be placed?
4
The Serial Monitor shows nothing while a sketch runs. What is the most likely cause?
5
Why can't a 250 mA relay coil be driven directly from a digital pin?
6
Order the parts of a typical Arduino sketch.
  1. 1void setup() with pinMode and Serial.begin
  2. 2Any helper functions after loop
  3. 3void loop() with the repeating behaviour
  4. 4Constants and pin definitions at the top
7
Match each Arduino feature to its description.
Digital pins 0–13
Pins 3, 5, 6, 9, 10, 11
Pins A0–A5
VIN
8
A microcontroller has no operating system. What follows from that?

Spot a mistake on this page? Let us know