๐ด Unit 1: Turn On the Red LED
๐ง What You Will Learn
In this unit, you will learn how to turn on an LED using code. Along the way, youโll understand what an output is, and learn two important commands:
-
pinMode() โ for setting up a pin as output.
-
digitalWrite() โ for turning a pin on or off.
๐ฏ Objective
By the end of this unit, you will be able to:
-
Understand the meaning of the term output
-
Use
pinMode()to set a pin as an output -
Use
digitalWrite()to turn an output pin on -
Recognize the purpose of curly braces { } in a sketch
-
Know why we add a semicolon
;at the end of each instruction
๐งฉ Used Component
- Red LED (connected to pin D11)
๐ ๏ธ Step-by-Step Instructions
โ Step 1: Open the Arduino IDE
Connect your Waliduino via USB and launch the Arduino IDE.
โ Step 2: Create a new sketch
Delete any existing code and enter the following:
void setup() {
pinMode(11, OUTPUT);
digitalWrite(11, HIGH);
}
void loop() {
}
โ Step 3: Ensure your board and port are correct
-
Tools > Board โ select Arduino Nano
-
Tools > Port โ choose the port your board is on (e.g., COM3 or COM4)
โ Step 4: Upload the sketch
Click the Upload button (โ).
If everything is correct, the red LED turns on! ๐ด
๐กLetโs break this sketch down step by step:
๐ ๏ธ pinMode(11, OUTPUT);
This line tells the Arduino that pin 11 will be used as an output.
That means the pin is prepared to control something, like an LED.
โก digitalWrite(11, HIGH);
This line sets pin 11 to 5 volts (HIGH).
Since the red LED is connected to that pin, the voltage makes the LED light up.
๐ง Why is the code only in setup()?
We only want the LED to turn on once, right when the program starts.
The setup() function runs only one time, so itโs perfect for actions that donโt need to be repeated.
๐ What does loop() do here?
Nothing โ itโs empty for now.
But it must still be included, because every Arduino sketch requires both setup() and loop().
๐ฃ What are the { } braces for?
Curly braces { } group instructions that belong together.
All the code inside setup() goes between its braces, and the same for loop().
๐ Why do we need semicolons ;?
Each command in Arduino must end with a semicolon ;.
It tells the Arduino: โThis line is complete.โ
โ Forgetting a semicolon will cause an error when you try to upload your sketch.
๐ How the LED is wired and why we use a Resistor
When you turn on an LED with the Arduino, youโre actually creating a small electrical circuit. Hereโs how itโs connected:
โก๏ธ Arduino Pin โ Resistor โ LED โ GND (Ground)
Letโs break this down:
๐ด 1. The Arduino Pin
When you use digitalWrite(11, HIGH), the Arduino pin sends out 5 volts โ like turning on a tiny switch that allows current to flow.
๐งฑ 2. The Resistor
Before the current reaches the LED, it flows through a resistor (typically 220 or 330 ohms).
๐ Why do we need it?
Without the resistor, too much current would flow into the LED โ which can burn it out or damage the Arduino. The resistor slows down the current to a safe level.
๐ก 3. The LED
After passing through the resistor, the current reaches the LED, making it light up.
๐ New Terms
-
Output: A pin that sends signals or power (e.g., to turn on an LED or motor)
-
pinMode(pin, OUTPUT): Tells the Arduino to use a specific pin as output -
digitalWrite(pin, HIGH): Sets the voltage at the pin to HIGH (5V) โ turning the connected device on
What We Have Learned
-
Every Arduino sketch must have two main parts:
setup()andloop() -
pinMode() sets a pin as either input or output
-
digitalWrite() turns a pin ON (
HIGH) or OFF (LOW) -
An output is a pin that will be setted as HIGH or LOW
-
Curly braces
{ }are used to group code blocks.- For example: everything inside
setup()must be inside{ }
- For example: everything inside
-
Semicolons
;mark the end of each command- If you forget them, the code wonโt compile or will cause errors