Using LeapMotion to Control an Arduino

Arduino and Leap Motion Controller

Using LeapMotion to Control an Arduino

After surpassing its Kickstarter goal, LeapMotion became one of the first affordable products in the interactive motion controlled peripheral market. I had to get my hands on one, as I could see limitless possibilities within the virtual and physical space. If you’re not familiar with Arduino but would like to learn more, check out my other article on Arduino Basics. Apart from a basic understanding of Arduino, you’ll also need to be familiar with Node.js and introductory Java. That being said, simply installing the modules and pasting the code is a great way to get started. Don’t stop there! Try adding your own spin to this project. We’ll be using LeapMotion, Arduino and a laptop to detect when two hands are placed within the field of the controller, and turn on a light on the Arduino when these hands are detected.

Lets Get Started


Parts

Dig into your handy toolbox and grab:

  • Your Arduino
  • LeapMotion Controller
  • Some USB cables

You’ll also need a laptop.

Software

Download the Arduino SDK if you don’t already have it installed. Next, install the Node.js package on the same computer. You’ll also have to have the LeapMotion SDK installed, in order for the controller to work. The LeapMotion SDK has a nice feature called a websocket server, which streams data from the controller when its plugged in. You may have to enable the server in the settings of the controller, often a tray icon on Windows machines.

Node.js

Once all these main software packages have been installed and configured (just follow the onscreen instructions provided during installation), install the ws library with npm install ws --save

You’ll now need to install the fascinating and very useful Johnny-five module into Node.

Open the Node command line and insert npm install johnny-five --save into the interface. This module allows Node to chat with Arduino through serial, and control the board using Arduino’s StandardFirmata.

Open the Arduino IDE, open the File>Examples>Firmata>StandardFirmata and upload it to your Arduino board. Once this is done, your board will be able to understand and execute commands sent by Node/Javascript.

Putting it all Together


The Code

Create a file in your favorite text editor and name it something, with a .js extension. In other words, your file should look something like file.js

The code is very simple, as we’re only trying to detect the presence of two hands within the LeapMotion’s view. Paste the following into the file we just made:

var webSocket = require('ws'),
 ws = new webSocket('ws://127.0.0.1:6437'),
 five = require('johnny-five'),
 board = new five.Board(),
 led, frame;
board.on('ready', function() {
 led = new five.Led(13); 
 ws.on('message', function(data, flags) {
 frame = JSON.parse(data); 
 if (frame.hands && frame.hands.length > 1) {
 led.on();
 }
 else {
 led.off();
 }
 });
});

The code first looks for the websocket server, and connects to it. Then, it creates a new board function and begins communication with the Arduino. Finally, a simple if-else statement inside the websocket loop looks for the presence of two hands within the LeapMotion’s field of view. When detected, the code will turn on Arduino’s on board LED (pin 13).

Taking it Further


Stay tuned where we’ll control a servo with the LeapMotion Controller!

LeapMotion Servo Control

LeapMotion Servo Control

 

 

 

Comments

comments

Leave a Reply

Your email address will not be published.