Code Along - Chapter 1

Sketch Structure & Serial Output

Write your first sketches: setup() and loop(), messages in the Serial Monitor, an LED you switch on and blink, and reading compiler errors.

Estimated time 60-75 minExercises passed 0 / 8

Every sketch has two parts

When the Uno powers up or is reset, it runs setup() once, then runs loop() over and over for as long as it has power. Code that prepares the board - pin modes, Serial.begin(9600) - goes in setup(). The work the board repeats goes in loop().

The Serial Monitor is your window into the running sketch. Serial.begin(9600) opens the connection at 9600 baud, and Serial.println(...) sends a line of text to it.

Reset starts setup once, then loop repeats
setup() runs once after reset; loop() repeats forever.
Worked example

Hello, Serial Monitor

Press Run. The first two lines appear once; the loop line appears every second until you press Stop.

void setup() {
  Serial.begin(9600);
  Serial.println("Hello, Uno!");
  Serial.println("setup() runs once.");
}

void loop() {
  Serial.println("loop() runs again and again...");
  delay(1000);
}

Turn on JavaScript to edit, run and test this code in the browser.

Expected output
Hello, Uno!
setup() runs once.
loop() runs again and again...
loop() runs again and again...
Exercise 1.1

Hello, Uno

Not started

Make the sketch print exactly two lines, once, when the board starts:

  • Hello, Uno!
  • Ready to code.

Text must match exactly - capital letters, spaces and punctuation included.

Sample output
Hello, Uno!
Ready to code.
void setup() {
  Serial.begin(9600);
  // Print the two lines here

}

void loop() {
}

Turn on JavaScript to edit, run and test this code in the browser.

Worked example

print or println?

Serial.print stays on the same line; Serial.println finishes the line. Numbers print without quotes.

void setup() {
  Serial.begin(9600);
  Serial.print("A");
  Serial.print("B");
  Serial.println("C");
  Serial.println("D");
  Serial.print("Total: ");
  Serial.println(42);
}

void loop() {
}

Turn on JavaScript to edit, run and test this code in the browser.

Expected output
ABC
D
Total: 42
Exercise 1.2

Wiring label

Not started

Print this three-line label for your breadboard, once.

Build the middle line from three Serial.print calls: the * at the start, the word WILTEQ, and * at the end - then finish the line.

Sample output
**********
* WILTEQ *
**********
void setup() {
  Serial.begin(9600);
  // Top line

  // Middle line - three prints, then end the line

  // Bottom line

}

void loop() {
}

Turn on JavaScript to edit, run and test this code in the browser.

Reading compiler errors

When a sketch will not compile, the Arduino IDE shows an error with a line number. Fix the first error first - later errors are often caused by it.

  • expected ';' before ... - a semicolon is missing, usually at the end of the line above.
  • 'Something' was not declared in this scope - a spelling or capital-letter mistake. serial and Serial are different names.
  • expected '}' at end of input - a { was opened and never closed.

The simulator uses the same wording and adds a plain-English hint under each error.

Four cards for case sensitivity, semicolons, braces and comments
Case, semicolons, braces, comments. Fix the first error, then compile again.
Exercise 1.3

Fix the three bugs

Not started

This sketch has three mistakes, so it does not compile. Press Run to see the first error, fix it, and repeat until the sketch prints the two lines in the sample. Do not change the text in quotes.

Sample output
Fixed!
All three bugs found.
void setup() {
  Serial.begin(9600)
  Serial.printLn("Fixed!");
  Serial.println("All three bugs found.");

void loop() {
}

Turn on JavaScript to edit, run and test this code in the browser.

Switching a pin on and off

A digital pin is either HIGH (about 5 V) or LOW (0 V). Two functions do the work:

  • pinMode(pin, OUTPUT) tells the Uno that the pin will drive something. It belongs in setup().
  • digitalWrite(pin, HIGH) or digitalWrite(pin, LOW) switches it.

LED_BUILTIN is the LED soldered onto the board, on pin 13. delay(1000) waits one second before the next line runs.

Exercise 1.4

Switch the LED on

Not started

Switch the onboard LED (LED_BUILTIN) on and leave it on. Two lines in setup(): make the pin an output, then drive it HIGH. Leave loop() empty.

void setup() {
  // Make LED_BUILTIN an output, then switch it on

}

void loop() {
}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 1.7

Flash an S in Morse code

Not started

The letter S in Morse code is three short flashes. The first flash is written for you: 200 ms on, 200 ms off. Add the second and third flashes, then keep the LED off for a full 1 second before the pattern repeats.

The test measures the timing, so the delays must be exact.

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

void loop() {
  // Flash 1 - done for you
  digitalWrite(LED_BUILTIN, HIGH);
  delay(200);
  digitalWrite(LED_BUILTIN, LOW);
  delay(200);

  // Flash 2 and flash 3 go here


  // Then keep the LED off for 1 second in total

}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 1.8

One number, three ways

DeeperNot started

Datasheets give register values in hexadecimal and bit patterns in binary. Serial.println can show a number in another base: Serial.println(42, HEX).

Print 42 in decimal, hexadecimal and binary, then the character 'A', then the code of 'A' using DEC. Let Serial do the converting.

Sample output
42
2A
101010
A
65
void setup() {
  Serial.begin(9600);
  // 42 in decimal, hex and binary

  // 'A' as a character, then as a number

}

void loop() {
}

Turn on JavaScript to edit, run and test this code in the browser.

Try it on a real Uno

Download Add a Serial trace, upload it to your Uno and open the Serial Monitor at 9600 baud. The LED and the ON/OFF lines should stay in step. Change both delays to 250 and upload again - Serial debugging lets you confirm the timing without a stopwatch.