Google+
Join free weekly webinars
Find us on
Back

How to control the Arduino boards in EventIDE

The Arduino USB boards provide an easy way to communicate with an arbitrary electrical hardware by a computer USB port. The attached hardware may include LEDs, motors, sensors, response buttons, etc. Current Arduino models feature an USB interface, analog input pins and digital I/O pins and a programmable processor. Arduino boards can be used as low-cost DAQ cards in many research scenarios. This post describes how to communicate with an Arduino card in EventIDE. Consider, for example, that you want to control a LED attached to the digital pin 13 on an Arduino board. 

 

First, you will need to prepare the following tools:

 

  • An Arduino board, connected to your computer by USB
  • The official Arduino IDE and drivers (download from here)
  • a LED, attached to the pin 13 and ground pin on the board

Upon installation the Arduino driver creates a new COM port that can be used to communicate with the board. There are no predefined serial commands, but the board can easily programmed to listen and execute custom user commands.

 

Run the Arduino IDE and select the Arduino serial port in the Tools menu:

image

 

Then paste the following script into Arduino editor:

   1: #define DIGITAL_PIN 13
   2: #define BAUD_RATE 9600
   3: #define CommandTerminator '#'
   4: #define OnCommand "ON"
   5: #define OffCommand "OFF"
   6:  
   7: char C;
   8: String Command;
   9:  
  10: void setup()
  11:   {    
  12:   //create a serial connection with the selected rate
  13:   Serial.begin(BAUD_RATE);
  14:   // Enable pin 13 for digital output
  15:   pinMode(DIGITAL_PIN, OUTPUT);
  16:   }
  17:  
  18: void loop()
  19: {    
  20:     //if we have some incomming serial data then..
  21:     if (Serial.available())
  22:       {
  23:         //read 1 char from the data sent by the pc
  24:         C = Serial.read();
  25:         if (C!=CommandTerminator)
  26:           Command=Command+C; //if char is not a terminator, add it to the end of the command buffer  
  27:           else
  28:           {
  29:           //if char is a terminator, check the buffer for a known command
  30:           if (Command=="ON")
  31:              digitalWrite(DIGITAL_PIN,HIGH);
  32:           if (Command=="OFF")
  33:              digitalWrite(DIGITAL_PIN,LOW);
  34:           // clear the buffer for the next command   
  35:           Command=""; 
  36:           }
  37:       }
  38:     delay(1); // wait for another char
  39: }

Once you upload this script to the board (via the Upload button), it begins to continuously listen the data arriving via the serial port. The script recognizes two string commands, “ON#” and “OFF#”, that switch the LED on and off. Note the commands must be terminated with ‘#’ character.

 

Start EventIDE now and add a new Serial Port element. Leave the element properties with default values, except for the Port Name, which has to point to the Arduino port, COM7 in this example.

 

To test communication, type a command, ON# or OFF#, in a field of the ‘Output Buffer’ property of the Serial port element and press the run button next to the ‘Test Output’ property. The typed command will be send to the Adriano script, which, in turn, will switch the attached LED.

 

At runtime you can send an instant command to Arduino with a simple code, if you create proxy variables, linked to the ‘Output Buffer’ and ‘Send Now’ properties:

   1: OutputBuffer="ON#";
   2: SendNow=true;

 

Using a similar approach, you can write an Arduino script that reads pin data from a board and sends it into the Adriano serial port. The Serial port element in EventIDE is also able to read incoming port data, when the element works in the input modes.

 

 

Useful links:

Arduino official website

Arduino script language reference

Arduino C# page