Sunteți pe pagina 1din 85

free ebooks ==> www.ebook777.

com

www.ebook777.com
Copyright
free ebooks ==> www.ebook777.com

Getting Started with Windows Remote Arduino


Agus Kurniawan
1st Edition, 2015
Copyright © 2015 Agus Kurniawan
free ebooks ==> www.ebook777.com

Table of Contents
Copyright
Preface
1. Preparing Development Environment
1.1 Arduino
1.1.1 Arduino Uno
1.1.2 Arduino Leonardo
1.1.3 Arduino Mega 2560
1.1.4 Arduino Due
1.2 Electronics Components
1.2.1 Arduino Starter Kit
1.2.2 Fritzing
1.2.3 Cooking-Hacks: Arduino Starter Kit
1.2.4 Arduino Sidekick Basic kit v2
1.2.5 Grove - Starter Kit for Arduino
1.2.6 DFRobot - Arduino Kit for Beginner v3
1.3 Windows 10 and Visual Studio 2015
1.4 Windows Remote Arduino
1.5 Arduino Software
1.6 Testing
2. Windows Remote Arduino for Windows 10
2.1 Setting Up Arduino on Windows 10
2.1.1 Arduino Hardware Driver on Windows 10
2.1.2 Simple Testing
2.2 Arduino and Windows Remote Arduino (WRA)
2.2.1 Configuring Arduino Board
2.2.2 Compiling Windows Remote Arduino
2.3 Windows Remote Arduino with USB
2.3.1 Wiring
2.3.2 Creating A Project

www.ebook777.com
2.3.3 Adding Reference Files for Windows Remote Arduino
free ebooks ==> www.ebook777.com
2.3.4 Configuring Windows Remote Arduino (WRA) on Project
2.3.5 Writing Program
2.3.6 Testing
2.4 Windows Remote Arduino with Bluetooth
3. Digital I/O
3.1 Getting Started
3.2 Wiring
3.3 Creating and Configuring a Project
3.4 Writing a Program
3.5 Testing
4. Analog I/O
4.1 Getting Started
4.2 Demo Analog Output: RGB LED
4.2.1 Arduino Analog output (PWM)
4.2.2 Wiring
4.2.3 Writing Program
4.2.4 Testing
4.3 Demo Analog Input: Working with Potentiometer
4.3.1 Wiring
4.3.2 Writing Program
4.3.3 Testing
5. Working with I2C
5.1 Getting Started
5.2 Writing Program
5.3 Testing
6. Servo Motor
6.1 Servo Motor
6.2 Wiring
6.3 Writing A Program
6.4 Testing
free ebooks ==> www.ebook777.com
Source Code
Contact

www.ebook777.com
Preface
free ebooks ==> www.ebook777.com


This book was written to help anyone want to get started with Arduino and Windows Remote Arduino. It describes
the basic elements of the integration of Arduino and Windows Remote Arduino.


Agus Kurniawan
Depok, June 2015
free ebooks ==> www.ebook777.com
1. Preparing Development Environment

In this chapter, you will learn the following topics:

Introducing Arduino board


Introducing electronics component for testing
Introducing development tools, Visual Studio and Windows 10
Introducing Windows Remote Arduino
Introducing Arduino software

www.ebook777.com
1.1 Arduino
free ebooks ==> www.ebook777.com
Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use
hardware and software. This board uses Atmel microcontroller series. There are many
Arduino hardware models that you can use. Further information about Arduino products,
you can visit on website http://arduino.cc/en/ .
You must one Arduino hardware to follow practices in this book. I recommend to obtain
one of the following Arduino hardware:

Arduino Uno
Arduino Leonardo
Arduino Mega 2560
Arduino Due

You can buy this product on your local electronic store. You also can order it by online.
Find it on http://arduino.cc/en/Main/Buy. The following is the list of Arduino store you
can buy

Arduino store, http://store.arduino.cc/


Amazon, http://www.amazon.com
Cooking-hacks, http://www.cooking-hacks.com/index.php/shop/arduino.html
RS Components, http://www.rs-components.com
Element 14, http://www.element14.com
EXP-Tech, http://www.exp-tech.de

Because Arduino is an open-source hardware, people can build it. It’s called Arduino
compatible. Generally it’s sold in low prices.

1.1.1 Arduino Uno


The Arduino Uno is a microcontroller board based on the ATmega328. You can download
the datasheet file, http://www.atmel.com/dyn/resources/prod_documents/doc8161.pdf .
Further information about Arduino Uno, you can read it on
http://arduino.cc/en/Main/ArduinoBoardUno .
free ebooks ==> www.ebook777.com

1.1.2 Arduino Leonardo


The Arduino Leonardo is a microcontroller board based on the ATmega32u4. Download
datasheet for this product on
http://www.atmel.com/dyn/resources/prod_documents/7766S.pdf .
Visit this product to get the further information on
http://arduino.cc/en/Main/ArduinoBoardLeonardo .

1.1.3 Arduino Mega 2560


The Arduino Mega 2560 is a microcontroller board based on the ATmega2560. You can
download the datasheet file on
http://www.atmel.com/dyn/resources/prod_documents/doc2549.PDF.
Further information about Arduino Mega 2560, you can visit on

www.ebook777.com
http://arduino.cc/en/Main/ArduinoBoardMega2560 .
free ebooks ==> www.ebook777.com

1.1.4 Arduino Due


The Arduino Due is a microcontroller board based on the Atmel SAM3X8E ARM Cortex-
M3 CPU. You can download the datasheet, http://www.atmel.com/Images/doc11057.pdf.
If you want to know about Arduino Due, I recommend to visit this website,
http://arduino.cc/en/Main/ArduinoBoardDue.
free ebooks ==> www.ebook777.com
1.2 Electronics Components
We need electronic components to build our testing, for instance, Resistor, LED, sensor
devices and etc. I recommend you can buy electronic component kit.

1.2.1 Arduino Starter Kit


Store website: http://arduino.cc/en/Main/ArduinoStarterKit

1.2.2 Fritzing
Store website: http://shop.fritzing.org/ .
You can buy Fritzing Starter Kit with Arduino UNO or Fritzing Starter Kit with Arduino
Mega.

www.ebook777.com
free ebooks ==> www.ebook777.com

1.2.3 Cooking-Hacks: Arduino Starter Kit


Store website: http://www.cooking-hacks.com/index.php/shop/arduino/starter-
kits/arduino-starter-kit.html
free ebooks ==> www.ebook777.com

1.2.4 Arduino Sidekick Basic kit v2


Store website: http://www.seeedstudio.com/depot/Sidekick-Basic-Kit-for-Arduino-V2-p-
1858.html
You also can find this kit on this online store.
http://www.exp-tech.de/seeed-studio-sidekick-basic-kit-for-arduino-v2

www.ebook777.com
free ebooks ==> www.ebook777.com
1.2.5 Grove - Starter Kit for Arduino
Another option, you can buy this kit on
Seeedstudio, http://www.seeedstudio.com/depot/Grove-Starter-Kit-for-Arduino-p-
1855.html .

1.2.6 DFRobot - Arduino Kit for Beginner v3


DFRobot provides Arduino kit too. You can buy it on the following website.
http://www.dfrobot.com/index.php?route=product/product&path=35_49&product_id=345
free ebooks ==> www.ebook777.com

www.ebook777.com
1.3 Windows 10 and Visual Studio 2015
free ebooks ==> www.ebook777.com
To develop app with Windows Remote Arduino, we use Visual Studio 2015 and Windows
10. Please install them for our development environment.
free ebooks ==> www.ebook777.com
1.4 Windows Remote Arduino
Windows Remote Arduino is an open-source Windows Runtime Component library which
allows Makers to control an Arduino through a Bluetooth or USB connection! It is
intended for Windows Runtime (WinRT) Developers who want to harness the power of
Arduino hardware using the Windows Runtime Languages. This library can be download
on https://github.com/ms-iot/remote-wiring/.
We will deploy and test program based Windows Remote Arduino on the next chapter.

www.ebook777.com
1.5 Arduino Software
free ebooks ==> www.ebook777.com
To develop application based on Arduino board, we need Arduino software. You can
obtain it on http://arduino.cc/en/Main/Software . Download Arduino IDE for Windows
platform.
Execute that setup file. Select all items on installation.

At the middle of installation process, you get security confirmation. Please click Install
button to install Arduino driver.

After completed installation, you can run Arduino at the first. You may get security
confirmation. Please give a permission by clicking Allow access button.
free ebooks ==> www.ebook777.com

Finally, you should see Arduino IDE.

www.ebook777.com
free ebooks ==> www.ebook777.com
free ebooks ==> www.ebook777.com
1.6 Testing
For testing, I used Arduino Uno R3 on Windows 10.

I also used Arduino Sidekick Basic kit for electronic components.

www.ebook777.com
2. Windows Remote Arduino for Windows 10
free ebooks ==> www.ebook777.com

This chapter explains how to work with Arduino and Windows 10 for getting started.
free ebooks ==> www.ebook777.com
2.1 Setting Up Arduino on Windows 10
After you installed Arduino software, you can plugin Arduino board into computer via
USB.

Then you run Arduino software. In general it will detect Arduino hardware include
Arduino type and model.

2.1.1 Arduino Hardware Driver on Windows 10


If you open Device Manager, you should see Arduino detected on Ports (COM & LPT).

www.ebook777.com
free ebooks ==> www.ebook777.com

However, if you get a problem about Arduino hardware driver, you update that driver.

You can update this device driver by navigating hardware driver on the driver folder of
Arduino software installation folder.
free ebooks ==> www.ebook777.com
2.1.2 Simple Testing
Now you’re ready to get started. For illustration, I use the sample code from Arduino,
Blink. You can visit on http://arduino.cc/en/Tutorial/Blink for configuration.
For this scenario, we use attached LED which is connected on Digital 13. Open Arduino
IDE.

On Arduino software, Click File -> Examples -> 01.Basics -> Blink.

www.ebook777.com
free ebooks ==> www.ebook777.com

Then you will get a sample code of Blink app.


free ebooks ==> www.ebook777.com

Now try to connect your Arduino into computer. Select Arduino Uno for board target. You
can select it by clicking menu Tools -> Board : Arduino Uno.

www.ebook777.com
free ebooks ==> www.ebook777.com
Furthermore, you must select Arduino port. Select menu Tools -> Port -> (your Arduino
port which already connected).

Now you can compile and upload this program into Arduino board.

If success, you should see blinking LED on Arduino board.


free ebooks ==> www.ebook777.com

www.ebook777.com
2.2 Arduino and Windows Remote Arduino (WRA)
free ebooks ==> www.ebook777.com
To communicate between Arduino and computer/tablet/embedded board based Windows
10, we can use Windows Remote Arduino. There are two types of connectivity for
Windows Remote Arduino:

Serial port (USB)


Bluetooth

I will explain them on the next section. Now we must configure Arduino board and
computer to establish a connection.

2.2.1 Configuring Arduino Board


To work with Windows Remote Arduino, we must configure Arduino board to be
deployed Firmata library. Firmata is a protocol for communicating with microcontrollers
from software on a computer (or smartphone/tablet, etc). The protocol can be
implemented in firmware on any microcontroller architecture as well as software on any
computer software package. Further information about Firmata, you can read it
on https://github.com/firmata/protocol .
To deploy Firmata into Arduino board, you do it opening Arduino IDE. Select menu File -
> Examples -> Firmata -> StandardFirmata.
free ebooks ==> www.ebook777.com

Then, you will get Firmata code on Arduino IDE.

www.ebook777.com
free ebooks ==> www.ebook777.com
By default, Firmata on Arduino uses a baud rate 57600. You can change it according to
your serial port (USB) or Bluetooth, for instance, 115200. After that, please compile and
upload this program into Arduino board.

Finally, Arduino board is ready for running with Firmata protocol.

2.2.2 Compiling Windows Remote Arduino


The next step is to compile Windows Remote Arduino library. You can download it
on https://github.com/ms-iot/remote-wiring/. It provides library for Windows 10 and
Windows 8.1. Open for Solution file (*.sln) for Windows 10 using Visual Studio 2015.
free ebooks ==> www.ebook777.com

Compile these libraries for ARM, x86, and x64 targets.


If finished, you should see the compiled files. Compiled ARM, x64, and x86 targets will
generate ARM, x64 and Debug folders.

www.ebook777.com
2.3 Windows Remote Arduino with USB
free ebooks ==> www.ebook777.com
In this section, we build a blinking LED program via USB using Windows Universal app.
The USB usage is applied as a bridge to remote Arduino board.
Our scenario needs the following items:

Three LEDs
USB cable
Jumper cables

To build the program, you can do the following steps:

Wiring
Creating a project
Adding reference files for Windows Remote Arduino
Configuring Windows Remote Arduino on project
Writing program
Testing

Let’s start!

2.3.1 Wiring
Connect three LEDs on Digital 12, 11, 10. Other LED pins are be connected to GND.
Now you connect Arduino board to Computer via USB cable.
free ebooks ==> www.ebook777.com

2.3.2 Creating A Project


Open Visual Studio 2015. Create a new project with template Blank App (Windows
Universal). It’s located on Visual C# -> Windows -> Windows Universal.
Fill the project name, called BlinkingLed.

www.ebook777.com
free
If done, click OK button. ebooks ==> www.ebook777.com

2.3.3 Adding Reference Files for Windows Remote Arduino


The next step is to add Windows Remote Arduino into our project. Firstly, add Microsoft
Visual C++ AppLocal Runtime from Windows Universal -> Extensions.

Then, we add our compiled files from Windows Remote Arduino (read section 2.2.2).
Add the following files (depend on your target: ARM, x64, x86):

Microsoft.Maker.RemoteWiring.winmd
Microsoft.Maker.Firmata.winmd
Microsoft.Maker.Serial.winmd
free ebooks ==> www.ebook777.com

Click OK button if finished.


Finally, you should see our referenced files on our project.

www.ebook777.com
free ebooks ==> www.ebook777.com

2.3.4 Configuring Windows Remote Arduino (WRA) on


Project
After added WRA Reference files, we must configure project config file and activate USB
capability on app.
To configure project config file (*.csproj) , you just unload the project (not solution). Or
you edit project file (*.csproj) directly.
Add this script into <Reference Include “Microsoft.Maker.Firmata”></Reference> tag
<IsWinMDFile>true</IsWinMDFile>
<Implementation>Microsoft.Maker.Firmata.dll</Implementation>

Add this script into<Reference Include=“Microsoft.Maker.RemoteWiring”></Reference>


tag
<IsWinMDFile>true</IsWinMDFile>
free ebooks ==> www.ebook777.com
<Implementation>Microsoft.Maker.RemoteWiring.dll</Implementation>

Add this script into<Reference Include=“Microsoft.Maker.Serial”></Reference> tag


<IsWinMDFile>true</IsWinMDFile>
<Implementation>Microsoft.Maker.Serial.dll</Implementation>

You also need to configure Package.appxmanifest file to enable Serial port capability.
Open Package.appxmanifest file and add the following script on <Capabilities>
</Capabilities> tag.
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>

www.ebook777.com
free ebooks ==> www.ebook777.com

2.3.5 Writing Program


Now we develop UI on XAML. We modify MainPage.xaml file and build the following
UI.
free ebooks ==> www.ebook777.com

We need three CheckBox, a TextBlock and a Button. The following is a script of


MainPage.xam.
<Page
x:Class="BlinkingLed.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BlinkingLed"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"


<CheckBox x:Name="chkLed1" Content="LED 1" Checked="HandleCheck"
<CheckBox x:Name="chkLed2" Content="LED 2" Checked="HandleCheck"
<CheckBox x:Name="chkLed3" Content="LED 3" Checked="HandleCheck"
<TextBlock x:Name="txtStatus" HorizontalAlignment="Left" Margin=
<Button x:Name="btnClose" Content="Turn Off All LEDs" HorizontalAlignm

</Grid>
</Page>

www.ebook777.com
free ebooks ==> www.ebook777.com

On MainPage.xaml.cs, we modify this code. Firstly, we add our namespace.


using Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;

Then, we declare some variables.


private UsbSerial connection;
private RemoteDevice arduino;
private const byte LED1 = 12;
private const byte LED2 = 11;
private const byte LED3 = 10;

LED 1, 2, 3 are connected to Digital 12, 11, and 10 on Arduino.


We define InitWRA() method and subscribe ConnectionEstablished event. We set pin
mode using pinMode() method.
private void InitWRA()
{
connection = new UsbSerial("VID_2341", "PID_0043");
arduino = new RemoteDevice(connection);

connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(57600, SerialConfig.SERIAL_8N1);

private void Connection_ConnectionEstablished()


{
arduino.pinMode(LED1, PinMode.OUTPUT);
arduino.pinMode(LED2, PinMode.OUTPUT);
arduino.pinMode(LED3, PinMode.OUTPUT);

txtStatus.Text = "Connected";
free ebooks ==> www.ebook777.com
}

You can see parameters on UsbSerial(). These values can be obtained from SerialPort
(USB) property which Arduino board is connected. For instance, My Arduino is
connected to COM3. You can open COM property from Device Manager.

We handle CheckBox checked or unchecked in HandleCheck() and HandleUnchecked()


methods. If it checked, we turn on LED. Otherwise, we turn off LED.
private void HandleCheck(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if(cb.Name== "chkLed1")
{
arduino.digitalWrite(LED1, PinState.HIGH);
}
if (cb.Name == "chkLed2")
{
arduino.digitalWrite(LED2, PinState.HIGH);
}
if (cb.Name == "chkLed3")
{
arduino.digitalWrite(LED3, PinState.HIGH);
}

}

www.ebook777.com
private void HandleUnchecked(object sender, RoutedEventArgs e)
{
free ebooks ==> www.ebook777.com
CheckBox cb = sender as CheckBox;
if (cb.Name == "chkLed1")
{
arduino.digitalWrite(LED1, PinState.LOW);
}
if (cb.Name == "chkLed2")
{
arduino.digitalWrite(LED2, PinState.LOW);
}
if (cb.Name == "chkLed3")
{
arduino.digitalWrite(LED3, PinState.LOW);
}
}

We also declare TurnOffLeds() to turn off LEDs. This is connected to button clicked
event.
private void TurnOffLeds(object sender, RoutedEventArgs e)
{
chkLed1.IsChecked = false;
chkLed2.IsChecked = false;
chkLed3.IsChecked = false;
}

Finally, on class constructor, we call InitWRA() method.


public MainPage()
{
this.InitializeComponent();

this.Unloaded += MainPage_Unloaded;

InitWRA();
}

private void MainPage_Unloaded(object sender, RoutedEventArgs e)


{
arduino.Dispose();
}

2.3.6 Testing
Run this program. If success, you can see our program UI. Try to do checked on
CheckBoxs. Then, you should see lighting LED.
free ebooks ==> www.ebook777.com

www.ebook777.com
2.4 Windows Remote Arduino with Bluetooth
free ebooks ==> www.ebook777.com
WRA also support using Bluetooth to remote Arduino. You can use Bluetooth module
from SparkFun Bluetooth Mate Silver, https://www.sparkfun.com/products/12576 .
Don’t forget to modify Firmata code on Arduino about baud rate.

Just connect your Bluetooth module on Tx and Rx from Arduino. Then, pair Bluetooth
module on Arduino to Bluetooth PC. Add Bluetooth capability on Package.appxmanifest
file.
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>

On our program, we change UsbSerial to BluetoothSerial object.


private UsbSerial connection;
private RemoteDevice arduino;
free ebooks ==> www.ebook777.com
...

private BluetoothSerial connection;


private RemoteDevice arduino;

On instantiate stage, you just construct connection object with BluetoothSerial() with
passing Bluetooth name.
connection = new BluetoothSerial("MyBluetooth");
connection.ConnectionFailed += Connection_ConnectionFailed;
arduino = new RemoteDevice(connection);

connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(115200, 0);

Just it!
Now you can program as usual with RemoteDevice object.

www.ebook777.com
3. Digital I/O
free ebooks ==> www.ebook777.com

In this chapter I’m going to explain how to remote Arduino Digital I/O using WRA.
free ebooks ==> www.ebook777.com
3.1 Getting Started
In this chapter, we learn how to access Arduino digital I/O via Windows Remote Arduino.
Our scenario is to use a pushbutton and LED. If pushbutton is pressed, LED will lighting.
You can find a pushbutton on the following online store:

Sparkfun, https://www.sparkfun.com/products/97
Adafruit, http://www.adafruit.com/products/1400
eBay, http://www.ebay.com

In this scenario, I use USB as bridge to Arduino. You can replace it by Bluetooth.
Let’s start!.

www.ebook777.com
3.2 Wiring
free ebooks ==> www.ebook777.com
Connect LED to Digital 12 on Arduino and pushbutton to Digital 11. The following is a
sample of wiring.
free ebooks ==> www.ebook777.com
3.3 Creating and Configuring a Project
Create a new project, called DigitalIoDemo. Follow instructions to create and configure
the project as explained on section 2.3.2, 2.3.3 and 2.3.4.

www.ebook777.com
3.4 Writing a Program
free ebooks ==> www.ebook777.com
We modify MainPage.xaml.cs. Add the following namespace.
using Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;

Define some variables for LED and pushbutton.


private UsbSerial connection;
private RemoteDevice arduino;
private const byte LED = 12;
private const byte PUSHBUTTON = 11;

We define InitWRA() to initialize WRA. Then, we subscribe DigitalPunUpdateEvent


event. We update a state on LED from pushbutton state.
private void InitWRA()
{
connection = new UsbSerial("VID_2341", "PID_0043");
arduino = new RemoteDevice(connection);

connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(57600, SerialConfig.SERIAL_8N1);

private void Connection_ConnectionEstablished()


{
System.Diagnostics.Debug.WriteLine("Connected");

arduino.pinMode(LED, PinMode.OUTPUT);
arduino.pinMode(PUSHBUTTON, PinMode.INPUT);

arduino.DigitalPinUpdatedEvent += Arduino_DigitalPinUpdatedEvent;

private void Arduino_DigitalPinUpdatedEvent(byte pin, PinState state)


{
if(pin==PUSHBUTTON)
{
arduino.digitalWrite(LED, state);
}
}

Finally, we call InitWRA() method on class constructor.


public MainPage()
free ebooks ==> www.ebook777.com
{
this.InitializeComponent();
this.Unloaded += MainPage_Unloaded;

InitWRA();
}

private void MainPage_Unloaded(object sender, RoutedEventArgs e)


{
arduino.Dispose();
}

Save this code.

www.ebook777.com
3.5 Testing
free ebooks ==> www.ebook777.com
Now you can compile and run this program. For testing, try to press pushbutton. You
should see a lighting LED.
free ebooks ==> www.ebook777.com
4. Analog I/O

This chapter explains how to work with Arduino Analog I/O via WRA.

www.ebook777.com
4.1 Getting Started
free ebooks ==> www.ebook777.com
Arduino board provides Analog I/O which can be connected to sensor or actuator devices.
In this chapter, we try to access Arduino Analog I/O using Windows Remote Arduino.
There are two scenarios for our cases:

Controlling RGB LED


Reading Analog input using Potentiometer

All scenarios use USB for connectivity between Arduino and PC.
Let’s start.
free ebooks ==> www.ebook777.com
4.2 Demo Analog Output: RGB LED
In this scenario we build a Windows Universal application to control RGB LED color
using Arduino Analog output (PWM). RGB LED has 4 pins that you can see it on Figure
below.

To understand these pins, you can see the following Figure.

Note:

Pin 1: Red

www.ebook777.com
Pin 2: Common pin
Pin 3: Green free ebooks ==> www.ebook777.com
Pin 4: Blue

Now we can start to build a Windows Universal application and hardware


implementation.

4.2.1 Arduino Analog output (PWM)


Please be careful if you want to work with Arduino PWM. If you have Arduino Mega, you
will see PWM label so you obtain PWM pins easily but if you have Arduino Uno, it writes
DIGITAL (PWM ~). It means your PWM pins can be found on DIGITAL pins which pin
with ~, for instance, ~3,~5,~6,~9, ~10, ~11.
For Arduino Mega 2560, you can see PWM pins on picture below (see red arrow).

For Arduino Uno R3, you can see PWM pins as below.
free ebooks ==> www.ebook777.com

4.2.2 Wiring
For our testing, we configure the following PWM pins.
Arduino Mega 2560:

RGB LED pin 1 (red) is connected to Arduino PWM pin 4


RGB LED pin 2 is connected to Arduino VCC 5V
RGB LED pin 3 (green) is connected to Arduino PWM pin 3
RGB LED pin 4 (blue) is connected to Arduino PWM pin 2

Arduino Uno R3:

RGB LED pin 1 (red) is connected to Arduino PWM pin 9


RGB LED pin 2 is connected to Arduino VCC 5V
RGB LED pin 3 (green) is connected to Arduino PWM pin 10
RGB LED pin 4 (blue) is connected to Arduino PWM pin 11

Here is a sample implementation with Arduino Uno R3.

www.ebook777.com
free ebooks ==> www.ebook777.com

4.2.3 Writing Program


Firstly, create a new project, called RGBDemo. Follow instructions to create and
configure the project as explained on section 2.3.2, 2.3.3 and 2.3.4.
Now we modify MainPage.xaml.cs file. Add these namespace.
using Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;

Declare some variables.


private UsbSerial connection;
private RemoteDevice arduino;
private const byte RED = 9;
private const byte GREEN = 10;
private const byte BLUE = 11;
private DispatcherTimer timer;
private int index = 1;

We define InitMRA(), SetColor() and ColorDemo() methods.


free ebooks ==> www.ebook777.com
private void InitWRA()
{
connection = new UsbSerial("VID_2341", "PID_0043");
arduino = new RemoteDevice(connection);

connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(57600, SerialConfig.SERIAL_8N1);

}
private void SetColor(ushort red, ushort green, ushort blue)
{
arduino.analogWrite(RED, red);
arduino.analogWrite(GREEN, green);
arduino.analogWrite(BLUE, blue);
}

private void ColorDemo(int index)


{
if(index==1)
SetColor(255, 0, 0); // red
if (index == 2)
SetColor(0, 255, 0); // green
if (index == 3)
SetColor(0, 0, 255); // blue
if (index == 4)
SetColor(255, 255, 0); // yellow
if (index == 5)
SetColor(80, 0, 80); // purple
if (index == 6)
SetColor(0, 255, 255); // aqua

A timer is used to running our RGB LED which consists of 6 color demo by calling
ColorDemo().
private void Connection_ConnectionEstablished()
{
System.Diagnostics.Debug.WriteLine("Connected");

arduino.pinMode(RED, PinMode.PWM);
arduino.pinMode(GREEN, PinMode.PWM);
arduino.pinMode(BLUE, PinMode.PWM);

index = 1;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(500);
timer.Tick += Timer_Tick;
timer.Start();

www.ebook777.com
private void Timer_Tick(object sender, object e)
{
free ebooks ==> www.ebook777.com
ColorDemo(index);
index++;
if (index > 6)
index = 1;

We call InitWRA() method on class constructor


public MainPage()
{
this.InitializeComponent();
this.Unloaded += MainPage_Unloaded;

InitWRA();
}
private void MainPage_Unloaded(object sender, RoutedEventArgs e)
{
arduino.Dispose();
}

4.2.4 Testing
Compile and run the program. You should see several color on RGB LED.
The following is a sample demo on RGB LED.
free ebooks ==> www.ebook777.com

www.ebook777.com
4.3 Demo Analog Input: Working with Potentiometer
free ebooks ==> www.ebook777.com
In this section, we learn how to read analog input on Arduino board. For illustration, I use
Potentiometer as analog input source. Our scenario is to read analog value from
Potentiometer. Then, display it on app UI.
Let’s start!.

4.3.1 Wiring
To understand Potentiometer, you see its scheme in Figure below.

You can connect VCC to Arduino board VCC 5V. Vout to Arduino board Analog input
A0. In addition, GND to Arduino board GND. The following is hardware implementation.
I use slide potentiometer.
free ebooks ==> www.ebook777.com

4.3.2 Writing Program


Firstly, create a new project, called RGBDemo. 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. One of
TextBlock will be used to display Analog value. The following is complete code for
MainPage.xaml file.
<Page
x:Class="PotDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PotDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"


<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin=
<TextBlock x:Name="txtVal" HorizontalAlignment="Left" Margin="80,28,0,

</Grid>
</Page>

Now we modify MainPage.xaml.cs file. Add these namespace.

www.ebook777.com
using Windows.ApplicationModel.Core;
using Microsoft.Maker.Serial;
free ebooks ==> www.ebook777.com
using Microsoft.Maker.RemoteWiring;

We declare some variables for our app.


private UsbSerial connection;
private RemoteDevice arduino;
private const byte NUM_DIGITAL_PINS = 14; // Arduino Uno
private const byte POT = 0;

We define InitWRA() method and subscribe ConnectionEstablished event. On this event,


we subscribe AnalogPinUpdateEvent event. On AnalogPinUpdateEvent event, we update
TextBlock UI. Because we update UI from other thread, we use Dispatcher.RunAsync().
We define UpdateDate().
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()


{
System.Diagnostics.Debug.WriteLine("Connected");
arduino.pinMode(NUM_DIGITAL_PINS + POT, PinMode.ANALOG);

var val = arduino.analogRead(NUM_DIGITAL_PINS + POT);


txtVal.Text = Convert.ToString(val);
arduino.AnalogPinUpdatedEvent += Arduino_AnalogPinUpdatedEvent;

}
private async void UpdateData(ushort value)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.
() =>
{
txtVal.Text = Convert.ToString(value);
});
}

private void Arduino_AnalogPinUpdatedEvent(byte pin, ushort value)


{
if (pin == POT)
{
UpdateData(value);
free ebooks ==> www.ebook777.com
}
}

Finally, we call InitWRA() on class constructor.


public MainPage()
{
this.InitializeComponent();
this.Unloaded += MainPage_Unloaded;
InitWRA();
}

private void MainPage_Unloaded(object sender, RoutedEventArgs e)


{
arduino.Dispose();
}

Save this code.

4.3.3 Testing
Compile and run this program. If success, you can see analog value on UI.

www.ebook777.com
5. Working with I2C
free ebooks ==> www.ebook777.com

In this chapter we learn how to work with I2C using Windows Remote Arduino.
free ebooks ==> www.ebook777.com
5.1 Getting Started
The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early ’80s to allow
easy communication between components which reside on the same circuit board. TWI
stands for Two Wire Interface and for most marts this bus is identical to I²C. The name
TWI was introduced by Atmel and other companies to avoid conflicts with trademark
issues related to I²C.
I2C bus consists of two wires, SDA (Serial Data Line) and SCL (Serial Clock Line).
Arduino Uno has I2C/TWI on Analog A4 (SDA) and Analog A5 (SCL).
For testing, I used PCF8591 AD/DA Converter module with sensor and actuator devices.
You can find it on the following online store:

Amazon, http://www.amazon.com/PCF8591-Converter-Module-Digital-
Conversion/dp/B00BXX4UWC/
eBay, http://www.ebay.com
Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-to-
analog-converter-module-w-dupont-cable-deep-blue-336384
Aliexpress, http://www.aliexpress.com/

In addition, you can find this device on your local electronics store/online store.

www.ebook777.com
This module has mini form model too, for instance, you can find it on Amazon,
free ebooks ==> www.ebook777.com
http://www.amazon.com/WaveShare-PCF8591T-Converter-Evaluation-
Development/dp/B00KM6X2OI/ .

This module use PCF8591 IC and you can read the datasheet on the following URLs.

http://www.electrodragon.com/w/images/e/ed/PCF8591.pdf
http://www.nxp.com/documents/data_sheet/PCF8591.pdf

The following is our wiring lab:

PCF8591 AD/DA SDA —> Arduino Analog A4 (SDA)


PCF8591 AD/DA SCL —> Arduino Analog A5 (SCL)
PCF8591 AD/DA GND —> Arduino GND
PCF8591 AD/DA VCC —> Arduino VCC +3.3V

Hardware implementation can be shown in Figure below.


free ebooks ==> www.ebook777.com

www.ebook777.com
5.2 Writing Program
free ebooks ==> www.ebook777.com
Firstly, create a new project, called I2CDemo. 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 six TextBlock to display
Analog values. The following is complete code for MainPage.xaml file.
<Page
x:Class="I2CDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:I2CDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"


<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin=
<TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin=
<TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Margin=
<TextBlock x:Name="txtThermistor" HorizontalAlignment="Left" Margin=
<TextBlock x:Name="txtPhoto" HorizontalAlignment="Left" Margin="163,77
<TextBlock x:Name="txtPot" HorizontalAlignment="Left" Margin="163,102,

</Grid>
</Page>

The next step is to modify MainPage.xaml.cs file. Firstly, we add our namespace.
free ebooks ==> www.ebook777.com
using System.Text;
using Windows.ApplicationModel.Core;
using Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;

Declare some variables. PCF8591 uses I2C address on 0x90. To get Thermistor value, we
send a command 0x40. Photo-voltaic value can be acquired by sending a command 0x41.
The last, Potentiometer, can be acquired using 0x43 command.
private UsbSerial connection;
private RemoteDevice arduino;
private const byte NUM_DIGITAL_PINS = 14; // Arduino Uno
private const byte SDA = 4;
private const byte SCL = 5;
private const byte PCF8591 = (0x90 >> 1); // Device address
private const byte PCF8591_ADC_CH0 = 0x40; // thermistor
private const byte PCF8591_ADC_CH1 = 0x41; // photo-voltaic cell
private const byte PCF8591_ADC_CH2 = 0x42;
private const byte PCF8591_ADC_CH3 = 0x43; // potentiometer
private int index = 0;
private Queue<int> i2cReading = new Queue<int>();

private DispatcherTimer timer;

We define InitWRA() method and subscribe ConnectionEstablished event. On this event,


we subscribe I2cReplyEvent event. On I2cReplyEvent event, we update TextBlock UI.
Because we update UI from other thread, we use Dispatcher.RunAsync(). We define
UpdateDate().
private void InitWRA()
{
connection = new UsbSerial("VID_2341", "PID_0043");
arduino = new RemoteDevice(connection);

connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(57600, SerialConfig.SERIAL_8N1);

private void Connection_ConnectionEstablished()


{
System.Diagnostics.Debug.WriteLine("Connected");
arduino.pinMode(NUM_DIGITAL_PINS + SDA, PinMode.I2C);
arduino.pinMode(NUM_DIGITAL_PINS + SCL, PinMode.I2C);

index = 0;
arduino.I2c.I2cReplyEvent += I2c_I2cReplyEvent;

timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(2000);

www.ebook777.com
timer.Tick += Timer_Tick;
timer.Start();
free ebooks ==> www.ebook777.com
}

private void I2c_I2cReplyEvent(byte address_, string response)


{
byte[] data = Encoding.UTF8.GetBytes(response);
int curr = i2cReading.Dequeue();
UpdateData(curr, data[1]);
System.Diagnostics.Debug.WriteLine("" + Convert.ToString(address_)
}

On Tick event from Timer object, we read analog values from PCF8591. Then, we define
UpdateData() to update app UI.
private void Timer_Tick(object sender, object e)
{
switch (index)
{
case 0:
i2cReading.Enqueue(index);
System.Diagnostics.Debug.WriteLine("PCF8591_ADC_CH0"
ReadADC(PCF8591_ADC_CH0);
break;
case 1:
i2cReading.Enqueue(index);
System.Diagnostics.Debug.WriteLine("PCF8591_ADC_CH1"
ReadADC(PCF8591_ADC_CH1);
break;
case 2:
i2cReading.Enqueue(index);
System.Diagnostics.Debug.WriteLine("PCF8591_ADC_CH2"
ReadADC(PCF8591_ADC_CH2);
break;

}
index++;
if (index > 2)
index = 0;

void ReadADC(byte config)


{
arduino.I2c.enable();
arduino.I2c.write(PCF8591, "" + (char)config);
arduino.I2c.read(PCF8591,2);
arduino.I2c.stop(PCF8591);
}
free ebooks ==> www.ebook777.com
private async void UpdateData(int index, byte value)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Wind
() =>
{
switch(index)
{
case 0:
txtThermistor.Text = Convert.ToString(value
break;
case 1:
txtPhoto.Text = Convert.ToString(value);
break;
case 2:
txtPot.Text = Convert.ToString(value);
break;
}
});
}

Finally, we call InitWRA() on class constructor.


public MainPage()
{
this.InitializeComponent();
this.Unloaded += MainPage_Unloaded;

InitWRA();
}

private void MainPage_Unloaded(object sender, RoutedEventArgs e)


{
connection.end();
arduino.Dispose();
}

Save this code.

www.ebook777.com
5.3 Testing
free ebooks ==> www.ebook777.com
Now you can compile and run the program.
If success, you can see sensor values via I2C on app UI. The following is a sample output.
free ebooks ==> www.ebook777.com
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.

www.ebook777.com
6.1 Servo Motor
free ebooks ==> www.ebook777.com
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.
free ebooks ==> www.ebook777.com
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.

www.ebook777.com
6.3 Writing A Program
free ebooks ==> www.ebook777.com
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.
<Page
x:Class="ServoMotor.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ServoMotor"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"


<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin=
<TextBlock x:Name="txtVal" HorizontalAlignment="Left" Margin="151,49,0

</Grid>
</Page>

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;
free ebooks ==> www.ebook777.com

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_ConnectionEstablish
connection.begin(57600, SerialConfig.SERIAL_8N1);

private void Connection_ConnectionEstablished()


{
System.Diagnostics.Debug.WriteLine("Connected");
arduino.pinMode(SERVO, PinMode.PWM);

direction = 1;
fwd = true;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(2000);
timer.Tick += Timer_Tick;
timer.Start();

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() +
UpdateData(angles[direction]);
arduino.analogWrite(SERVO, angles[direction]);

if(fwd)

www.ebook777.com
direction++;
else
free ebooks ==>
direction--;
www.ebook777.com

if (direction > 6)
{
fwd = false;
direction = 6;
}
else
if (direction < 0)
{
fwd = true;
direction = 0;
}

}

private async void UpdateData(byte value)


{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Wind
() =>
{
txtVal.Text = Convert.ToString(value);
});
}

Finally, we call InitWRA() on class constructor.


public MainPage()
{
this.InitializeComponent();
this.Unloaded += MainPage_Unloaded;

InitWRA();
}
private void MainPage_Unloaded(object sender, RoutedEventArgs e)
{
connection.end();
arduino.Dispose();
}

Save this code.


free ebooks ==> www.ebook777.com
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.

www.ebook777.com
Source Code
free ebooks ==> www.ebook777.com

You can download source code on


http://www.aguskurniawan.net/book/wra_20062015_01.zip .
free ebooks ==> www.ebook777.com
Contact

If you have question related to this book, please contact me at aguskur@hotmail.com . My


blog: http://blog.aguskurniawan.net

www.ebook777.com

S-ar putea să vă placă și