Learn Arduino Tutorials Open the 3D lab

Beginner · 5 min read

Arduino code basics — setup(), loop(), variables, if and for explained

Every Arduino program has the same simple shape. Learn the dozen words that make up most beginner sketches — and why a missing semicolon is the most common error in the world.

An Arduino UNO blinking a red LED, running the code explained in this guide
Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →

The short answer

An Arduino program is called a sketch. Every sketch has two parts:

  • setup() runs once, when the board powers up or you press reset. You prepare things here.
  • loop() runs again and again, forever, right after setup. Your project's behaviour goes here.
void setup() {
  // runs once
}

void loop() {
  // runs forever
}

Everything else — variables, if, for — goes inside (or above) those two.

Your first real sketch, line by line

This is the code of our Blink project. It switches pin 13 on and off every second:

const int LED = 13;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(LED, HIGH);   // 5 V on the pin: current flows, the LED lights
  Serial.println("on");
  delay(1000);
  digitalWrite(LED, LOW);    // 0 V: no current, the LED goes dark
  Serial.println("off");
  delay(1000);
}
LineWhat it does
const int LED = 13;Gives the number 13 a name, so the code reads like English
pinMode(LED, OUTPUT);Pin 13 will drive something (not read it)
Serial.begin(9600);Opens the serial monitor at 9600 baud
digitalWrite(LED, HIGH);Puts 5 V on the pin
digitalWrite(LED, LOW);Puts 0 V on the pin
delay(1000);Waits 1000 milliseconds = 1 second
This exact code, running on a simulated UNO — press Run, then try changing the delays
This exact code, running on a simulated UNO — press Run, then try changing the delays · open it full screen

Variables: boxes with names

A variable is a named box that holds a value. You say what kind of value it holds, give it a name, and (usually) a starting value:

int blinks = 0;          // a whole number
float celsius = 22.5;    // a number with decimals
bool fanOn = false;      // true or false
const int BUTTON = 2;    // const: a value that never changes
TypeHoldsExample
intWhole numbers, −32768 to 32767 on the UNOint speed = 120;
longBigger whole numberslong echo = pulseIn(ECHO, HIGH);
floatDecimalsfloat volts = 2.5;
booltrue or falsebool fanOn = false;

A variable made outside setup() and loop() (at the top) keeps its value between loops. That is how the Meet-the-board project counts: blinks = blinks + 1; adds one every time round.

The three commands you will use most

  • pinMode(pin, mode) — sets a pin up as OUTPUT, INPUT or INPUT_PULLUP. Call it in setup().
  • digitalWrite(pin, value) — sets an output pin HIGH (5 V) or LOW (0 V).
  • delay(ms) — pauses for that many milliseconds. 1000 ms = 1 s. While it waits, the board does nothing else.

Reading works the same way: digitalRead(pin) gives HIGH or LOW, and analogRead(A0) gives a number from 0 to 1023 (see digital vs analog).

Comments and semicolons

  • Comments are notes for humans. The board ignores them. // starts a one-line comment; /* … */ wraps several lines.
  • Every statement ends with a semicolon ; — it is like a full stop in a sentence. Lines that open a block with { (like void loop() { or if (…) {) do not get one.

Upper and lower case matter too: digitalwrite is not digitalWrite, and high is not HIGH.

if: making decisions

if runs a block only when a condition is true; else runs when it is not. This is from our Night light project:

if (light < DARK) {
  // the darker it is, the brighter the night light
  analogWrite(LED, map(light, 0, DARK, 255, 30));
} else {
  analogWrite(LED, 0);
}

Comparisons you can use: < > <= >= == (equal) and != (not equal).

for: doing something many times

A for loop repeats a block while counting. It has three parts: where to start, when to keep going, and how to step. From our Motor project:

for (int speed = 0; speed <= 255; speed += 15) {
  analogWrite(MOTOR, speed);
  Serial.print("speed ");
  Serial.println(speed);
  delay(150);
}

Read it as: "start speed at 0; while speed is 255 or less, run the block; then add 15". The motor speeds up in 18 steps.

Functions: your own commands

You can bundle lines into your own command. The RGB project does this so the main code stays short:

void setColor(int r, int g, int b) {
  analogWrite(RED, r);
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);
}

Now setColor(255, 0, 0); makes red. void means the function gives nothing back.

Check yourself

  1. How many times does setup() run?

  2. Which line is correct Arduino code?

  3. What does for (int i = 0; i < 3; i++) do?

Questions people ask

What is the difference between setup and loop in Arduino?

setup() runs once at the start — use it for pinMode() and Serial.begin(). loop() runs over and over for as long as the board has power — put the things your project keeps doing there.

Can an Arduino sketch work without loop()?

No. Both setup() and loop() must exist, or the code will not compile. If you have nothing to repeat, leave loop() empty: void loop() {}.

Why does my Arduino code say "was not declared in this scope"?

You used a name the compiler does not know: usually a typo (led vs LED), a variable made inside another block, or a missing #include for a library. Check the spelling and where the variable is created.

Is delay() bad?

Not for learning. But during a delay() the board cannot notice a button press. Bigger projects use millis() (the time since start) to do several things at once.

Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →
Next: Voltage, current and resistance — Ohm's law for Arduino beginners →

Your progress stays on this device — no account, nothing sent anywhere.

Last updated 2026-10-29

Read next