Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
17 Apr 2017 1  
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here