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.
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
You can code and run Picobricks’ modules without wiring. If you are going to use the modules by separating them from the board, you should make the module connections with grove cables.
Construction Stages of the Project
You can build the robot car by following the steps for the 2WD robot car assembly in the voice-controlled car project.
We will not use the HC05 bluetooth module in this project.
In order to mount the HC-SR04 ultrasonic distance sensor on the robot, you can download the required parts from the link here and print it on the 3D printer and mount it on the vehicle.
After assembling the robot, you can use cardboard boxes to build the maze. You can make walls out of cardboard, or you can use 3D printed walls to connect the walls with hot glue to build the maze.
MicroBlocks Codes of the PicoBricks
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
Arduino C Codes of the PicoBricks
#include
#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
}
Project Image
Project Proposal 💡
We used a HC-SR04 ultrasonic distance sensor, mounted to the front of the car, to navigate the maze. The algorithm used is very simple and relies on timed movements to navigate. You can enhance the code to use shorter timed movements and combine them with more sensor readings. Turns can be coded in a different way using this method, similar to the straight moves.
By using a servo motor, you can rotate the HC-SR04 module to the right and left, and detect the open paths without the vehicle turning. It will make the car progress faster. Or, you can develop the project by mounting 3 HC-SR04 ultrasonic distance sensors on the front, right and left side of the vehicle, measuring the distances in 3 directions at the same time and directing the vehicle towards the open paths.