💡 Uploading First Program (LED Blink)
🎯 Lesson Objective
In this lesson, students will understand:
• How to write a simple program in Arduino IDE
• How to upload a program to the ESP32 board
• How the ESP32 controls hardware components
• The basic structure of an embedded program
This lesson introduces the first practical programming example using ESP32.
1️⃣ Why the LED Blink Program is Important
The LED Blink program is traditionally the first program written when learning to program microcontrollers.
It is a simple program where an LED turns ON and OFF repeatedly.
Although it is a simple example, it teaches several important concepts:
💻 Writing a basic program
⚙️ Uploading code to a microcontroller
🔌 Controlling hardware using software
🔁 Understanding program execution
Running this program successfully confirms that:
✔ Arduino IDE is properly configured
✔ The ESP32 board is connected correctly
✔ Programs can be uploaded to the microcontroller
2️⃣ Understanding the Program Structure
Programs written in Arduino IDE are called sketches.
Every Arduino or ESP32 program contains two important functions:
setup()
The setup() function runs only once when the program starts.
It is used to configure hardware components such as:
• Defining pin modes
• Starting serial communication
• Initializing sensors
loop()
The loop() function runs continuously after the setup() function finishes.
This function contains the main logic of the program.
The instructions inside the loop() function repeat over and over while the board is powered.
3️⃣ Writing the LED Blink Program
Now we will write a simple program that makes an LED blink.
Steps:
1️⃣ Open Arduino IDE.
2️⃣ Create a new sketch.
3️⃣ Write the LED blink program in the code editor.
The program will:
• Turn the LED ON
• Wait for a short delay
• Turn the LED OFF
• Wait again
• Repeat the process continuously
This repeated cycle creates the blinking effect.
LED Blink code for ESP32
// LED Blink Program for ESP32
int ledPin = 2; // GPIO pin where the LED is connected
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn LED ON
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn LED OFF
delay(1000); // Wait for 1 second
}
Code Explanation
-
int ledPin = 2;
Defines the GPIO pin where the LED is connected. On most ESP32 boards the built-in LED is on GPIO 2. -
pinMode(ledPin, OUTPUT);
Sets the LED pin as an output so the ESP32 can send signals to it. -
digitalWrite(ledPin, HIGH);
Turns the LED ON. -
digitalWrite(ledPin, LOW);
Turns the LED OFF. -
delay(1000);
Waits for 1000 milliseconds (1 second).
The program runs inside the loop() function, so the LED continues blinking repeatedly.
4️⃣ Uploading the Program to ESP32
After writing the program, the next step is to upload it to the ESP32 board.
Follow these steps:
1️⃣ Connect the ESP32 board to the computer using a USB cable.
2️⃣ In Arduino IDE, select the correct board.
Go to:
Tools → Board → ESP32 Dev Module
3️⃣ Select the correct COM port.
Go to:
Tools → Port → Select the ESP32 Port
4️⃣ Click the Upload button in the Arduino IDE.
The IDE will compile the program and upload it to the ESP32.
During the upload process, you may see messages in the output window showing the progress.
Once the upload is complete, the program will start running automatically.
5️⃣ Observing the LED Blink
After the program is uploaded successfully, you should observe the LED blinking on the ESP32 board.
The LED will:
💡 Turn ON
⏳ Wait for a short delay
💡 Turn OFF
⏳ Wait again
This cycle continues repeatedly.
This confirms that the ESP32 is successfully running the uploaded program.
6️⃣ Why This First Program Matters
Although the LED Blink program is simple, it is an important milestone.
It demonstrates the complete workflow of embedded programming:
📝 Writing code
⚙️ Compiling the program
📡 Uploading the program
🔌 Controlling hardware
Understanding this process is essential before moving on to more advanced robotics projects.
🚀 What Happens Next
Now that you have successfully uploaded your first program to the ESP32, you are ready to explore the hardware components used in robotics systems.