LabVIEW and Arduino: Get Started in Minutes.
A couple of weeks ago, as I was changing jobs, my (now) former employer asked me to give a crash course in LabVIEW. The audience consisted of interns who would possibly take over my LabVIEW work.
After thinking about it, I thought: “Let’s pull in some cheap hardware (An Arduino) and hook it up with LabVIEW… That should keep these young spirits engaged”. So I want to share that experience with anyone who is trying to do the same thing.
The Plan
So the first exercise was to have LabVIEW show an LED that would follow the Arduino’s On-Board LED:
- There is already a standard sketch to “blink” an LED for Arduino Devices. We only have to send the state of the LED over Serial Communication for LabVIEW to intercept and update accordingly.
- On LabVIEW side, there is already the standard “Serial Communication” example. It can be modified to incorporate a loop, a stop button and a LED that turns ON or OFF following the Arduino’s lead.
Time to completion: Less than 10 minutes.
Phase 1: Arduino
Remember:
The Best Code is the Code not written…
So in the Arduino IDE:
- Select the “Blink” Example (File > Example > 01 Basics).
- Setup parameters (Board Type, Comport,…) in the IDE for your board (Any board that runs Arduino will do).
- Download the sketch on your device, and verify you have a LED that blinks.
If it does, we can move to the next step:
Update Blink LED Sketch
In the Setup section, we will initialize Serial Communication at 9600 Baud Rate. We will then publish a message just after the LED state changes.
Phase 2: LabVIEW
As stated previously, there is already an example shipping with all versions of LabVIEW. It does most of what we need.
Close the Arduino Serial Monitor before proceeding… since LabVIEW is going to try to open that Comport too.
- Navigate to LabVIEW Example (Help > Find Examples), and look for “Simple Serial.vi”
- Open this VI, setup Comport to match configuration in Arduino IDE (see above)
- Run the VI, and you should see the message published by the Arduino device.
If that worked then we can proceed to updating the VI.
Update “Simple Serial.vi”
See all updates made to the “Simple Serial.vi” in blue below.
In essence,
- We clear the I/O Rx Buffer every time our application starts, since the device pushes messages even when no one is listening. This buffer will fill up and create a backlog for LabVIEW that needs to be fully processed before its LED state (named “Remote LED State”) follows in Real Time the state of the Arduino LED.
- We add a while loop and stop button and make sure to exit the loop on error. Also, close the VISA session references outside the loop.
- We parse the message received from the Arduino. If it contains “ON” we switch on the LabVIEW LED.
- We handle a corner case: Since we read from the serial port faster than the device publishes messages, what do we do with our LabVIEW LED when we haven’t received anything? One Simple solution is to remember the previous valid value (using the shift register) and use that instead.
Et Voila…
Okay, that’s the gist of it. The idea is that someone who is just getting his feet wet using these two technologies can find some (hopefully) helpful resource here to get him started.
Bonus
The scenario above has the Arduino device controlling the LED and LabVIEW acting as an indicator for its state. Let’s turn things around and let LabVIEW control the LED now.
Arduino Sketch
Once again, we will update an Example shipping with the Arduino Installation (File > Examples > Communications > ReadASCIIString).
LabVIEW VI
We will update the previous VI to include sending command to the Arduino. See comment in Green.
And that’s it! Now, changing the state of the control button in LabVIEW forces a change of state on the Arduino device which then publishes its new state on the comport. LabVIEW then updates its own LED to follow the content of the message published. Easy Peasy,…
As I said, previously, I had to put together this small tutorial as a means to get new interns to started with LabVIEW. I’m documenting it here for public use.
That’s it for me. If you have questions / comments / remarks, get in touch.