Hello, friends how are you? i hope your are great so, today i am going teach you how you can make Esp32 based line follower robot? in just 30 min. so first of all you know about line follower robot so it means it's one type of machine that follow the white lines or black line in loop way. when you provide the power supply continuously. because it have one micro controller and IR sensor which perform the task which user or developer gives the command with the help of coding.
To build esp32 based line follower robot so you will need some basic knowledge of Arduino and Esp32 the estimated build time for this project is 2-3 hours with beginner or intermediate but it depends on skill level and the total cost for this project will something around between $22 to $30.
Now lets, start building robot without any wasting time so are you ready if yes, then followed my tutorial i will very simple explain in easiest way.
Here is Components list to Build a Line Follower Robot:
1. 3D printed or acrylic chassis
2. Castor wheel x 1
3. Rubber wheel (for N20 motor) x 2
4. Esp32 development board x 1
5. Motor driver (DRV8833 2 Channel DC or L298n Motor Driver) x1
6. Few male female jumper wire
7. On/off switch x 1
8. 7.4v lithium battery
9. Vero board x 1
10. Screws
In addition to the components mentioned earlier, you'll also need a few tools, such as a soldering iron and solder, a wire stripper, a screwdriver set, and a hot glue gun. It's also recommended to have some extra components on hand, like a multimeter for measuring voltage, a toggle switch, and an extra IR sensor module, in case you need to troubleshoot the robot later.
Working Principle of Line follower robot:
Before you create this type of robot, you should first understand line-following robots (LFRs). An LFR navigates by following a line, and to do this, it must first detect the line. The question is how to implement the line-sensing mechanism. We know that light reflects most strongly from a white surface and is absorbed most strongly by a black surface. We'll use this difference in light reflection to detect the line. We can use either an LDR (light-dependent resistor) or an IR sensor to detect the light.
For this project, we'll use IR sensors because they offer greater accuracy. To detect the line, we'll place two IR sensors, one on the left and the other on the right side of the robot, as shown in the diagram below. We'll position the robot on the line so that the line is centered between the two sensors. While this explanation focuses on the general principles, you can find many tutorials online about using IR sensors with an ESP32 for more specific implementation details..
An IR array sensor module has multiple IR (infrared) pairs. 1 Each pair has an IR LED that emits infrared light, and a phototransistor that detects it. 2 When an object comes near, it reflects the IR light back to the phototransistor, which then sends a signal. 3 This way, the module can sense objects or detect lines by "seeing" the reflected IR light
Now, How does a Line Follower Robot Navigates the lines?
Move Forward (Line Between Sensors):
- Both sensors detect the white surface (indicating the line is centered).
- Both motors rotate in the direction that propels the robot forward (even if that's physically "opposite" in your setup).
- The goal is to maintain a straight path with the line positioned between the sensors.
Turn Left (Left Sensor on Dark Line):
- Left sensor detects the dark line, right sensor detects white.
- Microcontroller receives signal from the left sensor.
- Left motor rotates backward, right motor rotates forward.
- Robot turns left to get back on the line.
Turn Right (Right Sensor on Dark Line):
- Right sensor detects the dark line, left sensor detects white.
- Microcontroller receives signal from the right sensor.
- Left motor rotates forward, right motor rotates backward.
- Robot turns right to get back on the line.
Stop (Both Sensors on Dark Line):
- Both sensors detect the dark line.
- Microcontroller interprets this as a "stop" signal.
- Both motors are deactivated.
- Robot halts.
Connection Diagram of Line follower robot:
The connection consists of mainly four parts: IR array sensors, one motor drive, two motors, one esp32, a battery and few connecting wires. The sensor senses the IR light reflected from the surface and feeds the output to the onboard op-amp comparator. When the sensor is situated over the white background, the light emitted by the sensor is reflected by the white ground and is received by the receiver. But when the sensor is above the black background, the light from the source is not reflected back to the sensor. The sensor senses the intensity of reflected light to give an output.
The sensor’s output is fed to the microcontroller, which gives commands to the motor driver to drive the motor accordingly. In our project, the Arduino Uno is programmed to make the robot move forward, turn right or turn left and stop according to the input coming from the sensor. The output of the Arduino is fed to the motor driver.
Why We Require a Motor Driver?
The ESP32's output signals are too weak to directly power the robot's motors, and we need to control their direction for turns. Therefore, we use an L293D motor driver. This dual H-bridge chip amplifies the ESP32's signals, providing the necessary current to drive the motors and allowing us to control their forward and reverse rotation, enabling precise line following.
For this line follower robot, a 7.4V Li-ion battery powers the entire circuit, offering flexibility as the system can operate with batteries ranging from 6-12V. To ensure the robot moves effectively, 60 RPM 6V geared motors were selected. These motors provide the necessary torque to carry the robot's weight while maintaining a controlled speed.
L298N Motor Driver for Line Following Robot:
The L298N motor driver is now a viable and often preferred alternative to the L293D for line-following robots. While the L293D was historically popular, recent price reductions have made the L298N a cost-effective choice. Both modules serve the crucial function of amplifying the microcontroller's signals to drive the motors, but the L298N often offers higher current handling capabilities. Therefore, due to the L298N's improved price point and performance, it's now a recommended option for line-following robot projects.
Here is the circuit Diagram of Robot:
Excellent! Clear labeling and schematics are essential for easy assembly. By following the provided diagram and wire markings, constructing the line-following robot's circuit becomes a straightforward process. With the circuit understood, it's time to begin the physical build.
I hope you will understood which and sorry for not providing assembly step because in that time i am able to click the image.
Now here is code of Esp32 based line follower robot and you can copy it and then paste in Arduino IDE.
After you all assemble the parts then you robots look like and i will also provide 3D STL file of line follower robot body which you can download and print It self, once you assembled robot then you will Upload code.
Here is complete source code:
//Esp32 based line follower robot:
#define m1 21 // Right Motor FORWARD MA1
#define m2 33 // Right Motor BACKWARD MA2
#define m3 25 // Left Motor FORWARD MB1
#define m4 22 // Left Motor BACKWARD MB2
#define e1 23 // Right Motor Enable Pin EA
#define e2 32 // Left Motor Enable Pin EB
// **********5 Channel IR Sensor Connection**********
#define ir1 26 // Sensor R
#define ir2 27 // Sensor S
#define ir3 14 // Sensor L
#define ir4 12 // Sensor R
#define ir5 13 // Sensor S
// ************************************************* //
void setup() {
Serial.begin(115200);
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(m4, OUTPUT);
pinMode(e1, OUTPUT);
pinMode(e2, OUTPUT);
pinMode(ir1, INPUT);
pinMode(ir2, INPUT);
pinMode(ir3, INPUT);
pinMode(ir4, INPUT);
pinMode(ir5, INPUT);
// Setup PWM for motor control
ledcAttachPin(e1, 0); // Attach e1 to channel 0
ledcAttachPin(e2, 1); // Attach e2 to channel 1
ledcSetup(0, 5000, 8); // 5 kHz PWM, 8-bit resolution
ledcSetup(1, 5000, 8); // 5 kHz PWM, 8-bit resolution
}
void loop() {
// Reading Sensor Values
int s1 = digitalRead(ir1); // Left Most Sensor
int s2 = digitalRead(ir2); // Left Sensor
int s3 = digitalRead(ir3); // Middle Sensor
int s4 = digitalRead(ir4); // Right Sensor
int s5 = digitalRead(ir5); // Right Most Sensor
Serial.print(s1);
Serial.print(s2);
Serial.print(s3);
Serial.print(s4);
Serial.println(s5);
// if only middle sensor detects black line
if ((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 1) && (s5 == 1)) {
// going forward with full speed
ledcWrite(0, 250); // you can adjust the speed of the motors from 0-255
ledcWrite(1, 250); // you can adjust the speed of the motors from 0-255
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
}
// if only left sensor detects black line
else if ((s1 == 1) && (s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1)) {
// going LEFT with full speed
ledcWrite(0, 250); // you can adjust the speed of the motors from 0-255
ledcWrite(1, 250); // you can adjust the speed of the motors from 0-255
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
}
// if only left most sensor detects black line
else if ((s1 == 0) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 1)) {
// going LEFT with full speed
ledcWrite(0, 250); // you can adjust the speed of the motors from 0-255
ledcWrite(1, 250); // you can adjust the speed of the motors from 0-255
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
}
// if only right sensor detects black line
else if ((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 1)) {
// going RIGHT with full speed
ledcWrite(0, 250); // you can adjust the speed of the motors from 0-255
ledcWrite(1, 250); // you can adjust the speed of the motors from 0-255
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
}
// if only right most sensor detects black line
else if ((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0)) {
// going RIGHT with full speed
ledcWrite(0, 250); // you can adjust the speed of the motors from 0-255
ledcWrite(1, 250); // you can adjust the speed of the motors from 0-255
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
}
// if middle and right sensor detects black line
else if ((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 1)) {
// going RIGHT with full speed
ledcWrite(0, 250); // you can adjust the speed of the motors from 0-255
ledcWrite(1, 250); // you can adjust the speed of the motors from 0-255
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
}
// if middle and left sensor detects black line
else if ((s1 == 1) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1)) {
// going LEFT with full speed
ledcWrite(0, 250); // you can adjust the speed of the motors from 0-255
ledcWrite(1, 250); // you can adjust the speed of the motors from 0-255
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
}
// if middle, left and left most sensor detects black line
else if ((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1)) {
// going LEFT with full speed
ledcWrite(0, 250); // you can adjust the speed of the motors from 0-255
ledcWrite(1, 250); // you can adjust the speed of the motors from 0-255
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
}
// if middle, right and right most sensor detects black line
else if ((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 0)) {
// going RIGHT with full speed
ledcWrite(0, 250); // you can adjust the speed of the motors from 0-255
ledcWrite(1, 250); // you can adjust the speed of the motors from 0-255
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
}
// if all sensors are on a WHITE line
else if ((s1 == 1) && (s2 == 0) && (s3 == 1) && (s4 == 0) && (s5 == 1)) {
// stop
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
}
}
And i will also explain the code which you can better understood;
1. Pin Definitions:
- #define m1 21, #define m2 33, etc.: These lines define the ESP32 pins connected to the motor driver.
- m1, m2: Right motor forward/backward control.
- m3, m4: Left motor forward/backward control.
- e1, e2: Right/Left motor enable pins (PWM speed control).
- ir1 - ir5: Pins connected to the five IR sensors.
2. Setup Function (void setup()):
- Serial.begin(115200);: Initializes serial communication for debugging.
- pinMode(...): Sets the motor control and sensor pins as outputs or inputs.
- ledcAttachPin(...): Attaches the motor enable pins (e1, e2) to PWM channels.
- ledcSetup(...): Configures the PWM channels (5 kHz frequency, 8-bit resolution) for motor speed control.
3. Loop Function (void loop()):
Sensor Reading:
- digitalRead(ir1), etc.: Reads the digital values from the IR sensors (0 or 1).
- These values are stored in s1, s2, s3, s4, and s5.
- Serial.print/println(): Prints the sensor values to the serial monitor for debugging.
- Line Following Logic (Conditional Statements):
- The code uses a series of if and else if statements to determine the robot's position relative to the line based on the sensor readings.
- Each if condition checks a specific pattern of sensor values.
Motor Control:
- ledcWrite(0, 250): Sets the PWM duty cycle (motor speed) for the right motor. The value 250 represents a high speed.
- digitalWrite(m1, HIGH), digitalWrite(m2, LOW), etc.: Sets the motor direction by controlling the motor driver's input pins.
- The motor control outputs are set according to the sensor readings.
Specific Logic:
- Forward: If the middle sensor is on the line, the robot moves forward.
- Left/Right Turns: If a left or right sensor (or a combination) detects the line, the robot turns in that direction.
- Stop: If all sensors are on a white surface, the Robot stops.
Sensor Interpretation:
The code assumes that a sensor reading of 0 indicates the sensor is over the black line, and 1 indicates it's over the white surface.
Key Observations:
- PWM Speed Control: The use of ledcWrite() allows for smooth motor speed adjustments.
- Sensor Patterns: The if conditions are carefully designed to recognize specific sensor patterns, enabling the robot to follow the line.
- Serial Debugging: The Serial.print() statements are very helpful for debugging and understanding the sensor readings.
- Full Speed: The motor speed is set to 250 which is near the maximum possible speed.
- Sensor Placement: The sensor placement is very important for the code to function correctly.
Once you have completed robot after that your robot look like:
Frequently Asked Questions (FAQ) About Line Follower Robots:
Q: What is a line following robot and how does it work?
A: A line following robot is an autonomous robot designed to follow a visible line on the ground. It uses infrared (IR) sensors to detect the line. White surfaces reflect IR light back to the sensor, while black lines absorb it. The robot's microcontroller uses this information to adjust motor speeds and stay on the line.
Q: What components do I need to build a basic line follower robot?
A: You need a microcontroller board like an ESP32 (or Arduino UNO), two IR sensors, a motor driver (L293D or L298N), two DC gear motors (60-100 RPM), wheels, a chassis, a battery (7.4V or 9V), and connecting wires. This project is beginner-friendly and typically costs around $25-30.
Q: Which IR sensor is best for a line following robot?
A: The TCRT5000 IR sensor module is highly recommended for its good detection range (2-30cm), sensitivity adjustment, and stable performance. 1 However, for cost-effectiveness, generic IR sensor modules can also be used, as demonstrated in this tutorial.
1.
TCRT5000 Single Channel Line Tracking Sensor Module IR sensor - Makestore
www.supotronix.in
Q: Why do we need a motor driver in a line follower robot?
A: Microcontroller output pins, like those on the ESP32, cannot directly power DC motors. A motor driver, such as the L293D or L298N, provides the necessary current and voltage control, protecting the microcontroller from damage.
Q: How can I improve my line follower robot's performance?
A: Improve performance by implementing PID control for smoother movement, adding more sensors for enhanced line detection, optimizing sensor height (5-10mm from the ground), and ensuring stable battery voltage.
Q: What are common problems with line follower robots and their solutions?
A: Common issues include inconsistent line following (adjust sensor calibration), jerky movements (tune motor speeds or implement PID), losing the line on sharp turns (optimize sensor placement or add more sensors), and erratic behavior (check battery and connections). If you encounter other problems, please leave a comment, and we'll assist you.
Q: Can a line follower robot detect different colored lines?
A: Basic IR sensors work best with black lines on white surfaces. Color sensors (TCS230) can detect different colors, but they increase complexity and cost.
Q: What programming skills do I need to build a line follower robot?
A: Basic C++ programming skills, especially for the Arduino/ESP32 environment, are sufficient. Understanding digital I/O, variables, and conditional statements is essential. Our tutorial provides fully commented code for easy learning. This project is an excellent starting point for learning programming and robotics.
video of robot
.....................................................
Tags
#Robotics, #AutonomousRobot, and #DIYRobotics highlight the project's core nature. Emphasize its specific function with #LineFollower and #LineFollowingRobot, and detail its sensor technology with #IRSensor. For the microcontroller and electronics aspects, include #ESP32, #Arduino, and #MotorDriver, specifically mentioning #L298N or #L293D. Programming and software are represented by #C++, #ArduinoProgramming, and #PIDControl for advanced control. Finally, to attract beginners and educational enthusiasts, use #DIY, #BeginnerRobotics, #EducationalRobotics, and #STEM. These tags collectively categorize your project, making it discoverable for those interested in robotics, electronics, and programming.