Top Banner
Serial Communication between Arduino and LabVIEW Using LabVIEW as a Graphical User Interface
47

Serial Communication between Arduino and LabVIEW

Mar 15, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Serial Communication between Arduino and LabVIEW

Hans-Petter Halvorsen

https://www.halvorsen.blog

Serial Communication between

Arduino and LabVIEWUsing LabVIEW as a Graphical User Interface

Page 2: Serial Communication between Arduino and LabVIEW

• Introduction to Serial Communication with Arduino• Serial Monitor• Serial Plotter• Serial Monitor and Serial Plotter is nice to use since Arduino programs have no

GUIs– Note! Typically, you use Serial Monitor to present values for different variables– Send Data: You can also use the Serial Monitor to update variables, etc.– Examples

• In stead of using Serial Monitor and Plotter you can create similar (or better) functionality using LabVIEW– Create Serial Plotter in LabVIEW– Create LabVIEW GUI Interface that Communicates with the Arduino Code– Examples

• LabVIEW LINX

Contents

Page 3: Serial Communication between Arduino and LabVIEW

Hans-Petter Halvorsen

https://www.halvorsen.blog

Serial Communication with Arduino

Table of Contents

Page 4: Serial Communication between Arduino and LabVIEW

• Arduino is a Microcontroller• Arduino is an open-source platform

with Input/Output Pins (Digital In/Out, Analog In and PWM)• Price about $20• Arduino Starter Kit ~$40-80

Arduino UNO

with Cables, Wires, Resistors, Sensors, etc.

Page 5: Serial Communication between Arduino and LabVIEW

Configuration

Sensors

ArduinoPC

USB cable Type A-B

Arduino IDE

PC with the Arduino Programming Environment

Page 6: Serial Communication between Arduino and LabVIEW

Arduino Programming Environment

In this window you create your

Program

Compile and Check if Code is OK

Creates a New Code Window

Open existing Code

Upload Code to Arduino Board Save

Open Serial Monitor

Error Messages can be seen herewww.arduino.cc

The software can be downloaded for free:

Page 7: Serial Communication between Arduino and LabVIEW

• Serial.begin(9600)–Open the Serial Port and set Baud rate

• Serial.print(“Hello”)• Serial.println(“Hello”)• https://www.arduino.cc/reference/en/lan

guage/functions/communication/serial/

Serial CommunicationSpeed: Baud Rate in bits per second

Page 8: Serial Communication between Arduino and LabVIEW

Arduino Exampleint x = 0;void setup() {Serial.begin(9600);

}

void loop() {Serial.print(x);x++;delay(1000);

}

Page 9: Serial Communication between Arduino and LabVIEW

Hans-Petter Halvorsen

https://www.halvorsen.blog

Serial Monitor

Table of Contents

Page 10: Serial Communication between Arduino and LabVIEW

Serial Monitor

Page 11: Serial Communication between Arduino and LabVIEW

xxx

Page 12: Serial Communication between Arduino and LabVIEW

Arduino Exampleint x = 0;void setup() {Serial.begin(9600);

}

void loop() {Serial.println(x);x++;delay(1000);

}

Page 13: Serial Communication between Arduino and LabVIEW

xxx

Page 14: Serial Communication between Arduino and LabVIEW

Hans-Petter Halvorsen

https://www.halvorsen.blog

Serial Plotter

Table of Contents

Page 15: Serial Communication between Arduino and LabVIEW

Serial Plotter

Page 16: Serial Communication between Arduino and LabVIEW

xxx

Page 17: Serial Communication between Arduino and LabVIEW

Arduino Examplefloat x = 0;float y;

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

}

void loop() {

y = sin(x);Serial.println(y);

x = x + 0.1;delay(100);

}

Page 18: Serial Communication between Arduino and LabVIEW

Arduino Examplefloat x = 0;float y;

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

}

void loop() {y = sin(x);Serial.print(y);

y = cos(x);Serial.print("\t");Serial.println(y);

x = x + 0.1;delay(100);

}

Page 19: Serial Communication between Arduino and LabVIEW

Hans-Petter Halvorsen

https://www.halvorsen.blog

Send Serial Data

Table of Contents

Page 20: Serial Communication between Arduino and LabVIEW

Send Serial DataWe can also send Serial Data using the Serial Monitor or the Serial Plotter

Page 21: Serial Communication between Arduino and LabVIEW

Examplechar input;

void setup() {Serial.begin(9600);Serial.println("Are you ready (Y/N)?");

}

void loop() {if (Serial.available()>0){input = (byte)Serial.read();

if (input == 'Y'){Serial.println("Great. You are ready");

}else if (input == 'N'){Serial.println("Let me know when you are ready");

}}delay(100);

}

Page 22: Serial Communication between Arduino and LabVIEW

Example

Page 23: Serial Communication between Arduino and LabVIEW

Examplechar input;int x;int y;

void setup() {

Serial.begin(9600);}

void loop() {

if (Serial.available()>0){

input = (byte)Serial.read();

if (input == 'x'){

x = random(0,10);Serial.println(x);

}else if (input == 'y'){

y = random(20,30);Serial.println(y);

}}delay(100);

}

Page 24: Serial Communication between Arduino and LabVIEW

Hans-Petter Halvorsen

https://www.halvorsen.blog

LabVIEW Serial Arduino Plotter

Table of Contents

Page 25: Serial Communication between Arduino and LabVIEW

• LabVIEW is Graphical Software• LabVIEW has powerful features for

simulation, control and DAQ applications

LabVIEW

Basic LabVIEW Example:

Page 26: Serial Communication between Arduino and LabVIEW

Configuration

Sensors

ArduinoPC

USB cable Type A-B

LabVIEW

Page 27: Serial Communication between Arduino and LabVIEW

Arduino Code float x = 0;float y;

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

}

void loop() {y = sin(x);Serial.println(y);

x = x + 0.1;delay(100);

}

Page 28: Serial Communication between Arduino and LabVIEW

Running Example

Page 29: Serial Communication between Arduino and LabVIEW

LabVIEW Code

Page 30: Serial Communication between Arduino and LabVIEW

LabVIEW Code

Page 31: Serial Communication between Arduino and LabVIEW

LabVIEW Code

Page 32: Serial Communication between Arduino and LabVIEW

Multiple Data

Page 33: Serial Communication between Arduino and LabVIEW

Arduino Code double x = 5;void setup() {

Serial.begin(9600);}

void loop() {

x = random(1,100)/10.0;Serial.print(x);

x = random(1,100)/10.0;Serial.print("\t");Serial.print(x);

x = random(1,100)/10.0;Serial.print("\t");Serial.println(x);delay(1000);

}

Page 34: Serial Communication between Arduino and LabVIEW

LabVIEW Code

Page 35: Serial Communication between Arduino and LabVIEW

Hans-Petter Halvorsen

https://www.halvorsen.blog

LabVIEW GUI Interface

Table of Contents

LabVIEW GUI Interface that Communicates with the Arduino Code

Page 36: Serial Communication between Arduino and LabVIEW

xxx

Page 37: Serial Communication between Arduino and LabVIEW

Running Example

Page 38: Serial Communication between Arduino and LabVIEW

Arduino float a=1.0;float x;float y;

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

}

void loop() {if (Serial.available()>0){a = Serial.parseFloat();

}

x = random(0,100)/10.0; //Random Value between 0-10y = a*x;Serial.println(y);delay(1000);

}

Page 39: Serial Communication between Arduino and LabVIEW

LabVIEW Code

Page 40: Serial Communication between Arduino and LabVIEW

LabVIEW Code

Page 41: Serial Communication between Arduino and LabVIEW

LabVIEW Code

Page 42: Serial Communication between Arduino and LabVIEW

Hans-Petter Halvorsen

https://www.halvorsen.blog

LabVIEW LINX

Table of Contents

Page 43: Serial Communication between Arduino and LabVIEW

• The LabVIEW LINX Toolkit adds support for Arduino• This means we use LabVIEW Programming instead of

Arduino Programming• In this Tutorial we have just used LabVIEW as an interface for

communication with your existing Arduino code• If use want to use LabVIEW 100% in your application,

LabVIEW LINX is a good alternative to the examples provided in this Tutorial

• I have made several other Tutorials and Videos where I introduce and use LabVIEW LINX– https://www.youtube.com/IndustrialITandAutomation– https://www.halvorsen.blog

LabVIEW LINX

Page 44: Serial Communication between Arduino and LabVIEW

LabVIEW LINX Example

Page 45: Serial Communication between Arduino and LabVIEW

Installing LabVIEW LINX ToolkitUse VI Package Manger

Note: Do not install this package if you are running LabVIEW 2020 Community Edition or later, as the Community Edition already includes the LabVIEW LINX Toolkit

Page 46: Serial Communication between Arduino and LabVIEW

• Arduino is great, but it lacks a Graphical User Interface (GUI)

• We have the Serial Monitor and Serial Plotter, but they are very limited

• I this Tutorial LabVIEW has been used to extend the Arduino by creating a GUI in LabVIEW, both for view/plotting data and for updating variables

• An even more flexible extension can be to use LabVIEW LINX, which I demonstrate and use in many other Tutorials and Videos

Summary

Page 47: Serial Communication between Arduino and LabVIEW

Hans-Petter Halvorsen

University of South-Eastern Norwaywww.usn.no

E-mail: [email protected]: https://www.halvorsen.blog