Making a Shake-Activated Christmas Gift Box(Based on Raspberry Pi 4B)

0 51231 Easy

Background

Christmas is coming, any ideas for Christmas gifts now? I was trying to seek online for a long time to find special and surprising gifts for my loved ones, but finally, nothing. It seems hard to discover unique presents. Well, at last, I determined, this year, I am gonna spend time on making a special gift box. This box will be opened only when shaken to the right directions in order.

Things we need:

1. Raspberry Pi 4 Model B - 4GB ×1

2. BMX160 9-axis Sensor Module ×1

3. 9g 180° Micro Servo (1.6kg) ×1

4. Gravity: Vibration Motor Module ×1

5. 5V Power Supply for Raspberry Pi ×1

6. A beautiful gift box ×1

7. A small gift ×1

8. Filler

Note:

1. The gift box should not be too big, just a little bit bigger than your gift.

2. Do not put breakable gifts inside this box since we have to shake it to open.

3. I will be using Raspberry Pi 4B here. If you are a Pi newbie, there are a larger number of tutorials online, just Google it. The program for this project is pretty easy.

Step 1. Get Sensor Library

Power up the raspberry, and obtain the BMX160 library through the following command.

git clone https://github.com/DFRobot/DFRobot_BMX160

This library includes Arduino and Python, only the later part will be used.

Step 2. Programming

BMX160 sensor is the smallest 9-axis sensor in the industry. It comprises an accelerometer, gyroscope, and geomagnetic sensor in a single package. I will use it to design “shake password” for the gift box.

CODE
import sys
sys.path.append('../')
import time
from DFRobot_BMX160 import BMX160
import RPi.GPIO as GPIO
import signal  
import atexit

up = 1
down = 2
left = 3
right = 4

atexit.register(GPIO.cleanup)  
servopin = 22
motorpin = 11
GPIO.setmode(GPIO.BCM)
GPIO.setup(servopin,GPIO.OUT)
GPIO.setup(motorpin,GPIO.OUT)
angle=GPIO.PWM(servopin,50)
angle.start(0)

bmx = BMX160(1)

while not bmx.begin():
    time.sleep(2)
bmx.set_accel_range(bmx.AccelRange_2G)
password = [up,down,up,down,left,right,left,right]

def check():
    data= bmx.get_all_data()
    if data[7]<-8 and data[8]>-4:
        return 1
    elif data[7]>8 and data[8]>-4:
        return 2
    elif data[6]<-8 and data[8]>-4:
        return 3
    elif data[6]>8 and data[8]>-4:
        return 4
    else:
        return 0
    

TIMEOUT=20000    #Set Timeout period, unit(mm)

def main():
  index=0
  pd_len = len(password)
  print("password length=%d"%pd_len)
  start_timestamp = time.time()
  print("please input the %d gesture"%(index+1))
  correct = False
  while(correct == False):
    now = time.time()
    if(now - start_timestamp >= TIMEOUT):
        start_timestamp = now
        index = 0
        print("timeout,input again")
        print("please input the %d gesture"(index+1))

    gesture = check()
    #print("**get gesture=%d"%gesture)    
    if(gesture == 0):
      continue;
    
    elif(gesture == password[index]):
      index = index + 1
      GPIO.output(motorpin,GPIO.HIGH)
      time.sleep(0.5)
      GPIO.output(motorpin,GPIO.LOW)
      print("please input the %d gesture"%(index + 1))
    if(index == pd_len):
      correct = True
  
  print("Unlock all gestures successfully, you have entered the system")
  print("To enter the gesture password, you have spent %d seconds"%((time.time()-start_timestamp)/1000))
  angle.ChangeDutyCycle(float(90)/18+2.5)
  time.sleep(20)
  angle.ChangeDutyCycle(float(0)/18+2.5)

if __name__ == "__main__":
  while True:
    main()

Step 3. Set the program to run on Startup

1. Switch to the root account

sudo su

2. Revise rc.local file

sudo nano /etc/rc.local

3. Add command in front of exit()

sudo python /xx/xx/text.py

Step 4. Fix the sensor

Fix the BMX160 sensor to the one corner of the box, pay attention the direction.

projectImage

Step 5 Install and Connect

1. Fix the servo to the side of the box, make it near the box switch. The servo will be the lock of the box.

2. Attach the vibration motor to the bottom of the box. It aims to get feedback every time one password is completed.

3. Fix the Raspberry Pi and 5V power inside the box, wire up all parts together.

projectImage
projectImage

Step 6. Decorate

Use some filler to cover the all electrical elements, then put the small gift inside. Wrap the box with exquisite paper.

projectImage
projectImage

Here it is. Let’s have a try before giving it away. Shake it as the preset directions in order(up, down, up, down, left, right, left, right) to open it.

That’s all for this shake-activated Christmas gift box. Hope my work may help you find inspiration for your Christmas gifts. Thanks for reading, have a happy Christmas!

License
All Rights
Reserved
licensBg
0