Coding education is as old as the history of programming languages. Today, different products are used to popularize coding education and make it exciting and fun. The first of these is educational robots. Preparing and programming robots improves children’s engineering and coding skills. Robotics competitions are organized by institutions and organizations to popularize coding education and encourage teachers and students. One of these competitions is the Maze Solver Robot competitions. These robots learn how to solve the maze by wandering around in the maze and returning to the starting point. Then, when they start the labyrinth again, they try to reach their destination in the shortest way possible. Robots use infrared or ultrasonic distance sensors while learning the maze.
Smart robot vacuums used in homes and workplaces also work with logic close to the algorithms of maze-solver robots. Thanks to their algorithms that constantly check and map the obstacles, they try to complete their tasks without crashing into things. Most of the smart vacuums are equipped with LIDAR and infrared sensors that are designed for high-precision laser measurements and obstacle detection.
In this project, we will make a simple robot that you can use to navigate your own maze.
Maze Robot Details and Algorithm
In this project, we will use the 2WD robot car kit that comes with the set. We will use the HC-SR04 ultrasonic distance sensor so that the robot can detect the walls in front of it and decide its movements. In the maze, the robot will scan the space in front of the car and move forward if it is empty. If there is a wall (obstacle) within 5 cm’s, the car will turn right and measure the distance again. If the new distance on the right is greater than 5 cm, it will continue on its way. If it is less, it will turn left and move forward. By turning right and left, we will guide the vehicle to make progress and exit the maze.
Components
- 1X PicoBricks
- 1X HC-SR04 Ultrasonic Sensor
- 2X DC Motor
- Jumper Cables
- Easy Connection Cables
Wiring Diagram
MicroBlocks Codes of Maze Solving Robot
You can access the Microblocks codes of the project by dragging the image to the Microblocks Run tab or click the button.
MicroPython Codes of the PicoBricks
from machine import Pin
from utime import sleep
import utime
# Define libraries
trigger = Pin(15, Pin.OUT)
echo = Pin(14, Pin.IN)
# Define sensor pins
m1 = Pin(21, Pin.OUT)
m2 = Pin(22, Pin.OUT)
# Define dc motor pins
m1.low()
m2.low()
signaloff = 0
signalon = 0
def getDistance():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
return distance
# Calculate distance
measure = 0
while True:
measure = int(getDistance())
print(measure)
if measure > 5:
m1.high()
m2.high()
sleep(1) # If the distance is higher than 5, the wheels go straight
else:
m1.low()
m2.low()
sleep(0.5)
m1.high()
m2.low()
sleep(0.5)
measure = int(getDistance())
if measure < 5:
m1.low()
m2.low()
sleep(0.5)
m1.low()
m2.high()
sleep(0.5) # If the distance is less than 5, wait, move in any direction; if the distance is less than 5, move in the opposite direction
</code>
Arduino C Codes
#include <NewPing.h>
#define TRIGGER_PIN 15
#define ECHO_PIN 14
#define MAX_DISTANCE 400
// Define sensor pins
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
pinMode(21, OUTPUT);
pinMode(22, OUTPUT); // Define dc motor pins
}
void loop() {
delay(50);
int distance = sonar.ping_cm();
Forward();
if(distance < 5){
Stop();
delay(1000);
Turn_Right();
delay(1000);
int distance = sonar.ping_cm();
if(distance < 5){
Stop();
delay(1000);
Turn_Left();
delay(500);
// If the distance is less than 5, wait, turn right; if the distance is less than 5 again, move in the opposite direction
}
}
}
void Forward(){
digitalWrite(21, HIGH);
digitalWrite(22, HIGH); // If the distance is higher than 5, go straight
}
void Turn_Left(){
digitalWrite(21, LOW);
digitalWrite(22, HIGH); // Turn left
}
void Turn_Right(){
digitalWrite(21, HIGH);
digitalWrite(22, LOW); // Turn right
}
void Stop(){
digitalWrite(21, LOW);
digitalWrite(22, LOW); // Wait
}
FAQ
What is the algorithm of the robot in the maze runner?
The algorithm of the robot in the maze runner typically involves a pathfinding technique like the A* (A-star) algorithm. This algorithm efficiently finds the shortest path through the maze by combining aspects of the best-first search and Dijkstra's algorithm, balancing between exploration and the goal's distance.