Sub topic 3
Opening and reading the Blink sketch
This chapter assumes you have the Arduino IDE installed, an Arduino Uno or compatible board, a USB data cable, and basic familiarity with C-style syntax. A sketch is the name the IDE gives to an Arduino program.
Opening the Blink sketch
In the IDE, choose File > Examples > 01.Basics > Blink. The sketch contains two functions:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}setup()
The board runs setup() once after power-up or reset. Here, pinMode() configures the built-in LED pin as an output.
loop()
The board runs loop() repeatedly after setup() finishes. digitalWrite() sets the pin HIGH (on) or LOW (off). delay() pauses for 1000 milliseconds, so the LED blinks once per second.
Verifying and uploading
Click Verify (the checkmark) to compile the sketch and catch errors without touching the board. Click Upload (the arrow) to compile and send the program over USB. The console pane reports progress and errors.
Printing to the Serial Monitor
Serial output sends text from the board to your computer. This sketch prints one line per second:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Hello, Arduino!");
delay(1000);
}Open Tools > Serial Monitor and set the baud rate to 9600. The rate in the monitor must match the value passed to Serial.begin().
Troubleshooting common problems
- No port listed: Try another USB cable. Some cables carry power only and no data.
- Wrong board selected: The upload fails with a sync or timeout error. Check Tools > Board.
- Port busy: Close the Serial Monitor before uploading, because it holds the port.
- Clone board fails: Try Tools > Processor > ATmega328P (Old Bootloader).
Adding libraries with the Library Manager
A library is a reusable code package. Open the Library Manager (the books icon in IDE 2, or Sketch > Include Library > Manage Libraries in IDE 1), search for a name, and click Install. Then add #include <LibraryName.h> at the top of your sketch.
Exercise
Change the Blink sketch so the LED stays on for 250 milliseconds and off for 750 milliseconds. Which lines change, and which function controls the timing?
Show answer
Change the first delay() to 250 and the second to 750. delay() controls the timing, while digitalWrite() still sets the pin state.