Saturday, June 9, 2012

Arduino + Labview


   This is a quick (REALLY quick) writeup on establishing a connection between a custom designed Arduino Board using Atmega8 and LabVIEW.  I used a very simple setup where Arduino would receive data  serially  from LabVIEW. To demonstrate this I have used an example of RGB-colour gradient. This employs a simple 2-way communication, the Arduino would listen for a comma separated values of red, green and blue at the beginning of each iteration. If a command is heard, it would react by first parsing these values and then changing the color of the RGB LED, depending on the combination of red, green and blue color received. If nothing was heard, then it would continue on through the read cycle. The method is inefficient and barbaric, but my intent was proof-in-concept.  The concept may be improved upon to increase reliability and efficiency.



Step 1:  Quick Arduino Sketch

    The first thing we need to do is write a sketch for the Arduino, to prepare it for taking values from the serial port and parse it to ensure correct intensities are set for red, green and blue pins of the RGB led..
/*
* rgb values are sent to the arduino board and as a comma seperated list of decimal codes of 0-255
* for example rgb could look like this 213,89,46.
* This program accepts these codes form serial comm port and seperates rgb values using (,) comma as the seperator
* and sends them to the leds
*/
// global variable declarations
int incomingByte = 0; // to hold incoming serial data
// pin number to connect red green and blue leds
int red = 9;
int green = 10;
int blue = 11;
int brightness = 0; //default brightness level of leds
int comma; //to keep a track of which led to process
int pin[] = {red , green , blue}; //the list of pins used for red, green and blue
int i = 0;
int x,l,j;
int temp[3];
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bits per second
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(green,OUTPUT);
}
void loop()
{
//wait for transmission of output serial data to complete
Serial.flush();
// send data only when you receive data:
if (Serial.available() > 0)
{
//confirm that data received
Serial.println("processing");
delay(100);
comma = 0;
do
{
// read the incoming byte:
incomingByte = Serial.read();
//the byte received would be processed as ascii code and so as to process it as integer subtract ascii code of 0 (48) from it
incomingByte -= '0'; //get the number as integer
//As all characters have been subtracted by the ascii code of 0
// ,(comma) ie 44 becomes -4
//loop to read the comma seperated incoming byte and add it to a temporary arrray
while(incomingByte != -4) //while incomingByte != comma
{
if((incomingByte>=0)&&(incomingByte<=9))\
{
temp[i] = incomingByte;
i++;
} //end if
incomingByte = Serial.read();
incomingByte -= '0';
}//end while
j = i;
i = 0;
brightness = 0;
//only a number can be sent to a pin and not an array so temp array must be changed to one number
for( x = 0,l = j; x<j; x++,l--) //convert the array to a single number
{
brightness = brightness + temp[x]*pow(10,l-1);
}//end for
brightness++;
analogWrite(pin[comma],brightness);
//use this to debug code
// Serial.print("brightness ");
// Serial.print(brightness);
// Serial.print(" pin ");
// Serial.println(pin[comma]);
comma++;
}while (comma < 3);
}//end if
}//end loop
view raw RGB.ino hosted with ❤ by GitHub
This is accomplished with a simple sketch that accepts comma separated values of red, green and blue and constantly transmits a message over the Serial comm. 





Step 2:  Let's do the Labview VI

    The trick in this step is more about the right serial data than anything else.  Below is a snapshot of the VI I threw together to connect to the Arduino.


























The Labview Vi creates a comma separated values of the colors and sends it to Arduino, which simply parses that string and does it's job. With that all wired together, save it as a VI named Arduino or something along those lines. Hypothetically you could set the baud rate as a constant 9600, but I left it changeable in case I feel crazy one day and want to spice up that rate a bit.

As a side note, the VI is a handy thing to be able to build, and when you establish a successful connection with the Arduino with reliable data flow and communication, I recommend wrapping the whole thing up as a VI so all you need to do is set the Baud Rate and Resource Name on input nodes and the output node will flow the raw data for you to do with whatever you want.

Step 3:  Now Look at the front-end to see if everything is perfect





Step 4:  Plug in the Arduino and turn it on

Plug in the Arduino and turn it on. Go to the Front Panel and select the appropriate VI resource name (which is hidden under the settings panel on my front panel) and flip the “On” switch.  If all goes well, on modifying the R, G and B values in the front-end above, the RGB Led would change color to the color shown in the front end. 

From here it is just a matter of using your new found connectivity to do something revolutionary.  Feel free to leave me any questions you might have.
You can get the code here

7 comments:

  1. nice post on integrating the powerful Labview and Arduino.

    ReplyDelete
  2. Actually there's more coming up, keep watching this space.

    ReplyDelete
  3. Thanks for posting this up, this is exactly what I'm looking for!

    I'm new to Arduino and want to communicate my Arduino Uno with Labview 2011.

    I've followed all the instructions here --> https://decibel.ni.com/content/docs/DOC-15971 but to no avail.

    I couldn't find the LVIFA_Base pde file to be uploaded to Arduino 1.0.1

    Any help would be much appreciated. Sorry for the noob question.

    ReplyDelete
    Replies
    1. Hi,
      Labview interface for Arduino has very limited functionality. This is a very basic project to do serial communication between labview and any other device using VISA. I suggest you use Visa to connect your arduino with labview. Compile and download the .pde file and also the Labview file is there which can be used as-is given with this link.
      http://sdrv.ms/NHGRi4



      http://sdrv.ms/NHGRi4

      Delete
    2. Note: We have shifted our blog to http://teamembedded4u.wordpress.com/

      Delete
  4. Hello, the color will still be able to change color even though i didn' t plug in my arduino. why is it so?

    ReplyDelete
  5. Thank You and I have a tremendous offer: House Renovation What To Do First home kitchen remodel

    ReplyDelete