How To Make A Secret Locked Box! | Arduino Tutorial

About the project

Create a simple locked box that will never open until you guess the correct combination of buttons!

Project info

Difficulty: Easy

Platforms: Arduino

Estimated time: 1 hour

License: GNU General Public License, version 3 or later (GPL3+)

Items used in this project

Hardware components

Arduino Uno R3 Arduino Uno R3 x 1
Push Button Push Button x 4
1K Ohm Resistor 1K Ohm Resistor x 4
Servo Motor Servo Motor x 1

Software apps and online services

Arduino IDE Arduino IDE

Story

The Video

This project is a locked box. It is a box that can be opened and closed with the use of a servo motor. You can hide anything inside, depending on the size of your box.

You can find my personal blog post here: My Blog Post

You can only open the box by guessing the secret combination of buttons. This a part of the project that can be expanded into a more elaborate secret combination.

Using the Pushbutton:

The Pushbutton can be used for input and output in different ways. In this project, 5V is connected to one pin on the button, and when the button is pressed, the power flows through the button, through a resistor, and into an input pin on the Arduino.

Using the Servo motor:

The servo motor is controlled by Pulse-Width Modulation. This means that the servo’s position is based upon the length of the pulse going to the signal pin. We can use the pre-installed servo library on Arduino to control the servo. To send it a signal, you use servo.write() and pass in the degrees as a parameter.

The Schematic:

Code

  1. //////// Part 1 //////// #include <Servo.h> // Pin variables for later int sPins[] = {4,5,6,7}; int servoPin = 9; // Initialize servo variable Servo servo; // Setup current position variable int pos[] = {0,0,0,0}; // Change this to your combo using 0's and 1's int goalPos[] = {1,1,0,1}; //////// Part 2 //////// // Setup the servo and the switch pins void setupPins() { servo.attach(9); servo.write(-1); for (int i=0; i<4; i++) { pinMode(sPins[i], INPUT); } } // Setup void setup() { setupPins(); } //////// Part 3 //////// // Check if two arrays are equal boolean array_cmp(int *a, int *b, int len_a, int len_b){ int n; if (len_a != len_b) return false; for (n=0;n<len_a;n++) if (a[n]!=b[n]) return false; return true; } //////// Part 4 //////// void loop() { // Checking and setting switch positions for(int i=0; i<4; i++){ int status = digitalRead(sPins[i]); if (status== HIGH) { pos[i] = 1; } else if(status==LOW) { pos[i] = 0; } } // Check if combo is correct if(array_cmp(pos,goalPos,4,4)) { servo.write(95); } else { servo.write(-1); } }

Schematics, diagrams and documents

Schematic

Code

Code

Credits

Photo of Sci Ranch

Sci Ranch

I make robotics projects and other Arduino-themed videos to try to improve small aspects of the world, one project at a time. My goal is that these projects and this knowledge can help people to solve small problems that need to be solved.

   

Leave your feedback...