Simple Example Controlling A Servo

<< Back to the SharedPhidgets page

What you will learn in this tutorial:

  • how to create a SharedPhidgets servo object,
  • how to assign a specific serial number,
  • how to use the servo skin to control a servo and
  • how to control the servo from your source code.

Download source:
The zip file contains the program code of this example.


The Phidget Servo
The Phidget Servo
  1. Create a new Windows C# application in VisualStudio.NET
  2. Use the ConnectionManager for connecting to the server
  3. Create a servo object
  4. Create a servo skin and connect it to the servo object
  5. Controlling the servo object directly (with 2 Buttons on the form)
  6. Finished! Compile the application and enjoy controlling of the servos!

Step-by-step in detail

1) Open VisualStudio.NET, click New Project, select a new C# Windows application, enter the name of the project (SharedPhidgetsExample_Servo) and confirm the dialog.

2) Now we click on the SharedPhidgets toolbox on the left side of the VisualStudio window, select the ConnectionManager object and click on your Windows form.

Then change the SharedDictionaryURL property of the Connection Manager to the address of the server:

3) Drag a SharedPhidget Servo component on your form on the FilterSerialNumber property in the Properties explorer of Visual Studio...

...and specify the serial number of the servo. If you have only one servo at your infrastructure, you don't need to add the serial number because the component will then connect to the only component it founds.

4) Create a skin object for the servo:

Change the servo object of the ServoSkin to the object of the created servo, here this is servo1

5) Create two additional buttons on the form, and change their Text property to "180 degrees" and "0 degrees".

Double click on the buttons, and add the following code to the button click handler:
for the 180-degree-button:

private void button1_Click(object sender, System.EventArgs e)
  this.servo1.Motors[0].Position = 180;

for the 0-degree-button:

private void button2_Click(object sender, System.EventArgs e)
  this.servo1.Motors[0].Position = 0;

6) Compile your application:

The following window will appear, where you can control the connected servo. Just try it out to move the slider and to press the button to change the position of the servo:

That's it,

you can now modify this application and enhance the functionality. You can create as many servo objects as you like, but remember to specify the serial number for each of the servos.