Click here to Skip to main content
13,198,322 members (54,627 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 14 Dec 2015

Control Arduino Servo By KinectV2 Using C#

, 14 Dec 2015
Rate this:
Please Sign up or sign in to vote.
Control Arduino ports and servo using C# and Kinect V2


Source code contains 4 projects:

  • Arduino project (includes the way Arduino handles the commands and servo, led control)
  • dllArduinoConnection: This is a library you can use to control Arduino by C#
  • MotorController: This is a DLL. You can use it alone for controlling your KinectV2 by Windows Forms application.
  • WinKinectControl: This program Windows Forms application to see all works together.

and more projects for making sure that everything is working fine:

  • Microsoft demo for kinectV2 as example (from KinectV2 SDK)

This simple programs allow you to:

  • Control your Arduino device using Kinectv2 .NET environment.
  • It is useful for controlling Arduino using C#.
  • Control Arduino using Microsoft Kinect Device V2.


I see many examples for the Arduino and how it works and is programmed. For .NET Environment, I made a DLL to control the Arduino from C# using Serial Port using (USB cable ) comes with and Arduinoono. Also, I applied this in a project using Kinect v2 and Servo as a simple example.

Using the Code

The project is divided into 4 parts:

  1. Arduino Code
  2. Arduino-C# Interface
  3. Servo C# control
  4. Kinect Control Servo

Part 1: Arduino Program

For Arduino part program send messages as parameters over serial port, Arduino device is just waiting for serial data which contains the command. The most important line is this one:

val =;         // read it and store it in 'val'

Then switch parameters read like this code:

//// param 1,2,3,4 is the chars read from serial port.
void SwitchParam1()
	switch (serialData.param1)
	/// case 'm' means that command is to move a motor.
	case 'm':		
	// ConvertToInt is a function which just convert char to Integer.
	serialData.intParam3 = serialData.convertToInt(serialData.param3);
	serialData.intParam4 = serialData.convertToInt(serialData.param4);		
			// case 'l' means we want to on/off a port			
	case 'l':
		Light(serialData.param2);// this is example to on/off leds 1,2,3,...

Part 2: Arduino-C# Interface

This part in the code is made by C# using:


to send data (Parameters) to the Arduino. Here is a simple example:

/// <summary>
/// send data with return the duino feedback then choose close/ let port open 
/// </summary>
/// <param name="data"></param>
/// <param name="close">false = the port will still open 
/// after sending the command to Arduino.</param>
/// <param name="waitForReplayMS">This Parameter for wait 
/// until return Value from Arduino with Millisacands</param>
public string Send(string data,bool close,int waitForReplayMS=2000)
    string strReturnData;
        strReturnData =  Read();
        strReturnData = currentRead;
        if (close)
        return strReturnData;                
    catch (Exception ex)
        throw ex;

Part 3: Servo C# Control

This is the code for control Servo connected to Arduino from C#.

It's similar to leds in C# for Arduino handle. You can see the handle in Arduino code because it converts the degree, Command to be Servo, etc.

/// <summary>
/// Actual Move motor to a degree or more/less and return moving result
/// </summary>
/// <param name="motor">motor name to move </param>
/// <param nameq="action">MoveTo/More/less</param>
/// <param name="degree">degree by 10-20,30,... untill 180 </param>
/// <param name="waitForReplay"> time to wait for return value and for moving </param>
/// <returns> moving return value from serial port </returns>
public string MoveMotor(Motor motor,MotorAction action, int degree, int waitForReplay=2000)

   string Command ="";
   switch (action)
           /// Get Command as string
       case MotorAction.MoveTo:
           Command =  MoveTo(motor, degree);
       case MotorAction.More:
           Command = More(motor, degree);
       case MotorAction.Less:
           Command = Less(motor,degree);
           /// End get
       /// Send Parameters and get the FeedBack
       string duinoReturn = blueTooth.Send(Command ,true, waitForReplay);
       // return the Duino feed Back after doing this Action
       return duinoReturn;
   catch (Exception ex)
       throw ex;

Part 4: Kinect Control Servo

To use all together, here is an example (included in the source code) Controls Servo using the left hand.

///this code get the current position for hand and move servo to left side.
if (hand.GetHandPosition(MotorController.utilities.Side.Left).x >=0)
////this method to move servo and return the result from Arduino 
	MoveResult =  moveMotor(dllArduinoConnection.clsMotor.Motor.leftMotor,

Points of Interest

Now, you can run control your Arduino Servos, Ports from your Windows Application using dllArduinoConnection DLL. You can also use your Kinect Device in a Windows Forms Application using MotorController DLL.


  • You have to upload the Arduino source attached in your Arduino before using the dllArduinoConnection DLL.
  • You can just add a reference to MotorController DLL in your program to use KinectV2 in your Windows Forms Application.


I plan to control Arduino using Bluetooth nested of just serial port using USB, also add more commands to Arduino.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader
Egypt Egypt
Tareq Gamal El-din Mohammed ,
Graduated from Modern Academy for Computer science and Information Technology. Egypt ,
Then flow Microsoft development track Certificates:
MCAD.NET (Microsoft Certified Application Developer)
MCSD.NET (Microsoft Certified Solution Developer)

Also others for Microsoft SharePoint Administration ,Configuration and Development.

Robotics fields was a Hobby since 2002 ,
started to develop some applications for "RoboSapien","RoboSapienV2","RS Media" ,RoboMe and WowWee Rovio. from WowWee company,

Started working with robots as professional way at 2014
By using "NAO" Robot from Aldebaran.

By developing some applications and libraries like :

OpenCV with NAO Robot:

- NAORobot Vision using OPENCV -TotaRobot P1

- NAO Robot Vision using OPENCV - P2

NAO Alarm Clock :

also Robotic Arm Project:

Other Projects Developed by Tareq Gamal El-din Mohammed :

Developed and posted some applications in Code Project web site like :

- Control your Distributed Application using Windows and Web Service

- Quick and dirty directory copy

- Program Execute Timer(From the Web)

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 14 Dec 2015
Article Copyright 2015 by Tareq_Gamal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid