Hacking your kit: Kano x Spotify - Motion Controlled Music

Over the past five years, Kano have become one of the most recognisable names in the learn to code sector. They now have a range of products from computers and light-boards to Harry Potter coding kits.

One of their lesser known products is the Motion Sensor Kit — a $29 USB peripheral that can detect the distance between itself and other objects.

This got me thinking, what else could you do with a motion sensor?

The weather in London has been glorious for the past few weeks, so it’s the perfect time to break out the barbecue, pour some drinks, and put on some music!

The only problem is, I don’t want to get greasy marks all over my laptop every time I change the song. And thus, an idea was born! I’ll show you how you can create a small script in Python that reads the data from the Motion Sensor Kit and skips to the next track on Spotify when you wave your hand close enough.


I’m going to be developing my app on Windows 10, but this project will work perfectly well on MacOS or Linux.

First, make sure that you have a copy of Python installed. You can then install PIP, which is a great tool that lets you easily install other useful packages. For more help setting up, Matthew Horne has an excellent guide.

Find the Sensor

The Motion Sensor communicates with the computer using a serial connection, making reading the data a breeze. Open the Command Line and run the mode command. You’ll see any devices that are currently using a serial port.

Connect your Motion Sensor and run the command again. This time, you’ll see an extra device along with the port it’s connected to (in my case, COM7). Make a note of this for the next step.

Install Packages

There are two package we’ll need to link the Motion Sensor Kit and Spotify together. Pyserial will read the data coming from the sensor, whilst pyautogui will simulate the ‘next track’ button being pressed on your keyboard.

From your Command Line, type pip install pyserial pyautogui and press enter. A couple of minutes later, you’ll be ready to continue!

Write some Python

There are four packages we’ll use: serial to communicate with the Motion Sensor and json to read the data, then pyautogui to press the media keys, and time to control how quickly we grab the data.

Open a text editor and copy the imports below:

Open Communications

Getting the data being generated by the Motion Sensor couldn’t be easier. Copy the line below, but replace ‘COM7’ with the port name you noted earlier.

While we’re here (no pun intended!) we’ll add a while loop to keep our script running indefinitely:

Cleaning the Data

The data the Motion Sensor creates has some extra detail that we won’t need for this project:

{u’type’: u’event’, u’name’: u’proximity-data’, u’detail’: {u’proximity’: 32}}

The good news is that the data is in JSON format, making it easy to get the info we need; the proximity. Add the line below to get a reading from the sensor and convert it into a JSON object:

Now we can isolate the proximity information using this code:

Control the Distance

The proximity of objects is measured using a scale from 0 to 255. To prevent the sensor being triggered accidentally, we’ll set a relatively high value. Use the if statement shown below (I’ve added a bit of text that will display to show that we’ve triggered the code):

Control Spotify

We’re using a neat trick to trigger Spotify to change the track. The pyautogui package can simulate a person typing on the keyboard, which we’ll use to “press” the next track button. Add the code below:

You’ll notice I also added a sleep of 1 second. This is to give you enough time to move your hand away from the sensor without accidentally triggering a track change.

Flush to Finish

The serial package we used can create a buffer so that you don’t lose information if a script is paused. In our case, we don’t want this to happen as it will trigger hundreds of track changes! The flushInput command prevents this happening:

Try it out!

Save your file as spotify.py in your home folder. Make sure that Spotify is open, then head back to the command line, type python spotify.py and press enter:

Now wave your hand over the Motion Sensor! Spotify will either play the next song in your playlist or a random song if you switch on shuffle.

Next Steps

The aim of this project was to give you a beginners guide to interacting with the Motion Sensor Kit using Python. You can now use the same basic code for reading the serial data in all your other projects!

Can’t wait to see what you make…

This post was originally created and posted here by Mathew Keegan, who worked at Kano for many years.

More from Mathew Keegan