Automatic mazes with Raspberry Pi and recursive backtracking

Engineerish is back with another Raspberry Pi–based project that you didn’t know you needed until now.

Printed mazes

Mattias Jahnke, better known as Engineerish to his online followers, was asked by his nephew to draw labyrinth mazes for the youngster to complete. While the task was fun to do by hand, Mattias soon found himself wondering what code and technology he could use to automate it. He soon hit upon the idea of using a Raspberry Pi, a thermal printer, and the recursive backtracking algorithm to produce mazes.

What is recursive backtracking?

Engineerish offers a simplified explanation of recursive backtracking in the video above, and you can learn even more about this algorithm here, here, and here.

The latter of these links provides the following summary of backtracking:

Backtracking problems are solved one step at a time. Literally!  Here’s the general algorithm:

1) Is where I am a solution?
2) No. OK, where can I go from here? If I can go somewhere, choose a place to go.
3) Go there.
5) Was that a solution? If yes, return true!
5) If there are remaining places to go, choose one and go to #3.
6) Out of places to go. Return false.

Building an automated maze printer

To fit the Raspberry Pi and printer, as well as an arcade button and a power supply, Engineerish built a custom wooden box.

The arcade button is wired to GPIO pin 16, and pressing it starts a Python script that runs the recursive backtracking algorithm and lets the thermal printer produce the finished maze.

Engineerish has provided the complete code for the project on his GitHub account, allowing everyone to try their hand at printing (and completing) these awesome mazes.

Engineerish

If you’d like to see more from Engineerish, be sure to subscribe to his YouTube account and follow him on Instagram.

Engineerish recursive backtracking raspberry pi mazes

And be sure to also check out his Raspberry Pi Binary Clock, which we covered here on the blog in January.

1 comment

Jürgen Böhm avatar

Old Xmaze from HP(?), running with Raspian.

https://forum-raspberrypi.de/forum/thread/29541-und-es-begab-sich-in-uralter-zeit/

On the third Thread are the Sourcecodes:
https://forum-raspberrypi.de/attachment/11282-maze-src-tgz/

Forum in German.

The oldest Sourcecodes are from 1972!

Have Fun

Jürgen

Comments are closed