Line Follower Robot using ESP32 and IR Sensors - Proteus Simulation: the ESP32, the 5-channel IR tracker, the L298N and two TT gear motors

Line Follower Robot using ESP32 and IR Sensors - Proteus Simulation

40
700
60
25
60
PCBWay

Hello friends, I hope you all are doing great. Today, I am going to share the Line Follower Robot using ESP32 and IR Sensors - Proteus Simulation, project 103 of our IoT Projects series. The line follower is the robot every robotics class builds and every robotics competition starts with: a small car that follows a black line on a white floor. Here is a complete one, with the circuit, the code with a real PID controller, and a Proteus simulation you can run before you cut a single piece of tape.

Five IR sensors on a tracker bar look at the floor under the front of the robot. The ESP32 works out where the line is under the bar, turns that into an error, and a PID controller turns the error into two wheel speeds for the L298N and the two TT gear motors: a small correction on a gentle curve, a hard one on a sharp curve. The robot also handles the things a real track throws at it: it drives straight across a cross line, it searches for the line when there is a gap, it waits when something stands in its way, and it stops when its bump switch hits something. Because it is an ESP32, there is a dashboard too, to start and stop it and set its speed. In this tutorial, we will go through the circuit, how an IR sensor sees a line, how five sensors give one position, the PID controller, the special cases and the code. Then we drive a whole recorded course in the simulation. At the end, you can download the project with the sketch and every library file.

NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the line follower robot using ESP32 and IR sensors in Proteus simulation:

Line Follower Robot using ESP32 and IR Sensors - Proteus Simulation: the ESP32, the 5-channel IR tracker, the L298N and two TT gear motors
Figure: The line follower robot in a Proteus simulation.
Line follower robot using ESP32 Proteus simulation running: curves, a gap, an obstacle and the line under S5
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This Line Follower Robot Do?

  • Five IR sensors: the position of the line under the bar, from far left to far right.
  • PID steering: a correction every 10 ms from the error and how fast it changes.
  • Junctions: all sensors black means a cross line: straight on, counted.
  • A lost line: it turns towards the side the line was last seen, and stops if it does not find it in 1.5 s.
  • Obstacles and bumps: it waits for an obstacle and stops when the bump switch is pressed.
  • A dashboard: it starts by itself at power-on; START, STOP and three speeds from the web, with the sensors and motors live.

In the simulation, the tracker's pop-up shows the floor under the bar with the line and the five sensors, and its chips move the line or play a recorded course; the L298N's pop-up shows each wheel's voltage.

Components Used in the Proteus Simulation

All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file.

The parts of the line follower robot and their library posts
PartProteus deviceLibrary postConnection
ESP32 DevKit V1ESP32 DEVKIT V1ESP32 DevKit V1 Library for ProteusU1, ESP32_Line_Follower.hex, Wi-Fi 192.168.1.101
5-channel IR line trackerLINETRACKER5TEP5-Channel Line Tracker Library for ProteusU2: S1 - S5 on D4, D16, D17, D18, D19; NEAR D21; CLP D22
L298N motor driverL298NDRIVERTEPL298N Motor Driver Library for ProteusU3: ENA D32, IN1 D33, IN2 D25, ENB D14, IN3 D26, IN4 D27
TT gear motors (x 2)TTGEARMOTORTEPTT Gear Motor Library for ProteusM1 left, M2 right
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Line Follower: the Circuit

Line follower robot Proteus simulation circuit: an ESP32, a 5-channel IR tracker, an L298N and two TT gear motors
Figure: The complete circuit of the line follower robot on an A3 Proteus sheet.
The connections of the line follower robot
SignalESP32Goes to
S1 - S5D4, D16 (RX2), D17 (TX2), D18, D19the five IR channels, LOW over black
NEAR / CLPD21 / D22the obstacle sensor and the bump switch, HIGH when active
ENA, IN1, IN2D32, D33, D25the L298N: the left motor
ENB, IN3, IN4D14, D26, D27the L298N: the right motor
VCC (5 V)VINthe tracker bar
+7.4V-the L298N's motor supply (a 2S Li-ion pack)

The tracker bar does the analog work on its own board: each TCRT5000's signal goes through a Schmitt inverter, so the ESP32 just reads five clean digital pins. The motors have their own 7.4 V pack through the L298N, whose enable jumpers are removed so the ESP32's PWM sets each wheel's speed.

How the Line Follower Robot Works

How an IR Sensor Sees a Line

A TCRT5000 is an infrared LED and a phototransistor side by side, both looking down. White floor reflects most of the infrared back and the phototransistor conducts; a black line absorbs it and the phototransistor turns off. The bar compares each channel against a threshold and gives a LOW over black and a HIGH over white, lighting the channel's LED. The bar must sit close to the floor, 3 to 10 mm: much higher, and even white looks grey to the sensors.

From Five Sensors to One Position

The five sensors sit about 15 mm apart. The sketch gives each one a weight, 0 for S1 on the far left, 1000, 2000 for S3 in the centre, 3000, and 4000 for S5 on the far right, and averages the weights of the sensors that see black. A line under S3 gives 2000; a line between S3 and S4 lights both and gives 2500. So five on-off sensors give a position with nine steps, and the error is simply the position minus 2000: negative when the line is to the left, positive when it is to the right.

The PID Controller

The P term steers in proportion to the error: a line far to the right makes the left wheel faster and the right wheel slower, hard. On its own, P makes a robot weave, because by the time the line is centred again the robot is already turning too fast. The D term looks at how fast the error changes and pushes against it, which damps the weaving, and it gives an extra kick when the line suddenly jumps sideways, as at the start of a sharp curve. An I term would remove a constant offset, but a line follower rarely needs it, so this sketch uses P and D: correction = 0.035 x the error + 0.25 x the change. Left = base + correction, right = base - correction, every 10 ms.

Junctions, Gaps, Obstacles and Bumps

Some patterns are not a position at all. All five sensors black is a cross line or a wide junction: the robot drives straight on and counts it. All five white means the line is lost: the robot turns towards the side where it last saw the line, which is almost always where the line went, and gives up after 1.5 s. The bar also has a NEAR sensor that sees an obstacle a few centimetres ahead, and a bump switch on its front edge: an obstacle makes the robot wait until it is gone, a bump stops it until someone presses START.

The Arduino Code Explained

The sketch is ESP32_Line_Follower.ino, built for our ESP32 DevKit V1 with the WiFi and WebServer libraries. The pins and the gains:

const byte S[5] = {4, 16, 17, 18, 19};                            // S1 (far left) ... S5 (far right)
const float KP = 0.035, KD = 0.25;                                // % per position unit; % per unit change in 10 ms

The position: the weighted average of the black sensors:

  for (byte k = 0; k < 5; k++) if (digitalRead(S[k]) == LOW) { pattern |= 1 << k; sum += k * 1000L; n++; }
  position = sum / n;

The PD correction and the two wheel speeds:

  const float corr = KP * error + KD * (error - lastError);
  drive(constrain((int)(base + corr), -30, 100), constrain((int)(base - corr), -30, 100));

A junction, and the search when the line is lost:

  if (n == 5) {                                                   // a cross line: straight on
    if (lastError < 0) drive(-SPIN / 2, SPIN); else drive(SPIN, -SPIN / 2);

The obstacle sensor and the bump switch come first in every loop:

  if (digitalRead(CLP) == HIGH) { drive(0, 0); setMode(BUMPED, "the bump switch: press START"); return; }
  if (digitalRead(NEAR) == HIGH) {

The robot starts following at power-on, like a competition robot, and does not wait for the WiFi. The web server starts in loop() as soon as the WiFi has joined:

  if (!webUp && WiFi.status() == WL_CONNECTED) {                  // the dashboard as soon as the WiFi is up

The dashboard's links are Refresh, START and STOP, and SLOW, NORMAL and FAST; /data gives the mode, the sensors, the position, the motors and the counts as JSON. START clears the last error, so the D term does not kick on the first loop:

  if (u == F("/start") && mode != FOLLOWING) { lastError = 0; setMode(FOLLOWING, "START from the web"); }

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files (the steps are under the download button).
  2. Open ESP32-Line-Follower.pdsprj from the Proteus Simulation folder. Keep the HEX and the DLL files next to it.
  3. Press Play: the robot follows the tracker's recorded course at once, curves, a junction, a gap and an obstacle, until the bump switch stops it.
  4. Click U2 for the tracker's pop-up and press Line centre; open the Laptop WiFi Monitor (press Go) and press START.
  5. Line left and Line right on the pop-up make the robot steer; try SLOW, NORMAL and FAST.

Simulation Results: the Line Follower in Proteus

We pressed Play and let the robot run the tracker's recorded course: a curve to the right, a cross line, a sharp curve to the left, a gap, an obstacle and finally a bump. Then we centred the line with the pop-up, started the robot again from the laptop, moved the line to each side, set FAST and stopped it from the phone. The tracker's pop-up shows the floor under the bar and the five channels, the L298N's pop-up shows each wheel's voltage and current, and the serial monitor prints the position and both wheel speeds every half second. The times are the sketch's clock; the whole run took 28.9 simulated seconds.

Following from Power-On

The sketch printed READY - following the line from power-on at 0.00 s and the robot drove off at once, before the WiFi had joined. The dashboard came up about 2.2 s later, when the ESP32 printed its address, 192.168.1.101. The line started under S3, the centre sensor, so the error was close to zero and both wheels ran at the base speed of 60 %.

Line follower robot Proteus simulation at power-on: the robot following the line, the tracker LEDs lit
Figure: Following from power-on.
Line tracker pop-up in the line follower robot simulation: the line under S3, the course beginning
Figure: The tracker: the line under the centre sensor.

A Curve to the Right

The line moved to +19.9 mm, under S4: the pattern 11101 and a position of 3000, so the error was +1000. The P term alone gives 0.035 x 1000 = 35 %, so the left wheel ran at 60 + 35 = 95 % and the right one at 60 - 35 = 25 %. The L298N's pop-up shows exactly that: bridge A forward at 95 %, 5.60 V and 0.15 A on the left motor, bridge B at 25 %, 1.41 V on the right one. A faster left wheel turns the robot to the right, back over the line.

Line follower robot Proteus simulation on a curve to the right: the left wheel faster than the right
Figure: A curve to the right.
Line tracker pop-up in the line follower robot simulation: the line at +19.9 mm under S4, pattern 11101
Figure: The tracker: the line under S4.
L298N pop-up in the line follower robot simulation: left motor 95 % at 5.60 V, right motor 25 %
Figure: The L298N: left 95 %, right 25 %.

Straight Across a Junction

At 2.00 s all five sensors saw black: a cross line. The sketch printed JUNCTION - junction 1 and drove straight on at the base speed instead of trying to steer; 150 ms later, with the cross line behind it, it was back to FOLLOWING. A weighted average of five black sensors is 2000, the centre, so the robot would have gone straight anyway, but counting junctions separately is what a maze robot or a delivery robot needs to decide where to turn. The count stays on the dashboard, as the screenshots further down show.

A Sharp Curve to the Left

Then the line jumped to -25.7 mm, under S1 on the far left: the pattern 01111, a position of 0 and an error of -2000. The correction was big enough to take the left wheel to zero and slightly past it: the sketch lets each wheel go down to -30 %, so the left wheel turned a little backwards (bridge A reverse at 3 %, -0.13 V, as the correction grew) while the right one ran flat out (bridge B forward at 100 %, 5.90 V and 0.16 A). That pivot is what gets a robot round a sharp corner without running off the line.

Line follower robot Proteus simulation in a sharp curve to the left: the right wheel at full speed
Figure: A sharp curve to the left.
Line tracker pop-up in the line follower robot simulation: the line at -25.7 mm under S1, pattern 01111
Figure: The tracker: the line under S1.
L298N pop-up in the line follower robot simulation: right motor forward at 100 %, left motor stopped
Figure: The L298N: the right wheel at 100 %.

Losing the Line and Finding It Again

At 3.39 s the line disappeared: a gap in the track, all five sensors on white (11111). The last error had been positive, the line last seen on the right, so the sketch printed SEARCHING - the line was right and turned right on the spot: the left wheel forward at 45 %, the right one backwards. The L298N's pop-up, 60 ms into the turn, shows bridge A forward at 45 % (2.66 V) and bridge B already in reverse. At 3.80 s, 0.41 s later, the line was back under the bar at -6.3 mm and the sketch printed FOLLOWING - the line found again, with both wheels back at 60 % (3.46 V and 3.49 V). Had the line not come back within 1.5 s, the robot would have stopped with LINE LOST.

Line follower robot Proteus simulation searching for a lost line: all five sensors on white
Figure: The line lost: searching.
Line tracker pop-up in the line follower robot simulation: a gap in the line, all five channels white
Figure: The tracker: a gap.
L298N pop-up in the line follower robot simulation: the search turn, left forward 45 %, right reverse
Figure: The L298N: the search turn.
Line tracker pop-up in the line follower robot simulation: the line found again at -6.3 mm
Figure: The tracker: the line found again.
L298N pop-up in the line follower robot simulation: both wheels back at 60 % after the gap
Figure: The L298N: both wheels at 60 %.

An Obstacle and a Bump

At 5.35 s the course put an obstacle 3.0 cm in front of the bar, inside the NEAR sensor's 5 cm range. NEAR went high, the sketch printed OBSTACLE - WAITING - something in front, and both bridges coasted: both enables low and no current in either motor. 0.2 s later the robot touched it: the bump switch closed, CLP went high and the sketch printed BUMPED - the bump switch: press START. The TT motor's pop-up shows the left wheel at 0 RPM with no drive across it. The bump switch is checked first in every loop, and once BUMPED the robot stays stopped until someone presses START.

Line follower robot Proteus simulation waiting for an obstacle 3 cm in front of the tracker
Figure: An obstacle: waiting.
Line tracker pop-up in the line follower robot simulation: NEAR high, an obstacle 3.0 cm ahead
Figure: The tracker: an obstacle at 3 cm.
L298N pop-up in the line follower robot simulation: both enables low, both motors coasting
Figure: The L298N: both motors coasting.
Line follower robot Proteus simulation stopped by the bump switch, both wheels still
Figure: Bumped: stopped.
Line tracker pop-up in the line follower robot simulation: CLP high, the bump switch pressed
Figure: The tracker: the bump switch pressed.
TT gear motor pop-up in the line follower robot simulation: the left wheel at 0 RPM after the bump
Figure: The left wheel: 0 RPM.

The dashboard on the Laptop WiFi Monitor told the story so far: BUMPED, 1 junction, 1 time lost, 1 obstacle and 5 s of driving, with the last three events and their times.

Laptop WiFi Monitor showing the line follower dashboard in the simulation: BUMPED, 1 junction, 1 obstacle
Figure: The dashboard after the bump.

START from the Dashboard

We pressed Line centre on the tracker's pop-up and START on the laptop at 11.98 s: FOLLOWING - START from the web. The page was built in that same moment, so it still shows the motors at 0 %; one loop later both wheels ran at 60 %, 3.51 V and 0.14 A each, straight down the line.

Laptop WiFi Monitor in the line follower robot simulation: START pressed, FOLLOWING again
Figure: START from the laptop.
Line follower robot Proteus simulation following a straight line, both wheels at 60 %
Figure: Following again.

The Line Under the Outer Sensors

With Line right on the pop-up the line sat under S5, +30 mm: position 4000 and an error of +2000. The P term asked for +70 %, so the left wheel was held at 100 % and the right one went to 60 - 70 = -10 %: bridge A forward at 100 % (5.90 V), bridge B in reverse at 10 % (-0.53 V). The serial monitor printed P 4000 L 100 / R -10 % every half second while the line stayed there. Line left gave the mirror image: P 0, L -10 / R 100 %. The D term is zero on these steady readings, because the error does not change; it only acts at the moment the line jumps.

Line follower robot Proteus simulation with the line under S5: a hard turn to the right
Figure: The line under S5.
Line tracker pop-up in the line follower robot simulation: the line at +30 mm under S5, pattern 11110
Figure: The tracker: the line under S5.
L298N pop-up in the line follower robot simulation: left 100 % forward, right 10 % in reverse
Figure: The L298N: left 100 %, right -10 %.
L298N pop-up in the line follower robot simulation: left 10 % in reverse, right 100 % forward
Figure: The L298N: the mirror image.

FAST and STOP from the Phone

We pressed Line centre again and FAST on the laptop at 21.98 s: speed: FAST 80 %, and both wheels went up to 80 % on the straight. The phone's dashboard showed the same page, FOLLOWING at a base of 80 %. At 26.78 s we pressed STOP on the phone: STOPPED - STOP from the web, both wheels at 0 %. The final counts: 1 junction, 1 time lost, 1 obstacle and 20 s of driving.

Mobile WiFi Monitor in the line follower robot simulation: FOLLOWING at FAST, both wheels at 80 %
Figure: The phone: FAST 80 %.
Mobile WiFi Monitor in the line follower robot simulation: STOP pressed, the robot stopped
Figure: The phone: STOP.

The serial monitor kept the whole run: the events with their times, and every half second the bar (# over the line), the position and the two wheel speeds.

ESP32 serial monitor in the line follower robot simulation: the sensors, the position and both wheel speeds
Figure: The serial monitor.

Download the Project Files

Click the button below to download Line-Follower-Robot-ESP32-IR-Sensors-Proteus-Simulation-v1.0.zip (about 3.7 MB, without the source code of the models):

Line Follower Robot using ESP32 and IR Sensors - Proteus Simulation V1.0
  • Proteus Simulation: ESP32-Line-Follower.pdsprj, ESP32_Line_Follower.hex and copies of the models.
  • Arduino Code: ESP32_Line_Follower.ino, the sketch.
  • Proteus Library Files: the LIB files of every part.
  • Proteus Model Files: the DLL models.
  • README.txt: the installation steps, the properties, the test results and the links to every library post.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install the Project Files

  1. Close Proteus and extract the whole zip file, for example to your Desktop.
  2. Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
  3. Copy the files from Proteus Model Files into the MODELS folder.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

Open the sketch in the Arduino IDE with the TEP Boards for Proteus installed, select ESP32 Dev Module (TEP Proteus), and use Sketch > Export Compiled Binary. Put the new HEX next to the project. KP, KD, the search speed and the lost time are near the top.

Ideas to Extend the Project

  • Add an I term and tune all three gains from the dashboard while the robot runs.
  • Count the junctions and turn left or right at a chosen one: a maze or a delivery route.
  • Time the laps with a start marker across the track and show the best lap.
  • Use analog sensors instead of digital ones for a finer position.
  • Add wheel encoders and slow down before sharp curves you have seen on the last lap.
  • Log the error over a lap and plot it to see how well the gains work.

From Simulation to Real Hardware

  • The track: black electrical tape 15 - 20 mm wide on a white floor or card works well.
  • The bar: mount it 3 - 10 mm above the floor and a few centimetres ahead of the wheels.
  • Levels: a 5 V tracker's outputs need a 3.3 V version or dividers before the ESP32 pins.
  • Tuning: start with KD at zero, raise KP until the robot weaves, then add KD until it stops weaving; tune on your own track.
  • Light: sunlight contains a lot of infrared and can blind the sensors; shield the bar.

Troubleshooting

  • It steers the wrong way: S1 must be the far left sensor; swap the order if your bar is turned round.
  • It weaves: lower KP or raise KD.
  • It flies off sharp curves: lower the base speed or raise KP.
  • Every sensor reads black: the bar is too high above the floor.
  • No dashboard: the ESP32 joins TEP-WiFi about three seconds after the start; press Go again.

Limitations of the Simulation

  • The tracker bar, the L298N and the TT motors are our libraries' models, built from their datasheets and the parts' behaviour, not every detail of real parts.
  • The floor is the tracker's pop-up: its chips and its recorded Demo run move the line under the bar, so the robot's own steering does not change what the sensors see.
  • The PD gains are tuned for the simulation; a real robot needs its own tuning.
  • ESP32 is a product of Espressif Systems; this project is not made or endorsed by Espressif or by Labcenter.

So, that was all about the Line Follower Robot using ESP32 and IR Sensors - Proteus Simulation. I hope it helps you understand how IR sensors see a line, how five of them give one position, how a PD controller turns the error into wheel speeds, and how a robot deals with junctions, gaps and obstacles, all tested in a simulation before you lay the tape. More popular projects with the ESP32 and the Raspberry Pi 5 are coming. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!


Comments

0

Join the conversation