Click here to Skip to main content
15,881,139 members
Articles / Internet of Things / Arduino

ArduinoDotNet -- A C#/VB Based Live Arduino Interface

Rate me:
Please Sign up or sign in to vote.
4.32/5 (9 votes)
17 Apr 2017CPOL3 min read 18.9K   16   2
ArduinoDotNet is an interface built to control the Arduino directly from a .NET app

Introduction

ArduinoDotNet is a tiny interface built to control the Arduino directly from a .NET application. The idea is NOT new and is NOT robust, but, this library was missing from observable web and I was compelled to add one. (This tip is about the ArduinoDotNet library present on THIS GitHubRepository.)

The Idea

An Arduino is integrated with the PC and controlled directly from the PC applications. Individual pins can also be read and written from the PC application. It is obvious that a specific ArduinoDotNet_Driver sketch resides on the Arduino which complements the PC interface. Other code can also go alongside this sketch, but is not recommended. The PC application, after importing/using the ArdunioDotNet library, can access digitalWrite, analogueWrite, pinMode, etc. right from the .NET environment. These functions do the necessary communication with the Arduino to implement the same function within the Arduino and to return the values, if necessary. This way, we have digitalWrite, analogueWrite, etc. functions within our C# (C Sharp), VB (Visual basic) or other .NET languages.

The Story

Once, I had to work with an electronics legend who has spent his life designing, repairing and re-engineering cool electronics gadgets, precision instruments, military equipment, radios, televisions, legacy microcontrollers, etc. He faces no problem creating a human machine interface using controllers like 8051, 8bit PIC and digital logic design. The only problem he seemed to face was that the world had changed so rapidly that interfacing an Arduino with SD card needed some learning. He had worked on .NET applications in Visual Basic and done all sorts of things like creating GUIs, saving data on the disc, etc.

Now, he had interface some sensors with an Arduino and log the data on an SD card. Instead of saving the data using Arduino on an SD card, he sends character commands via serial to an Arduino which implements the sensor routine and returns the data on serial which is eventually shown in a textbox on the computer screen.

Why so Indirect?

This technique seems quite indirect but reduces the design complexity for most kinds of data acquisition application. I had this thing in mind for years but never considered worth spending time on. With this experience, I realized that I should make an eloquent interface which integrates an Arduino with a PC seamlessly. It’s just like adding a parallel port to the PC. Not just a parallel port, but a full fledge microcontroller which has its own PWM generators, ADC, and many other peripherals.

Using the Codes

  • Clone/download the whole GIT from THIS git.

  • Create/open a .NET application, either in VB, C# or other .NET languages.

  • Depending upon the version of .NET Framework of your application (Visual Studio >> Project >> Properties >> Application), add reference to the appropriate ArduinoDotNetDNX_X.DLL to the project.

  • Import/use the namespace ArduinoDotNet and create a global ArduinoDotNetInterface object.

  • Open the interface using the COM port name where the Arduino is connected.

  • Use FlashFirmware method to flash the required driver on the Arduino. (This step is required only the first time an Arduino is prepared for interfacing. The driver can also be uploaded using the ArduinoDotNet_Driver.ino file under Arduino IDE.)

  • Start using the functions like digitalRead, analogue read, etc. under the ArduinoDotNetInterface object.

Example (C#)

C#
using ArduinoDotNet;
using System.Windows.Forms;

namespace ArduinoDotNetTestApplication
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            ArduinoDotNetInterface arduino = new ArduinoDotNetInterface();
            arduino.Open();
            arduino.pinMode(2, true);
            arduino.pinMode(Pins.A0, 0); // only Arduino Mega
            int i = arduino.analogRead(Pins.A0);
            if (i > 500)
            {
                MessageBox.Show("The Value is greater than 500");
                arduino.digitalWrite(2, true);
            }
            else
                arduino.digitalWrite(2, false);

            arduino.Close();
        }
    }
}

History

  • 18th April, 2017: Initial version
This article was originally posted at https://github.com/umartechboy/ArduinoDotNet

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer techCREATIONS
Pakistan Pakistan
Developer, Programmer, Beta Tester; technically, i'm none of these. I'm a mechanical engineer, programming is my passion, my hobby and my amateur non profit profession. I program when ii need and innovate whenever, wherever i want.

Learned:
C#

Mixed:
C#+Applied Mathematicss-Robotics+C++

Developed:
C# OMR Reader
Monopoly (Urdu language)
HybridAutomation Framework
SMS Bomber (Windows Mobile 6 Professional)
Hard disk watch tower
Farmville Super Clicker
Games Profile selector
Windows mobile salat reminder
Windows mobile SMS Pole Host
and alot of other small apps

Comments and Discussions

 
SuggestionVery nice Pin
Marco Bertschi17-Apr-17 21:40
protectorMarco Bertschi17-Apr-17 21:40 
GeneralRe: Very nice Pin
umar.techBOY19-Apr-17 10:27
umar.techBOY19-Apr-17 10:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.