Arduino C++ basics
What you’ll learn
- The shape of every Arduino program (a sketch)
- How to switch pins on and off and read them
- Variables,
ifstatements and printing messages with Serial
Every sketch has two parts
void setup() { // Runs ONCE when the board powers on or resets}
void loop() { // Runs AGAIN and AGAIN, forever}This is the sense, think, act loop in code: loop() is where your robot senses, decides and acts, thousands of times per second.
Blink, line by line
const int LED = 13; // a name for pin 13 (the built-in LED)
void setup() { pinMode(LED, OUTPUT); // pin 13 will SEND signals out}
void loop() { digitalWrite(LED, HIGH); // turn the pin ON (5 V) delay(1000); // wait 1000 milliseconds = 1 second digitalWrite(LED, LOW); // turn the pin OFF (0 V) delay(1000);}| Code | Meaning |
|---|---|
const int LED = 13; |
Make a named number. const means it never changes |
pinMode(pin, OUTPUT) |
Set a pin to send signals (LEDs, buzzers) |
pinMode(pin, INPUT_PULLUP) |
Set a pin to read a button (more below) |
digitalWrite(pin, HIGH/LOW) |
Turn an output pin on or off |
digitalRead(pin) |
Read an input pin: gives HIGH or LOW |
delay(ms) |
Pause for that many milliseconds |
Grammar rules (syntax)
C++ is fussy. Most beginner errors are one of these:
- Every statement ends with a semicolon
; - Code blocks live inside curly braces
{ }, and every{needs a matching} - Names are case-sensitive:
digitalWriteworks,DigitalWritedoesn’t //starts a comment, a note for humans that the board ignores
Variables
A variable is a named box that holds a value that can change.
int count = 0; // whole numbersfloat distance = 12.5; // numbers with decimalsbool pressed = false; // true or falseMaking decisions with if
if (pressed) { digitalWrite(LED, HIGH);} else { digitalWrite(LED, LOW);}Compare with == (equal), != (not equal), <, >, <=, >=. Remember: = stores a value, == compares two values.
Talking to your computer: Serial
Serial lets the board send text to the IDE’s Serial Monitor (magnifying-glass icon, top right). It’s your number-one debugging tool.
int count = 0;
void setup() { Serial.begin(9600); // start talking at 9600 bits per second}
void loop() { Serial.print("Count: "); Serial.println(count); // println adds a new line count = count + 1; delay(500);}Set the Serial Monitor’s speed to 9600 baud to match, or you’ll see gibberish.
Example: a countdown on the Serial Monitor
void setup() { Serial.begin(9600); for (int i = 5; i > 0; i--) { Serial.println(i); delay(1000); } Serial.println("Lift off!");}
void loop() {}Example: counting button presses
const int BUTTON = 2;int presses = 0;bool wasPressed = false;
void setup() { pinMode(BUTTON, INPUT_PULLUP); Serial.begin(9600);}
void loop() { bool pressed = digitalRead(BUTTON) == LOW; if (pressed && !wasPressed) { presses = presses + 1; Serial.print("Presses: "); Serial.println(presses); delay(50); // ignore button bounce } wasPressed = pressed;}Try it
Use the virtual Arduino below (or your real board). Paste the Serial example into sketch.ino and press ▶. The Serial Monitor opens under the board automatically.
Runs on Wokwi, free, no account needed.
- Run the Serial example. Watch the count go up.
- Change
delay(500)todelay(100). What happens? - Add an
ifso it prints"Big number!"whenevercount > 20.
Faster countingEasy
Make the Serial example count twice as fast.
Change delay(500) to delay(250).
Count to ten and stopMedium
Print 1 to 10, then stop printing.
Put the counting in setup(), or check count with an if in loop().
Show an answer
void setup() { Serial.begin(9600); for (int i = 1; i <= 10; i++) Serial.println(i);}void loop() {}Even or oddHard
Print each number from 1 to 20 followed by “even” or “odd”.
n % 2 gives the remainder after dividing by 2.
Show an answer
void setup() { Serial.begin(9600); for (int n = 1; n <= 20; n++) { Serial.print(n); if (n % 2 == 0) Serial.println(" even"); else Serial.println(" odd"); }}void loop() {}Quick check
Finished this lesson?