Introduction: Lighting Up an LED With Leap Motion on Mac OSX (For Beginners)

This beginner level instructable will be the gateway to combining the power of Arduino and Leap Motion. You will learn how to light up an LED when the Leap Motion device detects your hands. Once you have completed this, hopefully you can expand on what you have learned.

This will be demonstrated using Mac OSX.

Basic knowledge of circuit breadboard layout will be helpful but is not required.

Basic knowledge of programming is helpful but also not required.

Let's get started!

Step 1: Gather Your Components

These are all of the required components to build this project.

1. Laptop or desktop that runs Mac OSX.

2. Arduino Uno (Quantity = 1. Comes with usb cable)

3. Leap Motion Controller (Quantity = 1. Comes with usb cable)

4. Any text editor for Mac OS. (TextWrangler) This is a free text editor download.

(The following components can be bought at your local electronics store such as Circuit City. Most of these are also available on amazon. For these products it may be helpful to buy more than the quantity listed incase one of the components do not work)

5. 1000 Ohm Resistor (Quantity = 1)

6. Multiple Male to Male Jumper Wires (Quantity = 2)

7. LEDs (Quantity = 1. Any color will do)

8. Small breadboard (Quantity = 1)

Step 2: Arduino Software Download

You can download the Arduino Software using this link.

To download the most recent version, here is the homepage. It will be in the "Download" tab. The most recent version is recommended.

Download the version for Mac OS and follow the installation step during the installation.

***You do not need to have the Arduino device plugged in****

Once this has finished downloading, you are ready for the next step.

Step 3: Leap Motion Software Download

Take the Leap Motion Device out of its box packaging.

Connect the usb cable that came with the Leap Motion, connect it to the Leap device and one of your usb ports on your laptop or desktop. It should automatically ask you to download the required software if it was your first time connecting the Leap Motion. If that doesn't show up you can use the link here. Make sure to download the software for Mac OS. Follow the required instruction during the installation.

Once the installation has completed you should see an icon similar to the one on the far left (Bottom Picture in the Image section). If you do not you can go to your "Applications" Folder in "Finder" and double click "Leap Motion" to have the icon show up.

Once you have completed this step you can move on to the next step.

Step 4: Node.JS Download

This is a software tool that is necessary to have your Arduino and Leap Motion communicate with each other. For this step you do not need anything plugged in.

However you do want to create a folder before the download to hold all of the files from here on out. Create a folder on your desktop page called "LeapMotionLED".

Now, you can download this tool here. The "Mature and Dependable" version will work perfectly. If prompted, use the "LeapmotionLED" folder as the destination installation.

Once this is completed, move on to the next step.

Step 5: Mac OS Terminal Johnny-Five Download (Part 1)

Johnny-Five is another a software tool related to NodeJS that is required for communication between the Arduino and Leap Motion.

You will be using the Terminal (already provided in Macs) to download Johnny-Five.

To open it, go to your "Applications" folder using Finder, go to the "Utilities" folder, and double click on "Terminal". Or you can use Mac's Spotlight tool and search "Terminal" to open it. If your Terminal background is a different color, that will not affect anything.

A window should pop up and it should look similar to the photo provided in the image above. You will have a cursor that will constantly blink. It is ok if your screen has a different "username" or last login information. It is different on every laptop.

On the terminal screen you can type different commands. The commands you will be using will allow you to go in the "LeapMotionLED" folder (you have created it previously) in the Terminal page.

Type "ls" and hit enter. This command will list all of the folders that are in your Terminals current directory (folder). In that list you should see a "Desktop" in list like in the image (black background) provided for this step.

Then type in "cd Desktop" and hit enter. This will go into your "Desktop" folder where you have placed the "LeapMotionLED" folder.

If you then type "ls" and hit enter, you should see a folder listed as "LeapMotionLED". If you don't quit terminal and try again.

Then type "cd LeapMotionLED" and hit enter to go into that folder.

Stay on this Terminal screen because it is required for the next step.

Step 6: Mac OS Terminal Johnny-Five Download (Part 2)

While you are still on the Terminal screen type in "npm install" and hit enter.

Right after you hit enter, there will be a lot of information that will continuously appear on the Terminal screen. This is supposed to happen so just sit tight and wait for the "npm install" to finish.

This process may take more than a couple seconds to several minutes. The time is different on every computer.

The Terminal screen will show a message saying the installation is completed.

You can quit out of Terminal now. To get back to the "LeapMotionLED" folder you can use the Terminal commands "ls" and "cd FolderName" until you get into the "LeapMotionLED" folder.

To learn more about Terminal Commands you can use this link.

Step 7: Building the Circuit

For this step, do not connect the Arduino to your laptop or computer.

If you know your way around a breadboard, a resistor, and an LED, you can make a series connection between the led and the resistor. The input is coming from "Pin13" on the Arduino and the Ground goes to "GND" on the Arduino.

If you have no experience in circuits not to worry. All you have to do is follow the configuration shown in the picture. Layout the breadboard in front of you so that the blue and red colored lines are placed horizontally in front of you. The size of the breadboard does not matter.

Take the 1000 ohm resistor and place it horizontally like in the image. You can bend the metal pins attached to the resistor so that it goes into the breadboard. Place the metal ends into the breadboard slowly until it doesn't go in any further. The orientation does not matter as long as it is placed horizontally in front of you and parallel to the blue and red colored lines on the breadboard.

Next take a look at your LED. The two metal pins on the LED have different lengths. You can also bend these metal pins. Place the longer pin on the same vertical slot as the left end of the resistor like shown in the picture. It will be just one hole above it.

Finally take one of your jumper wires, (the colors do not matter), and insert one end to the Arduino pin labeled "13", (also shown in the photo). Take the other end and place it in the same vertical slot as the right end of the resistor.

Take your second jumper wire and connect one end to the "GND" pin on the Arduino. Connect the other pin of the jumper wire in the same vertical slot as the left end of the LED.

Step 8: Uploading Code for Arduino

This step will now require you to hook up the usb cable from the Arduino to your laptop or computer. The cable will fit on the Arduino's silver rectangular port. The circuit should still be connected to the Arduino. Also plug in your Leap Motion device and open Leap Motion in the Applications folder. (Finder > Applications > LeapMotion)

Hook up the Arduino to your laptop/computer.

Now open the Arduino software in your applications folder. (Finder > Applications > Arduino). A little window should pop.

Next go to File > Examples > Firmata > Standard Firmata

This should automatically open another window with this example code. You can close the first window that popped up.

Next click the Check mark located on the top left of the Arduino window. Wait for the program to finish.

Once finished, next hit the Right Arrow button which is located on the right of the Check mark. Wait for the program to finish.

Step 9: Code for Leap Motion (Part 1)

Make sure that your Leap Motion is still plugged in and the device is on. Also make sure you have a text editor such as TextWrangler.

For this step we will learn how to use the Terminal screen to upload the "test.js" code (attached file) that will control the Leap Motion. Later I will explain what the JavaScript code does, but for now all you need is to open the attached code, "test.js".

Once opened, highlight the code and copy it. Open your text editor and paste the code onto the new text editor screen. Once copied save the file in your LeapMotionLED folder and name it "test.js".

Open your Terminal screen and go to the LeapMotionLED folder where "test.js" is saved. The commands are "cd xxxfoldername", "ls", and "cd .." Remember to hit enter after each command.

If you are in the LeapMotionLED, you can type "ls" in the Terminal and "test.js" should show up in the list. If it does you can go to the next step which is running the code using Terminal.

If it doesn't restart Terminal and try again. Make sure the "test.js" file is in the LeapMotionLED folder.

Step 10: Code for Leap Motion (Part 2)

This step is now to run the code. You must be in the LeapMotionLED folder in terminal to do this step. Also make sure you see "test.js" when you use the "ls" command on terminal.

Now all you need to type on Terminal is "node test.js" and hit enter.

The Terminal screen should now output "Looking for connected device". This process will now connect your LeapMotion and Arduino and have them communicate through your computer.

There you have it! The LED will be lit when your the Leap Motion detects your hands. The LED will turn off if the Leap Motion does not detect your hands.

The next step will talk about the code in "test.js"

Step 11: Code Explanation

"var webSocket = require('ws')" obtains the required location inside your computer from which the Leap Motion sends the data.

"ws" = new webSocket('ws://127.0.0.1:6437')" saves the data location so that the user can access it later

"five = require('johnny-five')" gathers information on all of the johnny-five utilities you have downloaded earlier using Terminal.

"board = new five.Board()" checks to see if your Arduino board is connected.

"led, frame, servo, palm" are variables to store integer numbers.

"board.on('ready', function()" gets your Arduino board ready for data communication.

The code after "//" are just comments. These lines don't run in the code. They are just there for user information. If the user want to use servos, they can delete "//" to access the servo utilities. But in this case we only use the LED so the lines with "//" are not important for this tutorial.

"ws.on('message', function(data, flags)" allows data transfer over from the Leap Motion to the Arduino.

"fingers = JSON.parse(data);" gathers data about your finger locations.

"frame = JSON.parse(data);" gathers data about your palm locations.

"if (frame.hands && frame.pointables.length > 0{

led.on();

}

else {

led.off();

}"

this checks how many hands are being detected by the Leap Motion. If more than 0 hands are being detected the led will light up. If 0 hands are detected the LED will be off.