Servo Motor - Getting Started with Windows Remote Arduino (2015)

Getting Started with Windows Remote Arduino (2015)

6. Servo Motor

This chapter explains how to work with servo motor connected to Arduino board using Windows Remote Arduino. We explore how to access PWM (Pulse Width Modulation) Arduino.

6.1 Servo Motor

Servo motor provides a shaft movement 360 degree. We can control this movement based on its degree. In this scenario, you can use any DC motor (servo) that will be connected to Arduino. I used a mini servo from Arduino Sidekick Basic kit.

The following is a picture of my mini servo motor.


To understand servo's cables, you can identify as follows:

· Red cable is be connected to 5V

· Black or brown cable is be connected to GND

· The rest (yellow or orange cable) is be connected to Arduino PWM pin

The next step we are going to build a Windows Universal with Arduino and servo motor.

6.2 Wiring

To build hardware implementation, you can connect servo motor to Arduino by following configuration:

· Red cable is be connected to 5V

· Black or brown cable is be connected to GND

· The rest (yellow or orange cable) is be connected to Arduino PWM pin. I used pin 10 for Arduino Uno R3 or Arduino Mega 2560

The following is a sample of hardware implementation.


6.3 Writing A Program

Firstly, create a new project, called ServoMotor. Follow instructions to create and configure the project as explained on section 2.3.2, 2.3.3 and 2.3.4.

The first step is to develop app UI on MainPage.xaml. We add two TextBlock to display servo motor angle. The following is complete code for MainPage.xaml file.









<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,45,0,0" TextWrapping="Wrap" Text="Servo Motor angle:" VerticalAlignment="Top"/>

<TextBlock x:Name="txtVal" HorizontalAlignment="Left" Margin="151,49,0,0" TextWrapping="Wrap" Text="-" VerticalAlignment="Top"/>




The next step is to modify MainPage.xaml.cs file. Firstly, we add our namespace.

using System.Text;

using Windows.ApplicationModel.Core;

using Microsoft.Maker.Serial;

using Microsoft.Maker.RemoteWiring;

Declare some variables. Servo motor uses PWM Digital 10. We also define a list of angle: 0, 30, 60, 90, 120, 150 and 180.

private UsbSerial connection;

private RemoteDevice arduino;

private const byte SERVO = 10;

private byte[] angles = new byte[] { 0, 30, 60, 90, 120, 150, 180 };

private int direction;

private bool fwd;

private DispatcherTimer timer;

We define InitWRA() method and subscribe ConnectionEstablished event. On this event, we call a timer.

private void InitWRA()


connection = new UsbSerial("VID_2341", "PID_0043");

arduino = new RemoteDevice(connection);

connection.ConnectionEstablished += Connection_ConnectionEstablished;

connection.begin(57600, SerialConfig.SERIAL_8N1);


private void Connection_ConnectionEstablished()



arduino.pinMode(SERVO, PinMode.PWM);

direction = 1;

fwd = true;

timer = new DispatcherTimer();

timer.Interval = TimeSpan.FromMilliseconds(2000);

timer.Tick += Timer_Tick;



On Tick event from Timer object, we read change servo motor angle. Then, we define UpdateData() to update app UI.

private void Timer_Tick(object sender, object e)


System.Diagnostics.Debug.WriteLine("Dir " + direction.ToString() + " Angle: " + Convert.ToString(angles[direction]));


arduino.analogWrite(SERVO, angles[direction]);





if (direction > 6)


fwd = false;

direction = 6;



if (direction < 0)


fwd = true;

direction = 0;



private async void UpdateData(byte value)


await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,

() =>


txtVal.Text = Convert.ToString(value);



Finally, we call InitWRA() on class constructor.

public MainPage()



this.Unloaded += MainPage_Unloaded;



private void MainPage_Unloaded(object sender, RoutedEventArgs e)





Save this code.

6.4 Testing

Compile and run the program. You should see servo motor is moving from angle with incrementing angle 30. A sample output on app UI can be seen on Figure below.


Output program on debug mode also can be seen on Figure below.