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

📥 Reading Sensor Data

🎯 Lesson Objective

In this lesson, students will understand:

• How microcontrollers read data from sensors
• The difference between digital sensor data and analog sensor data
• How sensor values are stored in variables
• How sensor data is used in robotics programs
• How sensor readings help robots interact with the environment

This lesson explains how the microcontroller receives information from sensors and processes it in a program.


1️⃣ Why Reading Sensor Data is Important

Sensors allow robots to observe and understand their environment.

However, the sensor itself only detects physical changes such as:

• Distance
• Light intensity
• Temperature
• Motion
• Fire or gas

To use this information, the microcontroller must read the sensor signals and convert them into data that the program can process.

This sensor data is then used by the robot to make decisions.

For example:

• If an obstacle is detected → the robot turns.
• If light intensity increases → the robot changes direction.
• If fire is detected → the robot activates a safety mechanism.


2️⃣ How Sensors Send Data to the Microcontroller

Sensors communicate with the microcontroller through input pins.

There are two main types of sensor signals:

🔌 Digital signals
📊 Analog signals

Each type of signal is read differently by the microcontroller.


3️⃣ Reading Digital Sensor Data

Digital sensors produce signals that have only two possible states:

• HIGH (1)
• LOW (0)

Examples of digital sensors include:

• IR sensors
• Limit switches
• Flame sensors (digital output)

Digital signals are read using the digitalRead() function.

Example:

 
int sensorValue = digitalRead(sensorPin);
 

Explanation:

• The microcontroller reads the signal from the sensor pin.
• The value will be either HIGH or LOW.
• The value is stored in a variable.

The program can then use this value to perform actions.


4️⃣ Reading Analog Sensor Data

Analog sensors produce varying voltage signals depending on environmental conditions.

Examples of analog sensors include:

• LDR light sensors
• Temperature sensors
• Soil moisture sensors

Analog signals are read using the analogRead() function.

Example:

 
int sensorValue = analogRead(sensorPin);
 

The returned value usually ranges between:

0 to 4095 (for ESP32).

This value represents the strength of the sensor signal.


5️⃣ Storing Sensor Values in Variables

After reading sensor data, the value is usually stored in a variable.

Example:

 
int distance = analogRead(sensorPin);
 

Now the variable distance contains the sensor value.

The program can use this value to perform calculations or make decisions.

For example:

 
if (distance < 20) {
stopRobot();
}
 

This allows the robot to respond to environmental conditions.


6️⃣ Displaying Sensor Data Using Serial Monitor

During development, sensor data is often displayed using the Serial Monitor.

Example:

Serial.println(sensorValue);

This allows developers to observe the sensor readings in real time.

Monitoring sensor values helps verify that the sensor is working correctly.


7️⃣ Using Sensor Data in Robotics

Once the microcontroller reads sensor values, the program can use the data to control the robot.

Examples include:

🚧 Obstacle avoiding robots
🛤 Line follower robots
💡 Light-sensitive robots
🔥 Fire detection robots

For example:

1️⃣ The sensor detects an obstacle.
2️⃣ The microcontroller reads the sensor value.
3️⃣ The program checks the value using a condition.
4️⃣ The robot changes its movement accordingly.

This process allows robots to react intelligently to their surroundings.


8️⃣ Sensor Data Flow in a Robot

The general process of using sensor data in robotics is:

1️⃣ The sensor detects a physical condition.
2️⃣ The sensor sends a signal to the microcontroller.
3️⃣ The microcontroller reads the signal.
4️⃣ The program processes the data.
5️⃣ The robot performs an action.

This cycle repeats continuously while the robot is operating.


🚀 What Happens Next

Now that you understand how sensor data is read and processed by the microcontroller, the next step is to learn how programs use this information to control motors and movement.

Scroll to Top