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


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 



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

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.


import sys
import time
from DFRobot_BMX160 import BMX160
import RPi.GPIO as GPIO
import signal  
import atexit

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

servopin = 22
motorpin = 11

bmx = BMX160(1)

while not bmx.begin():
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
        return 0

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

def main():
  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):
    elif(gesture == password[index]):
      index = index + 1
      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))

if __name__ == "__main__":
  while True:

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/

Step 4. Fix the sensor

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


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.



Step 6.  Decorate

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



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!

All Rights