All lessons
⌨️Programming 7 min read

Basic Arduino Programming

setup, loop, and the shape every Arduino sketch shares.

In 10 seconds

  • setup() runs once, loop() runs forever.
  • pinMode decides if a pin listens or speaks.
  • delay() counts milliseconds: 1000 = 1 second.

Every Arduino program in the world, from a blinking LED to a flying drone, has the same skeleton. Two functions, always present, always named the same way. Once you understand what each one is for, you can read almost any sketch you find online.

setup() runs once

When the Arduino powers up or you press the reset button, it runs everything inside setup() exactly one time, then never again. This is where you prepare things: telling pins whether they are inputs or outputs, starting the Serial connection, attaching servos, initialising screens. Think of it as unpacking your tools before you begin work.

loop() runs forever

As soon as setup finishes, the Arduino starts running loop(). It reads every line from top to bottom, reaches the closing brace, and immediately starts again from the top. It does this thousands of times a second and never stops until you cut the power. All the actual behaviour of your project lives here: read a sensor, decide something, switch an output, repeat.

This repetition is why an Arduino feels responsive. It is not waiting for you — it is asking "anything changed? anything changed? anything changed?" constantly, faster than you can blink.

Reading the punctuation

C++, the language Arduino uses, is fussy about symbols. These few rules cover almost everything:

  • Every instruction ends in a semicolon ; — like a full stop. A missing one is the number one cause of errors.
  • Curly braces { } group instructions that belong together.
  • Round brackets ( ) hold the information you give a command, like the pin number.
  • Two slashes // start a comment — a note for humans that the Arduino ignores completely.
  • Capital letters matter. digitalWrite works; DigitalWrite does not.

The commands you will use every day

  • pinMode(pin, OUTPUT) — this pin will control something.
  • pinMode(pin, INPUT_PULLUP) — this pin will listen to a button.
  • digitalWrite(pin, HIGH) — switch it on.
  • digitalRead(pin) — is it HIGH or LOW?
  • analogRead(A0) — measure a sensor, 0 to 1023.
  • analogWrite(pin, 128) — half brightness using PWM.
  • delay(1000) — pause for one second.
  • Serial.println(value) — send a message to your computer.

The Serial Monitor is your best friend

You cannot see inside a running program, which makes bugs mysterious. Serial fixes that. Put Serial.begin(9600); in setup, then Serial.println() anywhere you want to peek at a value, and open the Serial Monitor in the IDE. Suddenly you can watch your sensor readings, check whether an if statement is being reached, and see exactly where things go wrong. Professional engineers do this constantly. It is not cheating; it is the job.

Quick quiz: Basic Arduino Programming

3 questions to check it stuck.

Take the quiz

Next lesson

📦 Variables

Keep learning

Ready to try it for real?

Reading is good. Wiring is better. Pick a project and put this to work.

Explore projects