Your First Sketch - Blink LED

Your First Sketch - Blink LED

Blink LED

Understanding Arduino Sketches

An Arduino program is called a sketch. Every sketch has two main functions:

1. setup() - Runs once when the board starts 2. loop() - Runs repeatedly forever

The Blink Sketch

This is the classic first Arduino project - making an LED blink.

Code

// Pin 13 has an LED connected on most Arduino boards
int led = 13;

void setup() { // Initialize the digital pin as an output pinMode(led, OUTPUT); }

void loop() { digitalWrite(led, HIGH); // Turn the LED on (HIGH is voltage level) delay(1000); // Wait for 1000 milliseconds (1 second) digitalWrite(led, LOW); // Turn the LED off (LOW is voltage level) delay(1000); // Wait for 1 second }

Understanding the Code

pinMode(pin, mode)

Configures a pin to behave as input or output:

pinMode(led, OUTPUT);   // Pin 13 as output
pinMode(sensorPin, INPUT); // Pin as input

digitalWrite(pin, value)

Sets output voltage on a digital pin:

digitalWrite(led, HIGH);  // Set voltage to 5V (LED on)
digitalWrite(led, LOW);   // Set voltage to 0V (LED off)

delay(milliseconds)

Pauses the program for specified time:

delay(1000);   // Wait 1 second
delay(500);    // Wait half a second
delay(100);    // Wait 0.1 second

Uploading the Sketch

Step 1: Connect Arduino

Connect your Arduino to the computer via USB.

Step 2: Open Blink Example

1. In Arduino IDE, go to File > Examples > 01.Basics > Blink 2. A new window opens with the Blink code

Step 3: Select Board and Port

1. Tools > Board > Arduino AVR Boards > Arduino Uno 2. Tools > Port > Select your Arduino port

Step 4: Upload

1. Click the Upload button (right arrow) 2. Wait for "Done uploading" message 3. Watch the LED on pin 13 blink!

Modifying the Blink

Change Blink Speed

Change the delay times:

delay(500);   // Faster blinking
delay(200);   // Very fast
delay(3000);  // Very slow (3 seconds)

Use External LED

Connect an external LED:

1. Connect LED long leg (anode) to pin 13 2. Connect LED short leg (cathode) to GND through 220 ohm resistor 3. Upload the sketch

Circuit Connection

Arduino Pin 13 ---[220Ω LED]--- GND

Built-in LED

Arduino Uno has a built-in LED connected to pin 13. This saves you from needing external components for your first project.

LED Polarity

LegNameDescription
LongerAnode (+)Positive, connect to output pin
ShorterCathode (-)Negative, connect to GND

Practice Exercise

Try modifying the sketch to create different patterns:

SOS Pattern

void loop() {
  // S: three short
  digitalWrite(led, HIGH); delay(200);
  digitalWrite(led, LOW);  delay(200);
  digitalWrite(led, HIGH); delay(200);
  digitalWrite(led, LOW);  delay(200);
  digitalWrite(led, HIGH); delay(200);
  digitalWrite(led, LOW);  delay(600);
  
  // O: three long
  digitalWrite(led, HIGH); delay(600);
  digitalWrite(led, LOW);  delay(200);
  digitalWrite(led, HIGH); delay(600);
  digitalWrite(led, LOW);  delay(200);
  digitalWrite(led, HIGH); delay(600);
  digitalWrite(led, LOW);  delay(600);
  
  // S: three short
  digitalWrite(led, HIGH); delay(200);
  digitalWrite(led, LOW);  delay(200);
  digitalWrite(led, HIGH); delay(200);
  digitalWrite(led, LOW);  delay(200);
  digitalWrite(led, HIGH); delay(200);
  digitalWrite(led, LOW);  delay(1000);
}

Summary

You've written and uploaded your first Arduino sketch!

  • setup() configures the pin
  • loop() runs repeatedly
  • digitalWrite controls output
  • delay creates timing
This is the foundation for all Arduino projects.

Next Lesson

In the next lesson, you'll learn about digital inputs and outputs - reading switches and controlling more devices.

Quiz - Quiz - Your First Sketch

1. How many milliseconds is delay(1000)?

2. Which function sets a pin to HIGH or LOW?

3. What are the two main functions in every Arduino sketch?

4. How do you make an LED blink faster?

Installing Arduino IDE on Windows