π‘ 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.