# Tilt Controlled Marble Maze

## Overview

https://youtu.be/9WE4oNzbEp8

This project uses the accelerometer on the Circuit Playground Express board to control the X and Y axes of a maze. The user can guide a marble through the maze by simply tilting the Circuit Playground Express. This project is constructed using only cardboard, hot glue, and a couple of screws.

![](https://cdn-learn.adafruit.com/assets/assets/000/077/317/medium800thumb/circuit_playground_projects_IMG_5136.2019-06-21_16_44_29.jpg?1561155404)

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
![A Black woman's manicured hand holds a round microcontroller with lit up LEDs.](https://cdn-shop.adafruit.com/640x480/3333-05.jpg)

### Micro servo

[Micro servo](https://www.adafruit.com/product/169)
Tiny little servo can rotate approximately 180 degrees (90 in each direction) and works just like the standard kinds you're used to but _smaller_. You can use any servo code, hardware, or library to control these servos. Good for beginners who want to make stuff move without...

Out of Stock
[Buy Now](https://www.adafruit.com/product/169)
[Related Guides to the Product](https://learn.adafruit.com/products/169/guides)
![Micro servo with three pin cable](https://cdn-shop.adafruit.com/640x480/169-06.jpg)

### Small Alligator Clip to Male Jumper Wire Bundle - 6 Pieces

[Small Alligator Clip to Male Jumper Wire Bundle - 6 Pieces](https://www.adafruit.com/product/3448)
When working&nbsp;with unusual non-header-friendly surfaces, these handy cables will be your best friends! No longer will you have long, cumbersome strands of alligator clips. These compact jumper cables have a premium male header on one end and a grippy mini alligator clip on the...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3448)
[Related Guides to the Product](https://learn.adafruit.com/products/3448/guides)
![Group of Small Alligator Clip to Male Jumper Wires](https://cdn-shop.adafruit.com/640x480/3448-00.jpg)

### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

Out of Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)
![USB cable - USB A to Micro-B - 3 foot long](https://cdn-shop.adafruit.com/640x480/592-01.jpg)

## Materials

In addition to the electronics, for this project you will also need:

- Marble or steel ball
- Scrap cardboard
- [Hot glue gun](https://www.amazon.com/dp/B006IY359K/ref=cm_sw_r_tw_dp_U_x_FYW0CbKBYQ6H1)
- Scissors or&nbsp;[hobby knife](https://www.thingiverse.com/thing:17551)
- Small magnet

# Tilt Controlled Marble Maze

## CircuitPython Code

## Getting familiar with CircuitPython

[CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython/what-is-circuitpython)&nbsp;is a programming language based on Python, one of the fastest growing programming languages in the world. It is specifically designed to simplify experimenting and learning to code on low-cost microcontroller boards.

CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu,&nbsp;[this guide will get you started](https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor).

![](https://cdn-learn.adafruit.com/assets/assets/000/077/386/medium800/circuit_playground_projects_makecode_circuitpython_circuit_playground_adafruit_blinka_computer.png?1561389261)

## Preparing your Board

To get your Circuit Playground Express set up to run this code, follow these steps:

1) Install the&nbsp;[latest CircuitPython for CPX&nbsp;](https://circuitpython.org/board/circuitplayground_express/)from CircuitPython.org. For this guide, please use version 4.0.0 or greater. Version 3.1.2 is not as efficient on memory and may give an error.

2) Get the&nbsp;[latest library pack](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/), unzip it, and drag the library files **adafruit\_motor folder** &nbsp;and **simpleio.mpy** over into the **/lib** &nbsp;folder on&nbsp; **CIRCUITPY**. If there is no&nbsp; **lib** &nbsp;directory, create one to put the file into. (More info on installing libraries, read information&nbsp;[here](https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-libraries).)

![](https://cdn-learn.adafruit.com/assets/assets/000/077/461/medium800/circuit_playground_projects_Screen_Shot_2019-04-05_at_2.34.35_PM.png?1561429584)

## Uploading

Make sure you've connected the Circuit Playground Express to your computer (mac/PC/Linux) via a known good USB A to micro-B cable. Your board should show up as a flash disk drive named&nbsp; **CIRCUITPY** &nbsp;(If you see a disk name&nbsp; **CPLAYBOOT** , try pressing the reset button again. If the only drive name you get is named&nbsp; **CPLAYBOOT** , CircuitPython may not be loaded on the board. You can load CircuitPython as&nbsp;[per this guide](https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart)).

Once your board is connected, copy&nbsp; **code.py** &nbsp;from the window below. You can select Download in the upper left tp save the code onto your computer.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CPX_Marble_Maze/code.py

Open the code up in the Mu editor.&nbsp;Press the&nbsp; **Save** &nbsp;button and your code should automatically be saved to the&nbsp; **CIRCUITPY** &nbsp;disk drive (which appears when the Circuit Playground Express is plugged into your computer) as&nbsp; **code.py**.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/077/387/medium800/circuit_playground_projects_makecode_circuitpython_Screen_Shot_2018-09-06_at_9.47.58_AM_copy.png?1561389404)

Alternatively, you can use your operating system file explorer/finder to copy code.py to the **CIRCUITPY** drive.

The board will restart itself and start running the code.

## Changing the Code

The default settings used in this code have been optimized for the particular servos used, but can be tweaked to suit your particular preferences. For example, you can increase NUM\_READINGS from 8 up to 12 if you feel the maze is too jerky, or decrease it down to 2 (but not below 2) if you feel the maze is too slow to respond.

You can also update time.sleep from 0.05 to something like 0.20 to make the movement of the servos smoother.

Once your code is uploaded to Circuit Playground Express you can connect some motors to your board and see what this code does! Read on...

# Tilt Controlled Marble Maze

## Build the Circuit

This project uses two mircro servo motors, powered and controlled using Circuit Playground Express.

The&nbsp; **Vout** &nbsp;pad on Circuit Playground Express (CPX) is connected to each servos' red wire, the&nbsp; **A1** &nbsp;and&nbsp; **A2** &nbsp;pads to the yellow wires, and the two&nbsp; **GND** &nbsp;pads to the servos' brown wire.&nbsp;

The&nbsp;[alligator clip-to-male jumper wire](https://www.adafruit.com/product/3448)&nbsp;connectors are great for connecting servo motors to CPX.

![](https://cdn-learn.adafruit.com/assets/assets/000/077/415/medium800/circuit_playground_projects_CPX_two_servos_bb.jpg?1561403897)

![](https://cdn-learn.adafruit.com/assets/assets/000/077/416/medium800/circuit_playground_projects_IMG_1452.jpg?1561403925)

Once your motors are connected, you can connect your board to a power source and test how the servos respond to the board being tilted along its x-axis and y-axis.

![](https://cdn-learn.adafruit.com/assets/assets/000/077/452/medium800thumb/circuit_playground_projects_IMG_5196.2019-06-24_17_46_45.jpg?1561412832)

If a servo does not move, check that you have form connections on the correct pads. Do not connect to pad A0, that is a sound output pad.

Info: 

# Tilt Controlled Marble Maze

## Build the Maze

## Grid System

&nbsp;

- Find a square-shaped piece of cardboard.
- Create a square grid (8"x8" / 20x20cm or 10"x10" / 25x25cm are good starting places).

![circuit_playground_projects_IMG_5061.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/296/medium640/circuit_playground_projects_IMG_5061.jpg?1561150475)

![circuit_playground_projects_IMG_5062.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/297/medium640/circuit_playground_projects_IMG_5062.jpg?1561150508)

![circuit_playground_projects_IMG_5063.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/298/medium640/circuit_playground_projects_IMG_5063.jpg?1561150520)

## Sketch your Maze

&nbsp;

- Using a dark marker, sketch out where the walls of your maze will go.&nbsp;
- Make it as simple or complex as you want! You can look at other examples of mazes online for ideas.

![circuit_playground_projects_IMG_5064.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/299/medium640/circuit_playground_projects_IMG_5064.jpg?1561150565)

![circuit_playground_projects_IMG_5066.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/300/medium640/circuit_playground_projects_IMG_5066.jpg?1561150578)

## Maze Walls

&nbsp;

- Cut strips of cardboard to fill in the walls of the maze.
- Use hot glue to start adding walls one at a time.

![circuit_playground_projects_IMG_5067.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/301/medium640/circuit_playground_projects_IMG_5067.jpg?1561150610)

![circuit_playground_projects_IMG_5068.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/302/medium640/circuit_playground_projects_IMG_5068.jpg?1561150623)

![circuit_playground_projects_IMG_5069.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/303/medium640/circuit_playground_projects_IMG_5069.jpg?1561150640)

![circuit_playground_projects_IMG_5070.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/304/medium640/circuit_playground_projects_IMG_5070.jpg?1561150686)

Warning: 

A beautiful cardboard maze, ready to be robotically navigated.

![](https://cdn-learn.adafruit.com/assets/assets/000/077/305/medium800/circuit_playground_projects_IMG_5071.jpg?1561150721)

## Frame

&nbsp;

- Build a square frame to hold the maze using some strips of cardboard.
- Connect a servo to one edge of the frame, connect the maze to the opposite edge of the frame with a short bolt. _This servo will control the x-axis_.

![circuit_playground_projects_IMG_5096.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/306/medium640/circuit_playground_projects_IMG_5096.jpg?1561150797)

![circuit_playground_projects_IMG_5104.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/307/medium640/circuit_playground_projects_IMG_5104.jpg?1561150886)

![circuit_playground_projects_IMG_5107.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/308/medium640/circuit_playground_projects_IMG_5107.jpg?1561150900)

![circuit_playground_projects_IMG_5106.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/309/medium640/circuit_playground_projects_IMG_5106.jpg?1561150971)

## Base

&nbsp;

- Create a base to hold the maze and frame.&nbsp;
- Install a second servo motor in one side. _This servo will control the y-axis._

![circuit_playground_projects_IMG_5099.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/310/medium640/circuit_playground_projects_IMG_5099.jpg?1561151097)

![circuit_playground_projects_IMG_5101.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/311/medium640/circuit_playground_projects_IMG_5101.jpg?1561151113)

![circuit_playground_projects_IMG_5102.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/312/medium640/circuit_playground_projects_IMG_5102.jpg?1561151122)

![circuit_playground_projects_IMG_5108.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/313/medium640/circuit_playground_projects_IMG_5108.jpg?1561151191)

![circuit_playground_projects_IMG_5109.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/314/medium640/circuit_playground_projects_IMG_5109.jpg?1561151201)

![circuit_playground_projects_IMG_5110.jpg](https://cdn-learn.adafruit.com/assets/assets/000/077/315/medium640/circuit_playground_projects_IMG_5110.jpg?1561151213)

Use hot glue to connect the servo arms to the edge of the maze frame. Once the glue has cooled and hardened, press the arm onto the servo horn.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/077/316/medium800/circuit_playground_projects_IMG_5137.jpg?1561151399)

Once the maze has been suspended in its frame, you can connect the servo motors up to Circuit Playground Express and test out its responsiveness.

![](https://cdn-learn.adafruit.com/assets/assets/000/077/453/medium800thumb/circuit_playground_projects_Marble_Maze_demo_720.2019-06-24_16_54_30.jpg?1561414310)

Enjoy playing with your robotic labyrinth!


## Featured Products

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

Out of Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)
### Small Alligator Clip to Male Jumper Wire Bundle - 6 Pieces

[Small Alligator Clip to Male Jumper Wire Bundle - 6 Pieces](https://www.adafruit.com/product/3448)
When working&nbsp;with unusual non-header-friendly surfaces, these handy cables will be your best friends! No longer will you have long, cumbersome strands of alligator clips. These compact jumper cables have a premium male header on one end and a grippy mini alligator clip on the...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3448)
[Related Guides to the Product](https://learn.adafruit.com/products/3448/guides)
### Micro servo

[Micro servo](https://www.adafruit.com/product/169)
Tiny little servo can rotate approximately 180 degrees (90 in each direction) and works just like the standard kinds you're used to but _smaller_. You can use any servo code, hardware, or library to control these servos. Good for beginners who want to make stuff move without...

Out of Stock
[Buy Now](https://www.adafruit.com/product/169)
[Related Guides to the Product](https://learn.adafruit.com/products/169/guides)

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Combo Dial Safe with Circuit Playground Express](https://learn.adafruit.com/combo-dial-safe-with-circuit-playground-express.md)
- [Personal UV HotSpot Parasol](https://learn.adafruit.com/personal-blacklight-uv-parasol.md)
- [Custom Controllers for MakeCode Arcade](https://learn.adafruit.com/custom-controllers-for-makecode-arcade.md)
- [Jack-o-Theremin](https://learn.adafruit.com/jack-o-theremin.md)
- [SpaceX Helmet](https://learn.adafruit.com/spacex-helmet.md)
- [NeoPixels with MakeCode](https://learn.adafruit.com/neopixels-with-makecode.md)
- [Four Seasons Fairy Bottle Lanterns](https://learn.adafruit.com/four-seasons-fairy-bottle-lanterns.md)
- [Cyberpunk Santa Eye](https://learn.adafruit.com/cyberpunk-santa-eye.md)
- [Musical Walking Stick with Circuit Playground](https://learn.adafruit.com/musical-cane-walking-stick.md)
- [Anatomical 3D Printed Beating Heart with MakeCode](https://learn.adafruit.com/anatomical-3d-printed-beating-heart-with-makecode.md)
- [Flapping Halloween Vampire Bat](https://learn.adafruit.com/flapping-halloween-vampire-bat.md)
- [Makecode para la Circuit Playground Express](https://learn.adafruit.com/makecode-es.md)
- [Installing Microsoft MakeCode for Adafruit](https://learn.adafruit.com/installing-makecode-for-adafruit.md)
- [Getting Started With Steven Universe](https://learn.adafruit.com/getting-started-with-steven-universe.md)
- [Circuit Playground or Hallowing Jack-o'-Lantern](https://learn.adafruit.com/circuit-playground-jack-o-lantern.md)
