Learn to code

ARDUINO & CODES

Previous Post
RFID door Lock with multiple RFID readers for Arduino
Next Post
Creating a proximity touch password lockbox with Neopixels and Arduino

Create a Marble Maze / Labyrinth for Arduino

in Arduino Project

In this project tutorial, you will create an Arduino Maze / Labyrinth that is controlled with two servos. In addition, you can use it as a puzzle to open a lock. The lock will be controlled by using a magnetic door switch that is triggered by the magnetic marble used in the Maze.

This project is using foamboard for the construction of the platform and the maze itself. Foamboard is easy to cut and is also strong in comparison to cardboard.

What you will need to know?

You should have read the notes on Arduino Tutorial: 6.2 Control a Servo Motor with a Potentiometer since you will use two servos in combination with two potentiometers. Furthermore you should know the basics of electronis as well as How to use a Breadboard. It will also help if you would read Introduction into Arduino DC Motor Control with Arduino.

You will use the map function for this project. If you are unfumiliar with this function you can read Arduino Tutorial: 4.8 Map function for analog results.

In addition, if you decide to make the maze / labarynth with the solenoid lock knowledge of the magnetic door switch in combination with the solenoid lock will be handy. You can read about this at How to use a Magnetic Contact Switch with Arduino.

If you read all the tutorials, or already know this, you are ready to build your own Arduino Maze / Labarynth.

Parts you will need

Arduino Uno
BreadBoard
Jumper Wires
Servo’s
DC Barrel Jack
12V Solenoid Lock
100 uF Capacitors
Potentiometers
Door Switch
Magnetic Ball

Circuit Diagram

Setup of the BreadBoard

The first connection that you will make is to connect the power and ground to the BreadBoard. You will use the 5V output for this project. Remember, the servo’s that you bought should be able to receive 5V input. We also add two capacitors to the board. This will regulate/smoothen the voltage that is directed to the servo motors.

Capacitor Warning! In the servo tutorials you can see that capacitors are added at the servo motor and the potentiometer. Only if you get bad readings from the potentiometer or the servo motors you could use the capacitors.

Connecting the Servo Motors

If the board is setup you can create the first connection. Firstly, you will connect the servo motors to the breadboard. You will use Pins 9 and 10.

Safety Warning! Not all servos have the same wiring colors. For example, the Hextronik servos that come with Adafruit’s ARDX kit use red for +5V, brown for ground, and mustard yellow for control. Check the specifications on your particular servomotor to be sure.

Connecting the potentiometers

Now you are going to attach the servo motors to the breadboard. Both of the motors are connected to the 5V and Ground buse of the breadboard. Pin A0 and A1 are used for the connection to the Arduino.

Connecting the magnet switch

After connecting the servo motors and the potentiometers you will connect the magnet reed switch. This switch enables you to detect the magnet ball that is in the maze.

Connecting the solenoid lock

The last part of the breadboard is to connect the solenoid lock. You do this by adding a relay between the breadboard and the solenoid. You are using pin 7 for sending the signal to the relay to open the lock. The ground and VCC are also connected to the breadboard.

Building the Maze

1. Cut out one 30 cm by 30 cm square for the base of the labyrinth.
2. Cut out one 24 cm by 24 cm square for the lay-out of the labyrinth.
2. Cut out two 15 cm by 20 cm squares for the sides of the labyrinth.
3. Make a rectangle hole in one of these two pieces for the servo motor (X axis).
4. Drill a 3mm hole in the other piece in the exact middle to be against the servo shaft in the other side (X axis).
5. Next you will need to cut out four 28 cm by 8 cm rectangles for the support in the interior of the labyrinth
6. Drill a 3mm hole in the other piece in the exact middle to be against the servo shaft in the other side (Y axis).
7. Make a rectangle hole in the middle as shown in one of these four pieces for the servomotor (Y axis).
8. Make squares in you maze by drawing squares of 4 cm by 4 cm. This will outline the maze.
10. You will need to cut out at least eight 2 cm by 12 cm rectangles for the walls in your labyrinth. Next you will need to cut them so that they are the right size and can fit in your labyrinth.
11. Follow the guide lines then glue the pieces in its place to become the wall of the maze.
12. Since the maze will move a lot, you can secure the servo motors with glue to ensure the lifetime of the maze.

Building Warning! Before attaching the servos with glue test you assembly first. This is because if you need to align the servos with the potentiometer first. In other words, if the potentiometer shaft is half turned the servo can be attached to the maze. So turn the shaft of the potentiometer, with the servo attached to it (not to the maze), half way. The servo will turn, if this happens you have the created the right position for the servo to be attached to the maze. Do this for the X and Y axis

The Code

// Control an Arduino Maze with two servos
// wwww.arduinoplatform.com

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

int potpin1 = A0; // analog pin used to connect the potentiometer
int potpin2 = A1; // analog pin used to connect the potetiometer
int value1; // variable to read the value from the analog pin
int angle1; //variable to store the mapped angle
int angle2; // variable to read the value from the second analog pin
int value2; // variable to store the second mapped angle
int magswitch = 4; // pin that is connected to  the door switch
int magswitchvalue;
int const relay = 5;

void setup()
{
  myservo1.attach(9); // attaches the servo on pin 9 to the servo object
  myservo2.attach(10); // attaches the servo on pin 9 to the servo object
  pinMode (magswitch, INPUT_PULLUP);
  pinMode (relay, OUTPUT);
  Serial.begin(9600);
}
void loop()
{

  value1 = analogRead(potpin1); // reads the value of the potentiometer

  Serial.print("value1: ");
  Serial.print(value1);

  angle1 = map(value1, 0, 1023, 60, 100); // scale it to use it with the servo
  myservo1.write(angle1); // sets position to the scaled value

  Serial.print(", angle: ");
  Serial.println(angle1);

  delay(15);

  value2 = analogRead(potpin2); // reads the value of the potentiometer

  Serial.print("value2: ");
  Serial.print(value2);

  angle2 = map(value2, 0, 1023, 40, 110); // scale it to use it with the servo
  myservo2.write(angle2); // sets position to the scaled value

  Serial.print(", angle2: ");
  Serial.println(angle2);
  delay(15);

  magswitchvalue = digitalRead(magswitch);
  if (magswitchvalue == HIGH) {

    digitalWrite(relay, HIGH);
    Serial.print("Door Switch: ");
    Serial.print(magswitchvalue);
  }
  else {
    digitalWrite(relay, LOW);
    Serial.print("Door Switch: ");
    Serial.print(magswitchvalue);
  }

}


Code Explanation

Declare the variables

First you are going to declare the variables that you will use in this project.  The project is not that hard to code however, there are many variables in this project to consider. You are needing variables for the servo motors, the potentiometer, the relay and for the magnetic reed switch.

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

int potpin1 = A0; // analog pin used to connect the potentiometer
int potpin2 = A1; // analog pin used to connect the potetiometer
int value1; // variable to read the value from the analog pin
int angle1; //variable to store the mapped angle
int angle2; // variable to read the value from the second analog pin
int value2; // variable to store the second mapped angle
int magswitch = 4; // pin that is connected to  the door switch
int magswitchvalue;
int const relay = 5;

Read out the potentiometers

To be able to move the servos you will need to read the value of the potentiometers. You will use the analogRead command for this. The map function is also used since we want to convert the value received from the potentiometer into degrees for controlling the servo motor.

value1 = analogRead(potpin1); // reads the value of the potentiometer

  Serial.print("value1: ");
  Serial.print(value1);

  angle1 = map(value1, 0, 1023, 60, 100); // scale it to use it with the servo
  myservo1.write(angle1); // sets position to the scaled value

  Serial.print(", angle: ");
  Serial.println(angle1);

  delay(15);

  value2 = analogRead(potpin2); // reads the value of the potentiometer

  Serial.print("value2: ");
  Serial.print(value2);

  angle2 = map(value2, 0, 1023, 40, 110); // scale it to use it with the servo
  myservo2.write(angle2); // sets position to the scaled value

  Serial.print(", angle2: ");
  Serial.println(angle2);
  delay(15);
Control the Solenoid Lock
After you build the maze and are able to control it with the potentiometers the goal is to go with the magnetic ball to the end of the maze. If you achieve in that a lock is opened.
 
A nice variation to this is to attach a buzzer to this project instead or simulatnious with the lock that is activated if the magnetic ball is in contact with the reed switch.
 
    digitalWrite(relay, HIGH);
    Serial.print("Door Switch: ");
    Serial.print(magswitchvalue);
  }
  else {
    digitalWrite(relay, LOW);
    Serial.print("Door Switch: ");
    Serial.print(magswitchvalue);
  }

}


2 Comments. Leave new

Leupold Vx I Lrp Canada
29 January 2020 14:53

Thank you for sharing this!! Keep up the good work. I’ll be back 🙂

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu