Introduction
This article will guide you through the process of robot or embedded system control using a WPF application. There are mainly two types of Robotics but sometimes a hybrid model becomes a necessity. Robotics control can be done using a controller but then again, the cost factor is also relevant. You won't feel like spending hundreds of bucks for your control of various robots. It's always beneficial when you have a software model for this purpose.
The following hardware is required:
- An Arduino developer board (I have used an Arduino Leonardo).
- Light Emitting Diode (LED).
The following software is required:
- Arduino IDE
- Visual Studio
Arduino
An Arduino is a development board built around the Atmel AVR microcontrollers. They are used in embedded systems and robotics applications, this open-source hardware is one of the best developer boards to design your embedded systems.
Let's begin with the Arduino part first. We will be turning on and off an LED and using a WPF app. First, let us see the circuit diagram. We will connect the LED to pin number 13. The LED's longer pin will be in pin number 13 whereas the shorter in the GND pin.

Figure 1: A layout of an Arduino Leonardo developer board. Picture credit: Arduino.

Figure 2: Circuit diagram.
In the preceding figure, the resistance may or may not be used. However, it's safe to use it. In that case, you need 220 ohms to 1K ohm resistance.
The LED is connected to pin 13 and GND. Our next course of action is to program the Arduino. First, connect the Arduino to the USB. For drivers and the IDE, visit Arduino. Now after connecting the Arduino board to your PC, go to Tools, then port to select the port where your Arduino is connected. Also, select your developer board by clicking Tools and selecting Board. The following Figure 3 shows the screenshot of the IDE where you need to select.
After selecting the board and the port, we will go to the programming.

Figure 3: Screenshot of the IDE where you need to select the port and the board.
- void setup()
- {
- pinMode(13, OUTPUT);
- Serial.begin(9600);
- }
- void loop()
- {
- if (Serial.available() > 0)
- {
- char c = Serial.read();
- if (c == '1')
- {
- digitalWrite(13, HIGH);
- } else if (c == '0')
- {
- digitalWrite(13, LOW);
- }
- }
- }
- pinMode(13,OUTPUT);
- if(Serial.available()>0)
- char c = Serial.read();
- digitalWrite(13,HIGH); //for high
- digitalWrite(13,LOW);//for low
- void loop()
- {
- if(Serial.available()>0)
- {
- char c = Serial.read();
- if (c == '1')
- {
- digitalWrite(13,HIGH);
- }
- else if (c == '0')
- {
- digitalWrite(13,LOW);
- }
- }
- }

Figure 4: Screenshot of the Arduino IDE with the final sketch.

Figure 5: Screenshot of the start page of Microsoft Visual Studio.
- A button named "on" with the content on to switch on a LED.
- A button named "off" with the content to switch off a LED.
- A button named connect with the content as connect to connect the Arduino board.
- A button named disconnect with the content as disconnect to disconnect the Arduino board.
- A TextBox with the named com port which will be taken as an input for the com port number.
- A TextBlock with the name status to display the current connection status.
Add the preceding components in the MainWindows.xaml page. Create a click event for each button. Alternatively, you can use the code pasted.
- <Window x:Class="controller_Arduino.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="Controller for Arduino v1" Height="750" Width="1020">
- <Grid>
- <Button x:Name="on" Content="On" HorizontalAlignment="Left" Margin="136,164,0,0" VerticalAlignment="Top" Width="87" Height="52" Click="on_Click"/>
- <Button x:Name="Of" Content="Off" HorizontalAlignment="Left" Margin="246,164,0,0" VerticalAlignment="Top" Width="87" Height="52" Click="Of_Click"/>
- <TextBlock x:Name="status" HorizontalAlignment="Left" Margin="506,253,0,0" TextWrapping="Wrap" Text="Disconnected" VerticalAlignment="Top" Height="29" Width="94"/>
- <TextBox x:Name="comportno" HorizontalAlignment="Left" Height="23" Margin="506,164,0,0" TextWrapping="Wrap" Text="COM13" VerticalAlignment="Top" Width="120"/>
- <Button x:Name="Connect" Content="Connect" HorizontalAlignment="Left" Margin="506,199,0,0" VerticalAlignment="Top" Width="55" Height="24" Click="Connect_Click"/>
- <Button x:Name="Disconnect" Content="Disconnect" HorizontalAlignment="Left" Margin="571,199,0,0" VerticalAlignment="Top" Width="55" Height="24" Click="Disconnect_Click"/>
- </Grid>
- </Window>
- using System.IO.Ports;
- SerialPort sp = new SerialPort();
- private void Connect_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- String portName = comportno.Text;
- sp.PortName = portName;
- sp.BaudRate = 9600;
- sp.Open();
- status.Text = "Connected";
- }
- catch (Exception)
- {
- MessageBox.Show("Please give a valid port number or check your connection");
- }
- }
- private void Disconnect_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- sp.Close();
- status.Text = "Disconnected";
- }
- catch (Exception)
- {
- MessageBox.Show("First Connect and then disconnect");
- }
- }
- private void on_Click(object sender, RoutedEventArgs e)
- {
- sp.Write("1");
- }
- private void Of_Click(object sender, RoutedEventArgs e)
- {
- sp.Write("0");
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.IO.Ports;
- namespace controller_Arduino
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- SerialPort sp = new SerialPort();
- public MainWindow()
- {
- InitializeComponent();
- }
- private void on_Click(object sender, RoutedEventArgs e)
- {
- sp.Write("1");
- }
- private void Of_Click(object sender, RoutedEventArgs e)
- {
- sp.Write("0");
- }
- private void Connect_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- String portName = comportno.Text;
- sp.PortName = portName;
- sp.BaudRate = 9600;
- sp.Open();
- status.Text = "Connected";
- }
- catch (Exception)
- {
- MessageBox.Show("Please give a valid port number or check your connection");
- }
- }
- private void Disconnect_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- sp.Close();
- status.Text = "Disconnected";
- }
- catch (Exception)
- {
- MessageBox.Show("First Connect and then disconnect");
- }
- }
- }
- }

Figure 6: Your first Arduino Controller

Firdaws FirdawsPosted Mar 11, 2019, 3:06 PM
Can you write an article about wpf and the Arduino connection over Bluetooth (preferably HC-06)?
Pawan Kumar TiwariPosted May 8, 2018, 6:03 AM
Hi sir my need is read data from Ethernet port to wpf can you explain about how to read if not using Arduino bord
Bert PeetersPosted Feb 21, 2017, 3:55 PM
Hi, your tutorial is great! Can i ask please how i can contol more pins ?
Jako ChenPosted Nov 21, 2016, 3:37 AM
It's very good share for me
Carmelo La MonicaPosted Jun 22, 2015, 12:48 PM
Very good work, congrats !
Sibeesh VenuPosted Jun 21, 2015, 12:51 PM
Good one. Welcome
Santhakumar MunuswamyPosted Jun 21, 2015, 1:57 AM
Welcome
Santhakumar MunuswamyPosted Jun 21, 2015, 1:57 AM
Thanks for excellent work. I hope you will do rock in future like pooja and abhishek
Dinesh BeniwalPosted Jun 21, 2015, 12:54 AM
Awesome, Gang of IOT's ..Pooja, Carmelo, Abhishek and now Avirup.
Abhishek Kumar RaviPosted Jun 21, 2015, 12:20 AM
Great Work Avirup :)
Pooja BaraskarPosted Jun 20, 2015, 2:50 PM
Impressive, Good article with good explanation. Wanna see more from you :) Welcome !!!
Mahesh ChandPosted Jun 20, 2015, 11:27 AM
Very excited to see young students are working on new tech. Thank you Avirup for sharing with us. Welcome to C# Corner.
Dinesh BeniwalPosted Jun 20, 2015, 10:47 AM
Welcome to C# Corner , great work ;)