Kids Project. Shake Animal!

What’s this?

Shake to move an animal!


  • obniz x1
  • power supply for obniz x1
  • DC Motor x1
  • paper cup x2
  • AAA battery x3
  • battery case x1
  • paper and wire for decorations



First, make a hole at bottom of one paper cup.

Fix a motor to a cup with double-sided tape.

Make an animal with the other paper cup and color paper and so on freely.


Connect DC Motor to an obniz like the table and the image below. Please adjust the direction of the DC Motors properly, because rotation direction depends on the DC Motors’ direction.

obnizDC Motor

Put AAA butteries in battery case to case and solder a cable to obniz J1. Be careful not to short circuit!

Finally, cover the other cup (decorated like animals) to the cup.

Run the program below and shake your smartphone! The stronger you shake it, the stronger the animals move.


<!-- HTML Example -->
  <script src=""></script>
  <script src=""></script>
  <div id="obniz-debug"></div>
  <div id="print"></div>

var motor;
var obniz = new Obniz("OBNIZ_ID_HERE")
obniz.onconnect = async () => {

  motor = obniz.wired("DCMotor",  {forward:0, back:1});


var shakeCount = 0;
var x_last = 0;
var vx = 0;
var vx_last = 0;
var ax = 0;
var ax_avg = 0;
var timer;

window.addEventListener("devicemotion", (event) => {
  if (!motor)
  let x = event.accelerationIncludingGravity.x;
  let y = event.accelerationIncludingGravity.y;
  vx = x - x_last;
  x_last = x;
  ax = vx_last - vx;
  vx_last = vx;
  ax_avg = Math.abs(ax) * 0.2 + ax_avg * 0.8;
  if (!timer) {
    timer = setInterval(()=>{
      var value = ax_avg * 5;
      if(value > 50) {
        value = 50;
      timer = null;
    }, 50);
}, true);