Arduino lesson 1: setup(), loop() and Serial Monitor
How an Arduino sketch works: setup() runs once, loop() repeats, and Serial helps you debug. Lesson 1 of the Arduino C course, with 12 exercises and answers.
Getting started
Home Assistant
Buying
How an Arduino sketch works: setup() runs once, loop() repeats, and Serial helps you debug. Lesson 1 of the Arduino C course, with 12 exercises and answers.


AI-generated illustration
Quick answer (November 2026) — An Arduino sketch is a small C/C++ program with setup() (runs once at boot) and loop() (repeats forever). Use Serial (UART text to your PC) like print() in Python to see what the board is doing.
This is lesson 1 of our Arduino course; it works on any UNO-compatible board, including kits in our Arduino robot kit guide and best programmable robot.

Arduino C is C/C++ adapted to microcontrollers. In robotics the idea is simple: your program runs in a loop on the board, reads sensors and drives actuators (LEDs, motors, buzzer).
In this lesson you will learn:
setup() / loop());On a Makeblock mBot (mCore board) programmed in Arduino C through mBlock, the logic is the same, with Makeblock’s libraries on top.
setup() and loop()Almost every Arduino sketch contains:
setup(): runs once at start-up;loop(): runs again and again.board starts
└── setup() (once)
└── loop() (repeated forever)
void setup() {
// runs once
}
void loop() {
// runs forever
}
Make small changes, compile often and test step by step. The Arduino IDE is free for Windows, macOS and Linux.
SerialSerial prints messages to the computer (the Serial Monitor). It is the Arduino equivalent of Python’s print().
void setup() {
Serial.begin(9600);
Serial.println("Hello!");
}
void loop() {
Serial.println("Running");
delay(1000);
}
Serial.begin(...) in setup(), and pick the same speed in the Serial Monitor.delay(...) or, later, use millis().counter, distance_cm, state.const for constants: const int LED_PIN = 13;9600, 13, 1000) scattered in the code: give them a named constant.Solutions are hidden: click Show solution.
Exercise 1: Write an empty sketch that compiles: empty setup() and loop(). Easy
void setup() {}
void loop() {}
Exercise 2: Start Serial at 9600 baud in setup(). Easy
void setup() {
Serial.begin(9600);
}
void loop() {}
Exercise 3: Print Hello once at start-up. Easy
void setup() {
Serial.begin(9600);
Serial.println("Hello");
}
void loop() {}
Exercise 4: Print Running every 2 seconds in loop(). Easy
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Running");
delay(2000);
}
Exercise 5: Declare a constant const int BAUD = 9600; and use it. Intermediate
const int BAUD = 9600;
void setup() { Serial.begin(BAUD); } void loop() {}
Exercise 6: Create a global int counter = 0;, print it every second, then increment it. Intermediate
int counter = 0;void setup() { Serial.begin(9600); }
void loop() { Serial.println(counter); counter++; delay(1000); }
Exercise 7: Print counter= followed by the value on the same line. Intermediate
int counter = 0;void setup() { Serial.begin(9600); }
void loop() { Serial.print(“counter=”); Serial.println(counter); counter++; delay(1000); }
Exercise 8: Count from 0 to 9 only, then print END and stop (deliberate infinite loop). Intermediate
int counter = 0;void setup() { Serial.begin(9600); }
void loop() { if (counter <= 9) { Serial.println(counter); counter++; delay(500); } else { Serial.println(“END”); while (true) { // stop } } }
Exercise 9: Print millis() (milliseconds since start-up) every second. Advanced
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(millis());
delay(1000);
}
Exercise 10: Without delay, print READY once after 3 seconds using millis() (hint: store the start time). Advanced
unsigned long t0 = 0; bool shown = false;void setup() { Serial.begin(9600); t0 = millis(); }
void loop() { if (!shown && (millis() - t0 >= 3000)) { Serial.println(“READY”); shown = true; } }
Exercise 11: Write a function void logValue(const char* name, int v) that prints name=v. Advanced
void logValue(const char* name, int v) { Serial.print(name); Serial.print('='); Serial.println(v); }
void setup() { Serial.begin(9600); logValue(“score”, 42); } void loop() {}
Exercise 12: Explain Serial.print vs Serial.println in a comment, then use both. Advanced
void setup() {
Serial.begin(9600);
// print: no line break
// println: adds a line break at the end
Serial.print("A");
Serial.print("B");
Serial.println("C"); // end of line here
}
void loop() {}
Next: Lesson 2: types, variables and constants → · Back to the course hub
What baud rate for Serial?
9600 is fine for lessons; match Serial.begin(9600) and the Serial Monitor dropdown.
Verify vs Upload?
Verify compiles only; Upload sends the binary to the board—compile often, upload when ready.
Same code on mBot mCore?
Logic is identical; mBlock may wrap Makeblock libraries—see mBot vs mBot2.
Was this page helpful?
Thank you!
What should we improve?
Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.
Cookies and analytics
We use Google Analytics 4 to understand how the site is used (page views, navigation). It is only switched on if you accept. Declining does not stop you using the site. More: cookie settings and privacy policy (in French).