Sunday, 8 June 2014

Serial port communication with Arduino

Serial port communication with Arduino

Type:                Hardware + Software
Difficulty:           Easy
Developed by:    Avirup Basu

Summary:

In this article, I will perform a very basic operation i.e Switch on and off a LED by a c# application. The hardware is an Arduino. As an output unit, I will use a Light emitting diode(LED), as an input source, I will be using a c# application which will communicate with the arduino serially. This micro project is a very basic level for upcoming projects.

Software:

1: Microsoft Visual Studio(I have used VS2013 ultimate)
2: Arduino IDE for Windows

Hardware:

1: Arduino development board(I have used Arduino leonardo)
2: A LED(Light Emitting Diode)

Method:

First I will be discussing about the c# code. The c# code will communicate with the arduino serially. The main purpose of the c# code is to create an interface between the Arduino and the pc. The c# code will serially send information to the arduino and accordingly the arduino will act. It's just like if 'a' is a code for the arduino to switch on the LED, then c# will send 'a' serially and the LED will be on. Yes it's simple. But as we will start with more complex projects, then perhaps it won't be easy.

Step1:  Create a new Windows form project. For this, first open Microsoft Visual Studio and then select new project. After that click the tab "Windows Desktop" and select WPF application. Name the project whatever you desire off.





Step2:  Now you need to design an user interface.  For this, we will be using xaml code. You can simply drag and drop the controls as well. For this project you need 2 buttons only. Drag the buttons to the layout and drop it.






Step4: The previous step was for designing the user interface. Now for the two buttons, we need to create event handler. The event handler of the buttons will handle the events and act accordingly. Another thing that I forgot to mention earlier is that you must assign names to the 2 buttons you just placed. For instance let us consider it start and stop. Now let us get into the backend code in c#. Browse to MainWindow.xaml.cs and paste the following code.


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 ArLed
{
    public partial class MainWindow : Window
    {
        int blink = 0;
        SerialPort sp1 = new SerialPort();
        string portName = "COM13";
        int baudeRate = 9600;
        public MainWindow()
        {
            InitializeComponent();
            //portName = "COM13";
            //baudeRate = 9600;
            sp1.PortName = "COM13";
            sp1.BaudRate = 9600;
            sp1.Open();
        }

        private void start_Click(object sender, RoutedEventArgs e)
        {
          //Code for the LED to glow
            sp1.Write("1");
         
        }

        private void stop_Click(object sender, RoutedEventArgs e)
        {
            //Code for LED to stop
            sp1.Write("0");
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            sp1.Close();
        }    
    }
}

In the above code, the library required for the function is  System.IO.Ports. Thus if you are using older versions, make sure you have one.


Step5:: As the UI is created, the above code sends characters to Arduino via serial port. Now  we will concentrate on the Arduino code. Well the arduino code is too simple. For compilation of the code you need the Arduino IDE for Windows. The code is given below.

int led = 13;
int blink=
void setup() {
 
  pinMode(led, OUTPUT);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
 if(Serial.available())
  {
    char c = Serial.read();
    if (c == '1')
    {  
      digitalWrite(led,HIGH);
    }
    else if (c == '0')
    {
      digitalWrite(led,LOW);
    }
    if(c== 'b')
    {
      digitalWrite(led,LOW);
      delay(blink);
    }
     
  }
}

Step6: Now upload the code from the IDE to the hardware. Don't forget to attach the LED. In case of arduino leonardo, attach the long pin of the LED to pin number 13 and the shorter one to ground. Connect the Arduino to the pc via a USB cable. Deploy the code to the device and run the c# code. The UI will pop up. If you press the "Start" key, the LED will glow. If you press the "Stop" key, the LED will stop glowing.
Schematic for connection




As shown in the figure, you can use the resistor. But it's not mandatory for you to use the resistor. You may experience problem if you are doing it for the first time. In case of any queries, click here to send a mail.