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.

PRINT MAZES WITH RASPBERRY PI

Don’t already have a device around your home that, at the press of a button, prints something cool? Build one! Mine prints randomly generated mazes but why stop there? Thermal printer: https://www.adafruit.com/product/597 Source code: https://github.com/mattiasjahnke/rpi-maze-printer Recursive backtracking algorithm: https://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_backtracker ———— Consider subscribing to the channel so you don’t miss out!

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.

This is me – only seconds away from realizing how mankind first discovered how to make a fire. They’ve must have been trying a cut a whole in a wooden box with a Dremel for a maze-generating raspberry pi project.

492 Likes, 17 Comments – Engineerish (@engineerish) on Instagram: “This is me – only seconds away from realizing how mankind first discovered how to make a fire….”

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.

Endless fun for the whole fam! Randomly generated mazes with adjustable difficulty at the click of a button. Even though that “button” is currently an ssh connection to a raspberry pi, the execution of two python scripts with passed process arguments to set difficulty and nanoing source code to set the maze size. I’ll try to go full Windows Vista on the user friendlyness another day. For now – it works!

689 Likes, 40 Comments – Engineerish (@engineerish) on Instagram: “Endless fun for the whole fam! Randomly generated mazes with adjustable difficulty at the click of…”

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.