Self-Opening Box

by ruadhjames in Circuits > Arduino

87 Views, 0 Favorites, 0 Comments

Self-Opening Box

gifIntro.gif
BoTW Box Arduino Video

This project is a self-opening box that uses a servo and accelerometer to open a laser-cut box, inspired by an interaction from the video game The Legend of Zelda: Breath of the Wild. This is not only a love letter to one of my favorite games, but if designed to be more portable and contained, could be used as a fun chest to keep things in.

Supplies

Materials

1x Arduino Uno

1x Arduino Breadboard

1x Arduino Piezo 24v Electronic Buzzer

1x Micro Servo SG90 - 9g

1x MPU-6050 Accelerometer and Gyroscope 3-Axis Module 3.3V-5V

16x Jumper Wire

1x Laser Cut Box (https://community.glowforge.com/t/hinged-lid-box-svg-generator/121731)

1-3x Zipties

1x Popsicle Stick


Tools

1x Soldering Iron

1x Lead/Soldering Metal

Tape

Concept Description / Early Iteration

example.jpg
interation 1.png
sketch.png

Early Iteration + Testing Concept

The very first iteration of my design used a photoresistor to trigger a piezo and 2 servos. The concept behind this was to cause the sound to begin when enough light entered the box, and then triggered the servos seperately with my accelerometer, though after some testing, I made some adjustments. Firstly, I realized I only needed one servo, since it happened to be strong enough to lift the box. This made the wiring a lot simpler, and the assembly less clunky. Secondly, I came to the conclusion that it was unecessarily complex to have the photoresistor trigger the noise, and that I could just couple it with the piece of code that triggers the servos.


Reflection

There were a lot of cool learning experiences during my time working on this project. I soldered with very little experience, worked with Arduino which I had never done before using a component did not come with the kit, and learned to design a circuit, all of which felt very rewarding to complete. There were many things I would change, however. Firstly, my soldering was sub-par, and I ended up resoldering a lot of my board near the end of my project on a new board, since it stopped functioning after soldering it the first time. I also had a large problem with cable management, and could have much more considerately desgined the box to allow for more room, shortened the wires, cut a hole near the back to allow the wires to come through, or use a battery. I could have also used a bigger box and made a fake wood bottom to conceal the circuitry. I also am not very satisfied with the quality of the box itself, I had to settle for one which was taped together.

Designing the Board

board.png
gifofitworking.gif

*Note - The 4-pin LED in the circuit above is supposed to represent the MPU6050 accelerometer, as the website I used to design the board did not come with an accelerometer component.


The Design

The design of this board is organized specifically with the arduino and breadboard being next to each other in mind. I used a box that would allow for both of them to fit next to each other, and ideally, this would have meant shorter wire distances - which was true for the pre-soldered version of my board, but not true for the new version I had to make after being unable to fix the first one.

Cutting the Box

booooxxx.jpg
Screenshot 2024-10-11 160042.png

https://3axis.co/laser-cut-hinged-box-3mm-dxf-file/eoxlze4o/

https://cuttle.xyz/@cuttle/Hinged-Lid-Box-rTDUUz652dVO

I used the link above to print a laser-cut box that would allow enough space for my circuits to fit inside. I considered using the latter, which would allow me more control over the dimensions, but the one linked worked just fine, and I found the customizable one too late. The second one is also given as an option for people who might want to recreate this project using a better box and remedy my own mistake.

Coding the Arduino

Adafruit Sensor Library: https://github.com/adafruit/Adafruit_Sensor

Adafruit MPU Library: https://github.com/adafruit/Adafruit_MPU6050

//Used Adafruit libraries for the accelerometer, and learned how the component worked from the example sketches that came with the library
#include <Servo.h>;
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

// Setting up the servo and the accelerometer
Servo servo1;
Adafruit_MPU6050 mpu;

//The isMoving bool is used later in loop() to trigger an if statement a single time, and //prevents the box from trying to open multiple times a second. The accelThreshold is used //to set up the sensitivity for the accelerometmer, and how much it needs to be displaced //to activate the servo. Note we're not using the gyroscope, so rotation won't trigger the //system, it's just using acceleration variables.
bool isMoving = false;
float accelThreshold = 10.0;
float accel_x, accel_y, accel_z;

void setup()
{
//The higher baud rate is necessary for accurate accelerometer readings
Serial.begin(115200);
//Pin 9 is used by the Piezo
pinMode(9, OUTPUT);
//Pin 11 is used by the servo
servo1.attach(11);
//We set the servo's position to 0 to make calibrating the opening feature easier
servo1.write(0);

Serial.begin(115200);
//Give the circuit a bit of a buffer between uses to minimize mistakes
while (!Serial)
delay(10);

//This code came with the MPU library, and mpu.begin scans analog pins to try and identify //if there's an MPU attatched to the arduino
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("Found a MPU-6050 sensor");

//These also came with the library, and establish a range of values and degrees for the //accelerometer to follow
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);

delay(100);

//This calls for acceleration (a) and gyro-rotation (g) and sets them to values to be used //later
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
accel_x = a.acceleration.x;
accel_y = a.acceleration.y;
accel_z = a.acceleration.z;
}

void loop()
{
// Same thing as before, but now being updated constantly to read live values from the accelerometer
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);

float accel_x_new = a.acceleration.x;
float accel_y_new = a.acceleration.y;
float accel_z_new = a.acceleration.z;

Serial.println(a.acceleration.x);
Serial.println(a.acceleration.y);
Serial.println(a.acceleration.z);

//This if and or statement checks the displacement in acceleration with the acceleration //threshold, and if it surpasses it, it tells the arduino the circuit is active by setting //isMoving to true
if (abs(accel_x_new - accel_x) > accelThreshold ||
abs(accel_y_new - accel_y) > accelThreshold ||
abs(accel_z_new - accel_z) > accelThreshold){
isMoving = true;
}

accel_x = accel_x_new;
accel_y = accel_y_new;
accel_z = accel_z_new;

//when ifMoving is set to true, this if statement triggers
if (isMoving)
{
//This makes the servo move 90 degrees, this is adjustable depending on how much you want //your box to open
servo1.write(90);
//Calls startTune
startTune();

//This delay keeps the box open for a fixed amount of time, you can make it as big or //small as you like, then it closes the box by setting the servo back to 0
delay(5000);
servo1.write(0);
//isMoving is set back to false to ensure this if statement isn't triggered again
isMoving = false;

sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);

accel_x = a.acceleration.x;
accel_y = a.acceleration.y;
accel_z = a.acceleration.z;
}

delay(100);
}

//startTune just uses the piezo's 'tone' feature to play a sound depending on hertz. I //used a graph to directly transpose a melody to hertz, they're not difficult to find
void startTune()
{
Serial.println("Open!");
analogWrite(9, 255);

tone(9, 392);
delay(160);
noTone(9);

tone(9, 370);
delay(160);
noTone(9);

tone(9, 311);
delay(160);
noTone(9);

tone(9, 220);
delay(160);
noTone(9);

tone(9, 207);
delay(160);
noTone(9);

tone(9, 329);
delay(160);
noTone(9);

tone(9, 415);
delay(160);
noTone(9);

tone(9, 523);
delay(240);
noTone(9);
}

Soldering the Board

IMG_2911.jpg
IMG_2915.jpg
IMG_2918.jpg
IMG_2916.jpg

Soldering First Try

The board in the beginning was a replication of my functioning breadboard soldered close to the conclusion of my project. The images above are the first attempt - most of the circuit seemed to work, but there were a few issues that were likely caused by my lack of experience with soldering that made it nonfunctioning. I am, however, proud that I was able to cut, strip, and solder wires for different lengths, even if there was a small imperfection somewhere on the board that stopped it from working.

Putting Together Project

box.png
Screenshot 2024-10-11 164312.png
servo.png

Assembly

The system itself is quite easy to assemble. As listed in the reflection, if I were to redo this project, I would likely design a more appropriate enclosure for the boards, but for now, I was able to just place the board and arduino next to each other inside the box. In the roof of the box, I placed the accelerometer, taping it down to avoid misfires. In order for the servo to open the box, I mounted a popsicle stick to the top of the motor with a ziptie and fastened it onto the side of the box, so that it would be long enough to lift the lid on its own.

Functioning

BoTW Box Arduino Video

This video displays my project working fully, displaying both the components working properly, as well as the soldering and updated board.