Course Content
Hands-On ESP32 Robotics: Build Smart Robots Step by Step

🧠 How Microcontrollers Run Programs

🎯 Lesson Objective

In this lesson, students will understand:

• How microcontrollers execute programs
• What happens when a program is uploaded to a microcontroller
• How instructions are processed inside the microcontroller
• The basic working cycle of a microcontroller

This lesson helps students understand how the microcontroller executes the programs we write.


1️⃣ What Happens When We Upload a Program

When we write a program in Arduino IDE, it is written in a programming language that humans can understand.

However, microcontrollers cannot directly understand human-readable code.

Before the program runs on the microcontroller, several steps occur:

1️⃣ The program is compiled by the Arduino IDE.
2️⃣ The compiler converts the code into machine language.
3️⃣ The machine code is uploaded to the memory of the microcontroller.
4️⃣ The microcontroller reads and executes the instructions stored in memory.

Once the program is uploaded successfully, the microcontroller begins executing the instructions automatically.


2️⃣ Microcontroller Memory

A microcontroller contains different types of memory that store data and programs.

💾 Program Memory

Program memory stores the program code that we upload to the microcontroller.

The program remains stored even when the power is turned off.

This memory ensures that the microcontroller runs the same program every time it is powered on.


📂 RAM (Random Access Memory)

RAM is used to store temporary data while the program is running.

Examples include:

• Sensor readings
• Variables used in the program
• Intermediate calculations

RAM is cleared when the microcontroller loses power.


🔐 Flash Memory

Flash memory stores the compiled program that the microcontroller executes.

It allows the program to remain stored permanently until a new program is uploaded.


3️⃣ Microcontroller Processing Cycle

A microcontroller executes programs using a repeating process called the instruction cycle.

The instruction cycle contains three main steps:

📥 Fetch

The microcontroller retrieves the next instruction from program memory.


🧠 Decode

The instruction is interpreted to determine what action needs to be performed.


⚙️ Execute

The microcontroller performs the required action, such as:

• Turning on a pin
• Reading a sensor value
• Sending data to a motor driver

This cycle repeats continuously while the microcontroller is powered.


4️⃣ Continuous Program Execution

Unlike normal computer programs that run once and stop, microcontroller programs typically run continuously.

The program keeps repeating instructions so the system can constantly monitor sensors and control devices.

For example:

• A robot continuously checks for obstacles.
• A temperature system continuously monitors temperature levels.
• A security system continuously monitors motion sensors.

This continuous operation allows embedded systems to respond instantly to environmental changes.


5️⃣ Microcontroller and Hardware Interaction

Microcontrollers interact with hardware through input and output pins.

These pins allow the microcontroller to communicate with sensors and actuators.

📥 Input

Input pins receive signals from sensors or switches.

Examples include:

• Reading distance from an ultrasonic sensor
• Detecting light using an LDR sensor


📤 Output

Output pins send signals to control devices.

Examples include:

• Turning on an LED
• Controlling a motor driver
• Activating a servo motor

By combining input and output operations, the microcontroller controls the entire robotics system.


6️⃣ Example of Program Execution in a Robot

Let’s look at a simple example of how a robot program runs.

1️⃣ The ultrasonic sensor measures the distance in front of the robot.
2️⃣ The ESP32 reads the sensor data.
3️⃣ The program checks if the object is too close.
4️⃣ If the distance is small, the program sends signals to the motor driver.
5️⃣ The motors change direction to avoid the obstacle.

This sequence of instructions repeats continuously while the robot is powered.


🚀 What Happens Next

Now that you understand how microcontrollers run programs and execute instructions, the next step is to learn about the basic structure of programs written in Arduino IDE.

Scroll to Top