AI-in-LEGO-EV3-Maze-Driving-Robot
AI-in-LEGO-EV3-Maze-Driving-Robot
by Tony--K
This is a simple, autonomous robot with some arti cial intelligence. It is designed to explore a maze and when
placed back at the entrance, to drive thru to the exit and avoid the dead ends. It is much more complicated than
my previous project, which simply drove thru the maze. Here, the robot must remember the path it has travelled,
remove dead-ends, store the new path, and then follow the new path.
The robot is built using LEGO Mindstorms EV3. The EV3 Software runs on a computer and generates a program,
which is then downloaded to a microcontroller called an EV3 Brick. The programming method is icon-based and
high-level. It’s very easy and versatile.
Supplies:
PARTS
TOOLS
SOFTWARE
M A Z E- DRIVING M E T HO D
There are several methods of navigating a maze. If you are interested in studying them, they are described very
well in the following Wikipedia article: https://en.wikipedia.org/wiki/Maze_solving_algorithm
I chose the left-hand wall-following method. The idea is that the robot will keep a wall on its left side by making the
following decisions as it goes thru the maze:
One caution is that the method could fail if the maze has a loop in it. Depending on the placement of the loop, the
robot could keep going around and around the loop. A possible solution for this problem would be for the robot to
switch to the right-hand wall-follower rule if it realized that it was going in a loop. I didn’t include this re nement in
my project.
While driving thru the maze the robot must memorize the path it is travelling and eliminate dead-ends. It
accomplishes this by storing each turn and intersection in an array, checking for speci c combinations of turns and
intersections as it goes, and replacing the combinations that include a dead-end. The nal list of turns and
intersections is the direct path thru the maze.
The possible turns are: Left, Right, Back (at a dead-end), and Straight (which is an intersection).
HO W T HE RO BO T HANDLES M Y M A Z E
1. When the robot starts driving it sees a space to the right and stores Straight in the list in the array.
2. Then it turns left and adds Left to the list. The list now contains: Straight, Left.
3. With a dead end, it turns around and adds Back to the list. The list now contains: Straight, Left, Back.
4. Passing the lane it used from the entrance, it adds Straight to the list. The list now contains: Straight,
Left, Back, Straight. It recognizes a combination and changes Left, Back, Straight to Right. The list
now contains Straight, Right.
5. With a dead end, it turns around and adds Back to the list. The list now contains: Straight, Right,
Back.
6. After the left turn the list contains Straight, Right, Back, Left. It recognizes a combination and
changes Right, Back, Left to Back. The list now contains Straight, Back.
7. After the next left turn the list contains Straight, Back, Left. It changes that combination to Right.
The list now contains only Right.
8. It passes a space and adds Straight to the list. The list now contains Right, Straight.
9. After the right turn the list contains Right, Straight, Right which is the direct path.
When the robot decides to turn it should either make a wide turn, or go forward a short distance before turning
and after turning go forward a short distance again without checking the sensor. The reason for the rst short
distance is that the robot shouldn’t bump into the wall after the turn, and the reason for the second short distance
is that after the robot has turned, the sensor would see the long space it had just come from, and the robot would
When the robot senses an intersection on the right but it’s not a right turn, I have found that it’s good to have the
robot drive forward about 10 inches (25 cm) without checking its sensors.
Although LEGO Mindstorms EV3 is very versatile, it allows no more than one of each type of sensor connected to
one Brick. Two or more Bricks could be daisy-chained, but I didn’t want to buy another Brick, and so I used the
following sensors (instead of three ultrasonic sensors): infrared sensor, color sensor, and ultrasonic sensor. This
worked out well.
But the color sensor has a very short range, of about 2 inches (5 cm), which leads to a few special considerations as
described below:
1. When the color sensor detects a wall in front and the robot decides to turn right or turn around, it
should back up rst, in order to give itself enough space to turn without bumping into the wall.
2. A complicated issue occurs with some “Straight” intersections. Because of the short range of the color
sensor, the robot cannot determine whether it senses a proper “Straight” intersection or the lead-up
to a right turn. I have tried to x this issue by setting the program to store a “Straight” in the list every
time the robot senses one, and then eliminate more than one “Straight” in a row in the list. This xes
the situation where a right turn follows a “Straight” in the maze but not the situation where there is a
right turn without a “Straight” before it. I also tried setting the program to eliminate a “Straight” if it is
just before a “Right” but this doesn’t work if a right turn follows a “Straight”. I haven’t been able to
nd a solution that ts all cases, altho I suppose it would be possible for the robot to look at the
distance travelled (by reading the motor rotation sensors) and decide whether it’s a “Straight” or a
right turn. I didn’t think this complication was worth doing for the purposes of demonstrating the AI
concept in this project.
3. An advantage of the color sensor is that it distinguishes between the brown of a wall and the red of
the barrier I used at the exit, and provides an easy way for the robot to decide when it has nished
the maze.
LEGO Mindstorms EV3 has a very convenient icon-based programming method. Blocks are shown at the bottom of
the display screen on the computer and can be drag-and-dropped into the programming window to build a
program. The EV3 Brick may be connected to the computer by either a USB cable, Wi-Fi or Bluetooth, and the
program may then be downloaded from the computer to the Brick.
The program consists of a main program and several “My Blocks” which are subroutines. The uploaded le contains
the entire program, which is here: https://github.com/Tony- le/myEV3maze
The Navigate My Block, which controls how the robot drives thru the maze, is shown. The print is very small and
may not be legible. But it’s a good example of how versatile and powerful are the if-statements (called Switches in
the LEGO EV3 system).
1. Arrow #1 points to a Switch that checks if the infrared sensor sees an object more than a speci c
distance away. If so, the top series of blocks is executed. If not, then control is passed to the large,
bottom series of blocks, where arrow #2 is located.
2. Arrow #2 points to a Switch that checks what color the color sensor sees. There are 3 cases: no color at
the top, red in the middle, and brown at the bottom.
3. Two arrows #3 point to Switches that check if the ultrasonic sensor sees an object more than a
speci c distance away. If so, the top series of blocks is executed. If not, then control is passed to the
bottom series of blocks.
The My Blocks for shortening the path and for driving the direct path are more complicated and would be totally
illegible, and so they aren’t included in this document.
ThIs step and the next 2 steps are for the top of the base of the robot, the color sensor, and the cables, which are all
10 inch (26 cm) cables.
Next, are the infrared sensor (on the left side of the robot) and the ultrasonic sensor (on the right). Also, the 4 pins
for attaching the Brick on top.
AI in LEGO EV3 Maze-Driving Robot: Page 7
The infrared and ultrasonic sensors are located vertically instead of the normal horizontal. This provides better
identi cation of the corners or ends of the walls.
Two corrugated cardboard cartons should be in any path with a dead end. The length should be no
su cient for the maze. I made the maze walls 5 inches less than 10 inches (25 cm). These distances are
(12.5 cm) high, but 4 inches (10 cm) should work just needed for the robot to turn around.
as well if you’re short of corrugated cardboard.
Some of the corners of the maze may need to be
First, I cut around the walls of the cartons, 10 inches reinforced, Also, some straight walls need to be kept
(25 cm) from the bottom. Then I cut around the walls from bending if they include a straightened carton
5 inches from the bottom. This provides several 5-inch corner. Small pieces of thin cardboard should be
walls. Also, I cut around the bottoms of the cartons, glued to the bottom at those places, as shown.
leaving about 1 inch (2.5 cm) attached to the walls for
stability. The exit has a red barrier consisting of half a red
greeting-card envelope and a base made from 2
The various pieces can be cut and glued or taped pieces of thin cardboard, as shown.
wherever needed to form the maze. There should be
an 11 or 12 inch (30 cm) space between the side walls
One caution is that the maze should not be large. If speci c distance from the left wall. I didn’t include
the robot’s turns are at a slight angle from the proper this. The program is complicated enough as it is, and it
one, the discrepancies add up after a few turns and is su cient for demonstrating the AI concept in this
the robot could run into the walls. I had to ddle project.
several times with the Rotations settings of the turns
in order to get a successful drive thru even the small CO NCLUDING REM ARK
maze I made.
This was a fun project and a great learning
A way around that issue is to include a path- experience. I hope you also nd it interesting.
straightening routine that would keep the robot a