Fork me on GitHub

Arduino Serial Communication

Submitted by Mauro Cicolella on 2012, December 19 - 16:08
Your rating: None Average: 4.8 (4 votes)
Driver
Development Status: 
Prototype
Developer: 
Mauro Cicolella
Tested on: 
Linux
Windows
Download Link: 
Download Link: 
Download Link: 
Short Description: 

This plugin is a basic example to communicate with an arduino board by serial connection.

Overview

This example shows how to control an arduino board connected by usb interface in order to switch on/off a led. The code is very simple but it's a base from which to create more complex plugin using serial protocol. It uses our Serial framework (based on RXTX project) so read this tutorial for more info.

NB: The last available code version (5.6) is based on a new serial framework. It's compatible only with the last dailybuild and requires Java 1.7+.

 

Configuration

To configure the plugin open arduinousb-manifest.xml in its folder and change the following properties. By default you need to change only port name and set the one you are in use.

  • serial.port: port name e.g. /dev/ttyACM0 for Linux or COMx for Windows
  •  serial.baudrate: baudrate of arduino serial port (the same set in the sketch)
  •  serial.databits: databits of arduino serial port
  •  serial.parity: parity bit of arduino serial port
  •  serial.stopbits: stop bit of arduino serial port
  •  chunk.terminator: read serial string terminator (by default "
" as "\n")
  •  chunk.size: size of chunk to read from serial
  •  delimiter: fields delimiter in read string

Source code

The plugin source code is hosted here.

Arduino sketch

The following sketch must be uploaded to your arduino board. It's very simple: when it receives a char 'a' from serial connection it switch on the led connected to pin 13. Viceversa with 'b' char the led is switched off. Every command writes a string on serial.

 

void setup(){
  Serial.begin(9600);

  //Set all the pins we need to output pins

  pinMode(13, OUTPUT);
}

void loop (){
  if (Serial.available()) {

    //read serial as a character
    char ser = Serial.read();

    //NOTE because the serial is read as "char" and not "int", the read value must be compared to character numbers
    //hence the quotes around the numbers in the case statement
    switch (ser) {
        case 'a':
        pinON(13);
        Serial.println("13;on");
        break;
        case 'b':
        pinOFF(13);
        Serial.println("13;off");
        break;
    }
  }
}

void pinON(int pin){
  digitalWrite(pin, HIGH);
}

void pinOFF(int pin){
  digitalWrite(pin, LOW);
}


 

Video

Support

For support please write on our forum.

Open Source Internet of Things Framework Drupal theme by Kiwi Themes.