Programming Code
Single IR Sensor Line Follower Robot
🎯 Lesson Objective
In this lesson, students will learn:
• How to program the Single IR Line Follower Robot
• How the ESP32 reads signals from the IR sensor
• How the ESP32 controls motors using the L298N motor driver
• How the robot changes direction based on the sensor input
By the end of this lesson, students will be able to upload the program and run the Single IR Sensor Line Follower Robot.
1️⃣ Program Logic
The robot follows a simple decision-making process.
The IR sensor continuously checks the surface under the robot.
Robot behavior:
| Sensor Detection | Robot Movement |
|---|---|
| Black Line | Robot turns LEFT |
| White Surface | Robot turns RIGHT |
This continuous adjustment allows the robot to remain close to the line.
2️⃣ Programming Code
int IR_SENSOR = 34;
int motor1A = 26;
int motor1B = 27;
int motor2A = 14;
int motor2B = 12;
void setup()
{
pinMode(IR_SENSOR, INPUT);
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
}
void loop()
{
int sensor = digitalRead(IR_SENSOR);
if(sensor == LOW) // Black Line Detected
{
turnLeft();
}
else
{
turnRight();
}
}
void turnLeft()
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}
void turnRight()
{
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
}
3️⃣ Code Explanation
IR Sensor Pin
This variable stores the GPIO pin connected to the IR sensor output.
The ESP32 reads this pin to determine whether the robot is on the line.
Motor Control Pins
int motor1B = 27;
int motor2A = 14;
int motor2B = 12;
These pins are connected to the L298N motor driver inputs.
Motor driver control:
| Pin | Function |
|---|---|
| motor1A | Left Motor Control |
| motor1B | Left Motor Control |
| motor2A | Right Motor Control |
| motor2B | Right Motor Control |
These pins determine motor direction and movement.
4️⃣ Setup Function
{
pinMode(IR_SENSOR, INPUT);
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
}
The setup() function runs once when the ESP32 starts.
This section configures:
• IR sensor pin as INPUT
• Motor driver pins as OUTPUT
This prepares the ESP32 to read sensor data and control motors.
5️⃣ Loop Function
The ESP32 reads the signal from the IR sensor.
Possible outputs:
| Sensor Output | Meaning |
|---|---|
| LOW | Black line detected |
| HIGH | White surface detected |
6️⃣ Decision Logic
{
turnLeft();
}
else
{
turnRight();
}
The robot decides how to move based on the sensor value.
• If the black line is detected, the robot turns left.
• If the white surface is detected, the robot turns right.
This constant checking allows the robot to follow the line.
7️⃣ Turn Left Function
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}
In this condition:
• Left motor stops
• Right motor moves forward
This causes the robot to turn left toward the line.
8️⃣ Turn Right Function
{
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
}
In this condition:
• Right motor stops
• Left motor moves forward
This causes the robot to turn right to search for the line.
9️⃣ How the Robot Follows the Line
The robot repeats this process continuously:
1️⃣ The IR sensor scans the surface.
2️⃣ The ESP32 reads the sensor value.
3️⃣ The program decides the movement direction.
4️⃣ The motor driver receives control signals.
5️⃣ The motors move the robot.
This continuous adjustment allows the robot to follow the path.
🚀 What Happens Next
Now that the robot program has been uploaded, the next step is to test the robot on a line track and observe its behavior.