Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← Back to Start Here

LEARN · FIRST PRINCIPLES

How code runs

Setup once, loop repeatedlyExplain framework calls without equating loop() with language loop syntax.
  1. startupthen
  2. setup()then
  3. loop() callthen
  4. call returns

↺ call returns → loop() call

reset → startup

At startup setup runs once. Then loop runs and returns. The framework calls loop again, repeating until execution stops or resets. delay waits within a call; setup runs again after a new startup.

Connections and limits

The Arduino framework initializes startup, calls setup once per startup, then calls loop again whenever the previous call returns. A wait delays progress; power loss stops execution and a restart begins again.

loop() is a function repeatedly called by the framework. for and while are separate C++ statements. Do not draw a second setup call on every repetition or imply reset retains ordinary working values.

The board is not understanding English. The code is transformed into processor instructions, and the processor follows those instructions. A processor is the part of the computer that performs these operations. Memory holds instructions and information; some storage retains a program when power is removed, while ordinary working values need not survive power loss.

A program is an organized set of instructions. Its written source code uses a language with exact rules. Our language is C++, in an ordinary .cpp file. Build translates and combines the project's code and supporting code into a runnable program for the chosen board. Upload transfers it to the board. See the software journey for the intervening steps.

Use the reviewed Uno PlatformIO project. PlatformIO is the tool managing its build settings and transfer; VS Code is the editor where you change text. Replace src/main.cpp, meaning main.cpp inside the src folder, with this complete example. For this page use the first lab's onboard LED, whose connections are already provided on the Uno. The code has one additional condition so you can see how a remembered value chooses behavior.

cpp
#include <Arduino.h>

bool lightOn = false;

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    lightOn = !lightOn;
    if (lightOn) {
        digitalWrite(LED_BUILTIN, HIGH);
    } else {
        digitalWrite(LED_BUILTIN, LOW);
    }
    delay(1000);
}

Names and stored information. #include <Arduino.h> makes names supplied by the Arduino framework available here. A framework is reusable support code, including the machinery that calls our functions. bool lightOn = false; creates a variable, a named value the program can remember and use. Its type, bool, allows the values true and false. Here those mean “request the light on” and “request it off.” The equals sign supplies its starting value. This initialization happens before the framework calls setup().

A job that runs once. A function is a named group of instructions doing a job. void setup() defines the startup job; void says this function gives no return value, or result, to whoever called it. Empty parentheses mean it receives no arguments, or supplied values. Braces group its instructions. The framework calls this function once after startup. pinMode(LED_BUILTIN, OUTPUT); calls a support function to configure the onboard light's pin as an output. LED_BUILTIN is the framework's name for this board's onboard LED connection, and OUTPUT is its output-mode name. The comma separates two arguments. A semicolon ends this action statement.

A job that runs again. The framework repeatedly calls the function named loop() after each call returns. It is not the same thing as the C++ for or while repetition statements. In lightOn = !lightOn;, ! means logical “not”: true becomes false and false becomes true. “Logical” here means following true/false rules. The assignment stores that new value under the same name.

A choice. A condition is a test producing a true/false answer. if (lightOn) uses the variable as the condition. When it is true, the first braced group calls digitalWrite with HIGH. Otherwise, else selects the second group, which calls it with LOW. HIGH and LOW request digital electrical states on a pin. On this Uno's onboard LED these requests produce on and off; they are not universal meanings for every circuit. delay(1000); waits 1,000 milliseconds, one second. It receives a number as an argument and does not itself change the light.

Trace two calls. At startup lightOn is false. In the first loop() call it becomes true, the on statement runs, and the program waits. The function then returns. In the next call it becomes false, the off statement runs, and the program waits again. The working value persists between calls while this program is running. Restarting starts this example's value at false again.

A comment, text beginning with // in a C++ source line, is a note for readers and does not perform an action. A bug is an error in intended behavior, even when Build succeeds. Debugging means investigating actual behavior to find and correct it. Trace values and choices, compare observed timing and read tool messages before changing several things. Do not memorize this page: recognize the jobs. The first lab supplies the setup; C++ explains more language rules. The external LED example uses a simpler straight sequence so you can concentrate on wiring.