Find a course

Module 1: Getting Started with ArduinoLesson 2 of 1214 min

Install the IDE and Upload Your First Sketch

Set up the Arduino IDE, choose your board and port, understand setup() and loop(), and upload the classic Blink sketch to your board.

Step 1 of 11Learn

Ten minutes from box to blinking light

Maya opens her new Uno, plugs it in, and the little LED next to pin 13 already blinks. That's the factory test program. Her goal for the evening is to replace it with code she wrote herself, and change the blink speed to prove it's really hers.

You need three things:

  1. The Arduino IDE (integrated development environment), free from arduino.cc for Windows, macOS and Linux. There's also a browser-based editor if you can't install software.
  2. A USB cable that carries data. Many cheap cables only carry power; if your computer doesn't detect the board, try another cable first.
  3. The board itself. Clones of the Uno often use a different USB chip (commonly the CH340), which may need a driver on older systems.

In the IDE, open Tools > Board and pick Arduino Uno, then Tools > Port and pick the port that appeared when you plugged it in (COM3, COM4 and so on on Windows, /dev/cu.usbmodem… on a Mac). If you're not sure which port is the board, unplug it, look at the list, plug it back in and see which entry is new.

Step 2 of 11Practice · Put in order

Put the steps for uploading your first program in the right order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Install the Arduino IDE
  2. Select the port the board appeared on
  3. Connect the board with a data-capable USB cable
  4. Select the board type under Tools > Board
  5. Click Upload and wait for "Done uploading"

Step 3 of 11Learn

The shape of every sketch

An Arduino program is called a sketch, and it's written in C++ with some helpful built-in functions. Every sketch has two required functions:

void setup() {
  // runs once, when the board powers up or is reset
}

void loop() {
  // runs over and over, forever
}

setup() is where you prepare: tell the chip which pins are outputs, start serial communication, set starting values. loop() is the program's heartbeat. When its last line finishes, it starts again from the top, thousands of times per second if nothing slows it down.

There is no "end" to an Arduino program. Your code is the only thing the chip runs, and it keeps running until power is removed. Hidden behind the scenes, the Arduino core provides a main() function that calls setup() once and then loop() in an endless cycle.

A few C++ basics you'll see everywhere: statements end with a semicolon, code blocks sit inside curly braces, and // starts a comment the compiler ignores. Names are case sensitive: pinMode works, pinmode does not.

Sketch flow: setup runs once at power-up, then loop repeats foreverPower onsetup()loop()runs oncerepeats forever

Step 4 of 11Practice · Fill in the blanks

Fill in the blanks

Code inside runs once when the board starts, while code inside repeats for as long as the board has power.

Step 5 of 11Learn

Blink, line by line

Here is Blink, the "hello world" of hardware:

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

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}
  • pinMode(LED_BUILTIN, OUTPUT) makes the built-in LED pin (pin 13 on the Uno) an output.
  • digitalWrite(..., HIGH) sets that pin to 5 V, so the LED lights.
  • delay(1000) pauses for 1000 milliseconds, which is one second.
  • The LED is switched LOW (off) and the program waits another second.
  • Then loop() starts again.

So the LED is on for 1 s and off for 1 s: one full blink every 2 seconds. Change both delays to 250 and it blinks four times faster, twice per second.

When you click Upload, the IDE first compiles your C++ into machine code, then sends it to the board through a small program called the bootloader. The onboard RX/TX LEDs flicker during the transfer, and the IDE shows "Done uploading" when it's finished.

Step 6 of 11Try the calculator

Blink timing calculator

Change the on and off delays to see how fast the LED blinks.

One full blink cycle
2,000 ms
Blinks per second
0.5 Hz
Blinks per minute
30
Share of time the LED is lit
50 %

Step 7 of 11Tap to reveal

IDE features worth knowing on day one

  1. Verify (check mark)

    Compiles your code without uploading. Use it to catch typos before you even plug in the board.

  2. Upload (arrow)

    Compiles and sends the program to the board. The status bar shows how much flash and SRAM your sketch uses; keep an eye on SRAM, because the Uno R3 has only 2 KB.

  3. File > Examples

    Dozens of ready-made, tested sketches, from Blink to servo control. Opening an example is the fastest way to check that your board and wiring work.

  4. Tools > Auto Format

    Re-indents your code so matching braces line up, which makes missing or extra braces easy to spot.

  5. Library Manager

    Installs code libraries for sensors, displays and motors with one click, so you don't have to write low-level drivers yourself.

Step 8 of 11Practice · Work it out

You change Blink so the LED stays on for 200 ms and off for 300 ms. How many times does it blink per second?

Step 9 of 11Tap to reveal

When the upload fails: check these in order

  1. "Port not found" or the Port menu is greyed out

    The computer can't see the board. Try a different USB cable (many are charge-only), another USB socket, and on clone boards install the CH340 driver.

  2. "Programmer is not responding" or a sync error

    Usually the wrong board or wrong port is selected, or something is connected to pins 0 and 1. Re-check Tools > Board and Tools > Port and unplug wires from pins 0 and 1.

  3. A compile error in red

    The code never reached the board. Read the first error message: it names the line. Missing semicolons, unmatched braces and wrong capitalization cause most of them.

  4. Upload succeeds but nothing happens

    Check that your code actually targets the pin you wired, that pinMode is set, and that an external LED is the right way around.

Step 10 of 11Practice · Spot the mistake

This Blink sketch fails to compile. Click the line with the mistake.

Step 11 of 11Practice · What would you do?

What would you do?

Jamal uploads a sketch that sets pin 13 HIGH in setup() and has an empty loop(). The LED turns on and stays on. He expected nothing to happen because "loop is empty". Who is right?