How to control servo motor using Leapmotion and Arduino

Control Servomotor For Making Robotic arm with Leapmotion and Arduino

Hello everyone,

In this quick tutorial, you will learn how to control servo motors with Leap motion controller using Arduino. The Robotic arm with leap motion.The technology of this 3D motion detector sensor made a great change in our projects. The world is now controlling electronic devices with the leap motion controller.
The aim of this post is to show you how to control a servo motor and then leads it to control a robotic hand with the help of motion detector sensor using with Arduino. In this post, we will look a couple of platforms that can get you to right and easiest way of control and use of 3D sensor technology.

For getting started with it. First of all, you should know about the Leap Motion technology. It’s all about your hands detect in the 3D frame.  It detects your hand, wrist, elbow and also worked for your shoulder while you are making a robotic arm with it. This motion sensor senses your waving hands and shows it in 3D form. It tracks 200 frames per second with its cameras. it has the ability to detects both hands, fingers and things look like fingers. From the hardware perspective, the motion detector sensor is quite simple. It has consisted of two cameras and three IR (Infra rays) L’s that makes an area of detecting the hand, wrist etc.
The charm of this Leapmotion technology increase when its join with real world hardware. These all can be done with the help of microcontrollers like Arduino, Raspberry Pi etc.

Watch the demonstration Video on our YouTube Channel.

For connecting hardware with leap motion sensor using Arduino, there will be two software used. The controlling purpose of 3d motion controller device we used Processing software. Processing is an open-source computer programming language. Java-based software in which we are programming in Java language. It has a collection of libraries that help to control the commands of leap motion controller and then sends these all commands to Arduino. Download the processing software and its libraries if you don’t already have installed.

Read: Processing software and library for leap motion and Arduino


Here we are using Arduino as a microcontroller to communicate Leap motion controller with hardware like servo motor. The simple work is read data from a leap in the form of you hand gestures in X, Y, Z axis and pass it to Arduino for Arduino basics getting-started-with-Arduino you don’t have to install any extra library in Arduino software. There is a built-in library in it.

For controlling a servo motor with leap motion and Arduino we are using the y-axis of leap device to control the angles of the servomotor. We are sending the commands via serial ports of processing to Arduino IDE.

In processing, you can use leap motion controller of any axis X, Y, Z but we are using x-axis to control our servo angles from 0 to 180 degree. This is seems something out of science fiction to make a movement of physical things by only gestures of your hands but we just made it in reality.


Circuit Diagram

Attach servo with pin # 9 to control it with Hand gesture recognition technology. Make sure that VCC is connected with 5volt and black wire of motor connected with Ground. After uploading the code which is given below it turns first 0 degrees then tracks gestures on the y-axis and changed its angles as per gestures changes.

Coding For Processing

First of all in the coding of processing import the library of leapmotionp5. Remember that the library given in my previous post is only best when you are using it for led blink function. For servo motor, you have to add a new library of motion sensor name leapmotionp5. This library can be used for controlling the servo motor. Here I am giving you link for downloading the library for servo motor LeapMotionP5 Library


Download this library and add it to the inside of processing folder libraries. Note that you can use only one library at a time so remove the previous library of named leap motion for processing. For removing the previous library open you processing software click sketch than in drop-down list click add a library and remove the library of leap motion for processing.


Here import com.onformative.leap.LeapMotionP5; is import all the functions of 3D technology sensor.

Here import command of fingers. Leap Motion can detect all fingers, wrist angles, palm, pitch, yaw, roll and waving of hands these all the process that can be used for controlling servo motors with a leap motion controller with Arduino. With the help of cameras motion detector sensor captures the images and process them to see which gesture exactly moves. It can only work if gestures moving in a limit. Limit of motion control sensor is from 1 inch to 2*2 feet.

Making a class of leap and making an integer of name angle which is used further for storing the angles of the servo motor and send it to Arduino. import processing.serial.*; used to communicate the processing software commands and sends it to Arduino via the serial port. At detection of gestures in motion sensor along Y-AXIS the particular value assigned to servo motor via leap motion controller then processing and Arduino interchanged the value with each other by serial-communication.
In setup when running the program in processing new windows open. The size of the new window is 720*720. In this windows, you can see the 5 fingers dots. You can change the size of the window it is up to you. If you wave your hand gestures after recognizing the movements of gestures your gestures shown in that window.

Here make the positions of gestures and mount it on angles of the servo. As mention above when you run the program a new window open which is 720 height and width. This window is used for controlling the position of the servo with 3D hand recognition technology. If hands at above the 720 size the servo angle move to 180 degrees. If the position of hand gestures are below the zero the angle turns to 0 degrees. In between the windows, the angle has a formula of y-axis divide by 4.

Coding For Arduino

Controlling hardware with motion control technology needed a microcontroller which controls the hardware practically. Arduino has a simplest programming that’s why we called it a sketch. That’s the easiest way to control 3D motion controller device with real-world hardware.

First, include servo library which is already built in its software. First, check your servo with simple examples to know its working or not. Read: how-to-control-servo motor-with-Arduino Hope your servo motor is running fine. Let’s move further with a servomotor, Leapmotion, and Arduino.

In setup attach your servo motor in Pin # 9 as shown above in Circuit diagram. Communicate serially with serial.begin command with the baud rate of 9600. Baud rate can also be changed if needed.

Read serial if available means read the angle from processing or whatever values coming from processing via hand detection. Saved in variable named angle and write it to the servo motor. Remember that values are within 0 to 720 range. whatever gestures come to leap motion controller the processing tracks and send data to Arduino that moved servo motor at some particular angles.

[mks_button size=”medium” title=”Step: 04″ style=”rounded” url=”http://” target=”_self” bg_color=”#0e938f” txt_color=”#FFFFFF” icon=”” icon_type=”” nofollow=”0″]Final Code of Processing


Full Code Processing:

Final Code Arduino:



Hope You liked this tutorial (How to control a servo motor using leap motion controller and Arduino).
There will be much more stay connected.
If you have any query so we are always here for your help. Stay connected for more Tutorials and Subscribe to our YouTube Channel for video Tutorials.

Exit mobile version