Arduino Tutorial: C# to Arduino Communication. Send data and commands from Computer to an Arduino.

Arduino Tutorial: C# to Arduino Communication. Send data and commands from Computer to an Arduino.


Dear friends welcome to another Arduino Tutorial! Today we are going to build a C# windows application
to send data and commands to an Arduino. Let’s get started! Hello guys, I am Nick and welcome to educ8s.tv
a channel that is all about DIY electronics projects with Arduino, Raspberry Pi, ESP8266
and other popular boards. Subscribe to the channel now if you don’t
want to miss any future videos. In this video we are going learn how to build
our own computer programs, in order to communicate with Arduino. I have built a simple project to demonstrate
how easy it is to build your own Windows applications that can control an Arduino. I have connected an LCD shield to an Arduino
Mega, and I have also connected three LEDs to it. The application that I have developed connects
to the Arduino using the Serial connection, and allow us to write some text to the LCD
display and control the state of the LEDs. As you can see this is a very useful project
if you need to send some data from your computer to an Arduino. Let’s see how to build this project. The parts needed in order to build this project,
are the following: • An Arduino Mega
• An LCD shield • Three LEDs
• Three 100Ω Resistors for the LEDs • A small breadboard
• Some wires The cost of the project is around $15. You can find links for all the parts I use
in the description of the video below. Of course you can use an Arduino Uno to build
this project or any Arduino board you like. I am using the Arduino Mega because it has
more pins available so it makes things easier to demonstrate. First of all, let’s build the hardware part
of the project. It is very easy to build since we are using
a shield. We connect the LCD shield to the Arduino like
this, and then we connect the LEDs. I am using pins, 19,20,21 for the LEDs. Connect a resistor to the long leg of each
LED and the other leg to GND. You can find the schematic diagram of the
project in the description of the video. If we program the Arduino, we can see, that
the LCD display shows the message “Ready to connect”, which means the Arduino is
programmed successfully and it is waiting for commands from the computer. Then we open the Computer application, we
select the serial port that the Arduino is connected to and we press connect. Now we can control the Arduino from the computer! How cool is that! Let’s now see how to develop the computer
software to send commands to the Arduino. The software that we are going to develop
today, runs only on Windows, it won’t work on Linux or Mac. We are going to use Visual Studio 2017 community
edition in order to develop the application. Visual Studio 2017 community edition is absolutely
free. It is great development environment for Windows. With Visual Studio, you can literally build
simple Windows Applications in minutes. First of all you have to download the installer,
and install it on your machine. When prompted, select to install the .NET
desktop development package and then press the install button. After the installation is complete you can
launch the development environment. Now you have two options, you can create a
new project and build your own application from scratch using the C# programming language
or you can download my project and modify it to achieve your desired results. Of course you want to just test the project
and can just run the executable that you can find in the description of the video. If this is the first time you are using Visual
Studio, please watch some tutorials on YouTube to help you getting started. Since Visual Studio is very popular you can
find great tutorials online. I won’t show how I built this Windows Application
in detail in this video but if you are interested I can prepare another very detailed tutorial
on that. Are you interested in a detailed tutorial
on how to build a Windows Application like this on Visual Studio? Please vote by clicking on the card here. Let’s see how you can build your own Application. From the Menu on the Start Page we Select
File->New ->Project and we select to create a Visual C# ->Windows Classic Desktop ->Windows
Forms App. We define a name for the Project and we press
OK. That’s it a new project has been created
and we can start to build our UI. As you can see, there are two tabs on the
main project window. In the Form1.cs file we write our C# code. In the Form1.cs [Design] tab we design the
User Interface of our Application. From the toolbox on the Left, we can select
any tool we like and we can drag and drop it on our form. It is that easy. Each User Interface element has some Properties. We can modify them from this window. For example we can change the button text
here. If we now press the Start button we can see
our application running. Let’s now open the ComputerToArduino project
to see how it works. This is the UI design and this is the code
of the project. Before we start let me explain you how this
project works. I have created a simple communication protocol
for this project which consists of 6 commands. Each command is 4 characters long and it can
be followed by some data. Each command is starts with the # character
and it is terminated by the n symbol. This symbol will tell the Arduino that the
command is complete so it can act. So we have the following commands:
• STAR for Start • STOP
• TEXT • LED1
• LED2 • LED3
The STAR command tells the Arduino to wait for commands from the computer. The STOP command tells the Arduino that the
computer has disconnected from it. The TEXT command is followed by the text we
want to display on the LCD. For example if we write #TEXTYOUTUBEn on
the serial port the word YOUTUBE will appear on the display. The LED1, LED2 and LED3 commands control the
state of each LED. So if we want to turn on the first LED we
write the following command to the serial port: #LED1ONn . If we want to turn off the
second LED we send the command #LED2OF . The same thing applies to the third LED as well. That’s the very basic protocol I use in
this project. Of course you can design and use your own
protocol which can use many more commands. In the code now at first, after the User Interface
is initialized we disable all the controls. Then we get the available serial ports on
the computer and we load them on the combox. When we press the Connect button, the button1_Click
method is called and we try to connect to Arduino if we are not already connected. In order to connect to Arduino, we open the
Serial port, and we send a command to it. We simply write #STARn to the serial port. Then we change the button text and we enable
all the controls on the user interface. Now if we write a text on the textbox and
press the write button, we get the text from the textbox and write it to the Serial port
with the command TEXT. Also, when the state one of the checkboxes
changes we send the command LED1 for example with the state, on or off. Lastly, when we press the connect button and
we are already connected to Arduino we send the STOP command. Now let’s take a quick look at the Arduino
code. At first we initialize the display. Then we wait for serial data. When we receive data on the serial port we
save them in the variable inputString until we receive the n character. When we receive this character we know that
command is complete, so we have to act. If we receive the TEXT command for example
we know that we have to print something on the LCD display. So, we remove the first five characters from
the inputString variable, because that’s the command, we delete then last 2 character
which is the n symbol and we print the remaining characters on the display. We do something similar when we receive an
LED command. We read the next two characters after the
command to see if we need to turn the LED on or off and we act accordingly. As you can see the Arduino code is very simple,
we just use so if statements to respond to the commands sent by the computer. As always you can find the code of the project,
both the Arduino Code and the C# code in the description of the video below. From this demonstration project you can see
that it is relatively easy to build your own Windows applications to control an Arduino. If you learn the basics of C# and Visual Studio
you can build impressive projects. You can build your own computer peripherals! There are so many projects that we can build
around a computer. I have already a project in mind that I am
going to build soon. I am going to build an external Arduino computer
monitor which will display the CPU usage, memory usage and so on in real time. I think this is going to be cool. I would love to hear your ideas as well. Do you find Computer to Arduino communication
useful and if so what kind of projects are you going to build? Please post your comments in the comments
section below and don’t forget to like the video and share it with your friends if you
find it useful! Also consider subscribing to the channel and
do click that bell or YouTube might not show you updates as new videos come out. If you are going to be shopping for parts
check out the affiliate links from the video description. That’s it for today guys, thanks you very
much watching, I will see you in the next video! [ Translating these subtitles? Add your name here! ]

Comments

  1. Post
    Author
  2. Post
    Author
    Ramin Kaleybar

    I just discovered your channel today, the tutorials are awesome, please do more videos on arduino VS communication
    I appreciate your work in advance.

  3. Post
    Author
  4. Post
    Author
  5. Post
    Author
    Matthew C

    i used to use Clion IDE but i"m switching to Visual Studios because it has the .NET package and an extension to program Arduinos. educ8s.tv have you ever used the Visual Studios extensions to program Arduinos before?

  6. Post
    Author
    blybaly

    very simple and great video! please continue c# in details. Also please include remote internet control with c#

  7. Post
    Author
  8. Post
    Author
  9. Post
    Author
    Joe Blow

    I love your videos, thanks for posting them! However, I HATE Visual Studio with a passion. If you're used to being able to modify libraries like with Arduino, don't, you will completely screw up VS if you try to modify anything. I ended up so messed up that even completely removing all traces of it and reinstalling it didn't fix the libraries.

  10. Post
    Author
    Acrimonious Mirth

    Hey, I’m trying to send commands to my Arduino of the form: M104 S200; where “M” is an alpha character that denotes a form of command, “104” is the exact command, “S” denotes the beginning of the value being written and “200” is the value. “;” denotes the end of the command string.
    I’m looking to write it in C as it is less memory-consuming than Arduino but I have no idea how to write it on my Arduino and can’t find anything really helpful online. How should I do this? Thanks 🙂

  11. Post
    Author
  12. Post
    Author
  13. Post
    Author
  14. Post
    Author
    Christos Vrettos

    Very comprehensive tutorial. I find it helpful and easy to follow. This is a useful tool for all of us who try to enter the Arduino world. Thank you so much

  15. Post
    Author
    Dr. Paul Plumbing Heating

    Any Arduino board will do the same job, heck a micro pro & a ic2 display will be the simple hardware to start with… Nick your always Cool overall..

  16. Post
    Author
  17. Post
    Author
    aykut sirma

    Hi educ8s.tv, if it is possible would you make a tutorial about how to use USB Host Shield and Joystick ( HID device ) on Arduino too please ? I try to learn it too but I cannot find a solid sample and guidance to learn it. Thus, I wanted to ask you if you would make such a project and do tutorial for us ? At this link: https://forum.arduino.cc/index.php?topic=511952.new#new , I was asked this same topic there to learn it; but noone still answers to me :(. I look forward your reply to me, thank you.

  18. Post
    Author
    Denis DESCHÊNES

    iam trying to find a way to send ( 0 @ 1023 ) to the arduino with an track bar ????do you have any suggestion …i already tryed the lowbyte and highbyte but doen't work at all….

  19. Post
    Author
  20. Post
    Author
  21. Post
    Author
    Samra Naseem

    Hey! Please tell what i need to write if i want to control rfid reader (connected with arduino) and rfid tag through visual studio. plz tell me as soon as possible.Thanks in advance. here is my address [email protected]

  22. Post
    Author
    Anil Sethi

    thanks for great work ! please please do kindly try to slow down in certain places like when showing which modules to install, but on the whole, youre awesome !

  23. Post
    Author
  24. Post
    Author
  25. Post
    Author
    susana paurelio

    Is it possible that the arduino can change directly a data from android application using GSM Module code

  26. Post
    Author
    Hisam K

    Hello sir…. Very thanks for your great information ……I really enjoyed your work… Plz upload nice videos like this….The video and concept is awesome…

  27. Post
    Author
    Hisam K

    And if you are doing videos like this, I really want to get that also… So please share the links to us….I already subscribed ur videos….

  28. Post
    Author
    EEEWorks

    Well explained sir!!! here’s another project https://youtu.be/UfrL7zlgYtg can you modify the windows app to creat a radio button for controlling the robot to work same as joystick?

  29. Post
    Author
  30. Post
    Author
    Andra Şerban

    Hello. What if I try to print a string on the LCD which exceeds its size? I am trying to display titles from an RSS feed like this: " foreach (SyndicationItem item in feed.Items ){ port.Write("#TEXT" + item.Title.Text + "#n"); } ", but the news are moving really fast on my LCD and I can only see the last one, but incomplete, because the size is to big. Can I make a delay or something between two strings or a continuous slow display of the string? I tried various methods but none of them worked and I thought that you can give me some tips. Thank you in advance for your support and keep going with the great videos.

  31. Post
    Author
  32. Post
    Author
  33. Post
    Author
    Matías Murillo

    Great video man! I'ts going to be very helpfull for the project i'm starting… saddly the link for downloading the code is broken, can you fix it please? 🙂

  34. Post
    Author
  35. Post
    Author
  36. Post
    Author
    Bestin P Sebastian

    Is this possible to get a reading value from a weighing machine to a windows Application using c#?

  37. Post
    Author
  38. Post
    Author
  39. Post
    Author
  40. Post
    Author
  41. Post
    Author
    Ramon Rodriguez Solar

    Would you mind helping me ?
    How could I do this exact thing but with an I2c.
    Me LCD is connected to one, so I'm using 2 wire to program it.

  42. Post
    Author
  43. Post
    Author
  44. Post
    Author
  45. Post
    Author
    Sergio Orozco

    Thank you very much, this tutorial is Gold, you really helped me, i worked in visual c++ windows forms; i'm a beginner and i didn´t have an idea of how to do it but you have done an excellent work, thank you again, now i'm gonna subscribe to your channel hahaha, and i'd like to see more content of this. Thanks for your time on this

  46. Post
    Author
  47. Post
    Author
  48. Post
    Author
  49. Post
    Author
  50. Post
    Author
  51. Post
    Author
  52. Post
    Author
  53. Post
    Author
  54. Post
    Author
    educ8s.tv

    I hope you find this video useful. If you want to support the channel see how here: http://educ8s.tv/support-educ8s-tv/

  55. Post
    Author
  56. Post
    Author
  57. Post
    Author
  58. Post
    Author
  59. Post
    Author
  60. Post
    Author
  61. Post
    Author
  62. Post
    Author
  63. Post
    Author
  64. Post
    Author
    charly potter

    Hi, thanks for that! I have a project idea but I’m a complete novice and would need a YouTuber to be interested in it…fancy hearing?

  65. Post
    Author
  66. Post
    Author
  67. Post
    Author
  68. Post
    Author
  69. Post
    Author
  70. Post
    Author
  71. Post
    Author
  72. Post
    Author
  73. Post
    Author
  74. Post
    Author
  75. Post
    Author
  76. Post
    Author
  77. Post
    Author
    Andrea Gnassi

    Hi!! Your Idea is really cool!! but i have a problem…when I run the program in c # and load the program into arduino, if I go to open the serial monitor I get the following error: Error in opening the serial port "COM7" (Port busy) how can I solve it? thank you so much

  78. Post
    Author
  79. Post
    Author
  80. Post
    Author
  81. Post
    Author
  82. Post
    Author
    Leo Zendo

    password storage and hotkey autofill. It is useful for anonymous browsing(unless your computer has viruses that steal clear code passwords.

  83. Post
    Author
  84. Post
    Author
  85. Post
    Author
    HøLY GhOsT

    I made your idea of external computer stats monitor using a nano and ssd1306
    I would like to see your version

  86. Post
    Author
  87. Post
    Author
    free electron

    Hi Nick – Great C# Arduino project! Could you do one using ESP32 or ESP8266 with a c# form connecting via WiFi or BLE? Thanks!

  88. Post
    Author
  89. Post
    Author
  90. Post
    Author
    Mattia Galiani

    What if I'd like to run this application on MacOS or Linux? What options do I have and which Language/IDE is appropriate?

  91. Post
    Author
  92. Post
    Author
    Josué Barbosa

    Very good, i'm brazilian and your video go me be very help, very thank !
    Sorry my english is not very good !!!
    I go wait for many more videos!!!!

  93. Post
    Author
  94. Post
    Author
    Safework Consulting Group S.A

    i have a question. its posible to import the data directly from pc with software like visual studio?

  95. Post
    Author
    Informaciones Prova Chile

    Could you explain how to read an external eeprom through arduino and visualize the dump in csharp software?

  96. Post
    Author
  97. Post
    Author
    Tajemniczy

    Hi :), do you have maybe tutorial where you show how send 'click button from arduino' to pc app and, will be good with bluetooth / wifi. For example I have small device with button and few leds and connected to this device some bluetooth/wifi adapter/arduino part and on the computer side in my application as I click search for wifi / bluetooth devices so that it can detect my device arduino and remembers it, and then when I click on the device, I would like it to send me a specific message, eg 'managed' to the application on the computer. I would like to use this solution in my project, or rather in a program, where there would be a pair of devices with 1-2 buttons, eg four devices, all of them I would like to connect to my application and then recognize which device was pressed. Sorry for bad english, I hope you understand what I mean @educ8s.tv :).

  98. Post
    Author
  99. Post
    Author
    J_Net Reloaded

    Please make a vid for com port io triggerng relays with 2 pins from a com port with c and i mean actual serial port not uart or usb 🙂

  100. Post
    Author

Leave a Reply

Your email address will not be published. Required fields are marked *