Sending data from LabVIEW to an Arduino over serial (USB or UART) is straightforward with NI-VISA. Here’s a clean, repeatable setup that works on Windows, macOS, or Linux.
1) Prereqs
- Arduino: upload a sketch that listens on Serial at a known baud (e.g., 115200).
- Drivers: NI-VISA installed (and Arduino USB driver on Windows).
- Port: note the Arduino COM port (Windows: COMx; macOS/Linux: /dev/tty.//dev/ttyACM).
- Close other apps: Arduino Serial Monitor/Plotter must be closed (they lock the port).
2) Arduino example (reads lines, parses, echoes)
// Set a simple, line-based protocol: e.g., "LED,1\n" or "PWM,120\n"
void setup() {
Serial.begin(115200);
// Give the board time to reboot when VISA opens the port
while (!Serial) { ; } // for Leonardo/Micro; harmless elsewhere
delay(2000); // LabVIEW open toggles DTR → UNO resets
Serial.println("READY");
}
void loop() {
if (Serial.available()) {
String line = Serial.readStringUntil('\n'); // expects newline from LabVIEW
line.trim();
// Example: command,value
int comma = line.indexOf(',');
String cmd = (comma >= 0) ? line.substring(0, comma) : line;
String val = (comma >= 0) ? line.substring(comma + 1) : "";
if (cmd.equalsIgnoreCase("LED")) {
// do something with val...
Serial.println("ACK:LED=" + val);
} else if (cmd.equalsIgnoreCase("PWM")) {
int duty = val.toInt();
// analogWrite(pin, duty); // example
Serial.println("ACK:PWM=" + String(duty));
} else if (cmd.length()) {
Serial.println("ERR:UNKNOWN");
}
}
}
Tip: Use a newline protocol; it’s easy to generate and parse on both sides.
3) LabVIEW (VISA) block diagram recipe
**
**Minimal one-shot send:
- VISA Open → VISA Configure Serial Port
- Baud: 115200 (match sketch), Data bits: 8, Parity: None, Stop bits: 1
- Flow control: None
- Property Node (VISA Serial) (optional but helpful)
- Set Termination Character Enable = True
- Termination Char = decimal 10 (LF, \n)
- Set DTR/RTS not required (leave default)
- Wait (ms) = 2000 (Arduino UNO/MEGA reset after open)
- VISA Write: wire a String Control → e.g., "LED,1\n"
- (Optional) VISA Read (e.g., 64 bytes) to capture "ACK:LED=1"
- VISA Close
Continuous/interactive VI:
- Wrap steps 4–5 in a While Loop with a Stop button.
- Add a Bytes at Port property (read → feed into VISA Read count), or rely on termination char.
Sending raw bytes instead of text:
Build U8 array → Byte Array to String → VISA Write.
4) Common pitfalls & fixes
Board resets when you open VISA
Expected on UNO/MEGA (DTR toggles). Add 2 s delay after Configure, or hardware-disable auto-reset (advanced).No response
Check baud rate, newline (\n) presence, and that Serial Monitor is closed.Garbled text
Baud mismatch or multiple opens. Ensure only one VISA session and correct baud.Timeouts on VISA Read
Enable termination (\n) or use Bytes at Port to read the available count.COM port not found
Replug Arduino; verify in Device Manager / ls /dev/tty*. Use the exact resource name in VISA.Flow control
Keep at None unless you know you need RTS/CTS or XON/XOFF.
5) Using pins 0/1 (TTL UART) instead of USB (optional)
- Connect Arduino D0/D1 (RX/TX) to a 3.3/5 V TTL-USB adapter (not RS-232 levels!).
- Cross lines: PC-TX → D0 (RX), PC-RX → D1 (TX), and common GND.
- In LabVIEW, you’ll still use VISA on the adapter’s COM port.
6) Alternative: LINX/LabVIEW toolkits (optional)
If you prefer not to maintain your own protocol, the LabVIEW LINX toolkit (MakerHub/Digilent) can deploy a firmware to Arduino and provide ready-made VIs. For simple “send line / read line” tasks, plain VISA (as above) is lighter and faster to set up.
Quick checklist (TL;DR)
- Match baud on both sides.
- Use newline-terminated strings.
- Add 2 s delay after opening the port (UNO reset).
- Close other serial apps.
- In LabVIEW, enable termination char = LF and use VISA Write/Read.
Top comments (0)