LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to read Data from Arduino using labVIEW VISA

I'm a beginner with LabVIEW and I'm having trouble reading data from Arduino and sending commands to Arduino. Can anyone help me?

#include <OneWire.h>
#include <DallasTemperature.h>

// ================= PINS =================
#define ONE_WIRE_BUS 9
#define MQ8_PIN A0
#define VoltageSensor A1
#define CurrentSensor A2

#define START_PIN 2
#define STOP_PIN 3

#define TEMP_ALARM_PIN 4
#define H2_ALARM_PIN 5
#define TIMER_PIN 6
#define SHUTDOWN_PIN 7

#define RL 10000.0

// ================= OBJECTS =================
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// ================= VARIABLES =================
float Ro = 0;
float filtered_ppm = 0;

float R1 = 30000.0;
float R2 = 7500.0;

float sensitivity = 0.100;
float offset = 2.5;

unsigned long startTime = 0;
unsigned long hydrogenStart = 0;
unsigned long tempStart = 0;

bool hydrogenActive = false;
bool tempActive = false;

// ================= SETUP =================
void setup() {

Serial.begin(115200);

sensors.begin();

pinMode(START_PIN, OUTPUT);
pinMode(STOP_PIN, OUTPUT);

pinMode(TEMP_ALARM_PIN, OUTPUT);
pinMode(H2_ALARM_PIN, OUTPUT);
pinMode(TIMER_PIN, OUTPUT);
pinMode(SHUTDOWN_PIN, OUTPUT);

digitalWrite(START_PIN, LOW);
digitalWrite(STOP_PIN, LOW);

digitalWrite(TEMP_ALARM_PIN, LOW);
digitalWrite(H2_ALARM_PIN, LOW);
digitalWrite(SHUTDOWN_PIN, LOW);

// D6 يبدأ HIGH
digitalWrite(TIMER_PIN, HIGH);

startTime = millis();

// ===== MQ8 Calibration =====
float sum = 0;

for (int i = 0; i < 20; i++) {
sum += analogRead(MQ8_PIN);
delay(50);
}

float v = (sum / 20.0) * (5.0 / 1023.0);

Ro = RL * (5.0 - v) / v / 9.8;
}

// ================= LOOP =================
void loop() {

// ================= SERIAL COMMANDS =================
if (Serial.available()) {

String cmd = Serial.readStringUntil('\n');
cmd.trim();

if (cmd == "START") {

digitalWrite(START_PIN, HIGH);
delay(1000);
digitalWrite(START_PIN, LOW);
}

else if (cmd == "STOP") {

digitalWrite(STOP_PIN, HIGH);
delay(1000);
digitalWrite(STOP_PIN, LOW);
}

else if (cmd == "RESET") {

hydrogenActive = false;
tempActive = false;

digitalWrite(TEMP_ALARM_PIN, LOW);
digitalWrite(H2_ALARM_PIN, LOW);
digitalWrite(SHUTDOWN_PIN, LOW);

digitalWrite(TIMER_PIN, HIGH);

startTime = millis();
hydrogenStart = 0;
tempStart = 0;
}
}

// ================= TEMPERATURE =================
sensors.requestTemperatures();

float temp = sensors.getTempCByIndex(0);

if (temp >= 70) {

digitalWrite(TEMP_ALARM_PIN, HIGH);

if (!tempActive) {

tempActive = true;
tempStart = millis();
}

if (millis() - tempStart >= 180000) {

digitalWrite(SHUTDOWN_PIN, HIGH);
}

} else {

tempActive = false;
digitalWrite(TEMP_ALARM_PIN, LOW);
}

// ================= HYDROGEN =================
int val = analogRead(MQ8_PIN);

float v = val * (5.0 / 1023.0);

float rs = RL * (5.0 - v) / v;

float ppm = pow(rs / Ro, -1.8);

filtered_ppm = 0.7 * filtered_ppm + 0.3 * ppm;

if (filtered_ppm >= 0.5) {

digitalWrite(H2_ALARM_PIN, HIGH);

if (!hydrogenActive) {

hydrogenActive = true;
hydrogenStart = millis();
}

if (millis() - hydrogenStart >= 180000) {

digitalWrite(SHUTDOWN_PIN, HIGH);
}

} else {

hydrogenActive = false;
digitalWrite(H2_ALARM_PIN, LOW);
}

// ================= VOLTAGE =================
int valueV = analogRead(VoltageSensor);

float vOUT = (valueV * 5.0) / 1023.0;

float vIN = vOUT / (R2 / (R1 + R2));

// ================= CURRENT =================
float sumCurrent = 0;

for (int i = 0; i < 50; i++) {

sumCurrent += analogRead(CurrentSensor);
delay(2);
}

float avgValue = sumCurrent / 50.0;

float voltageCurrent = (avgValue * 5.0) / 1023.0;

float current = (voltageCurrent - offset) / sensitivity;

// ================= POWER =================
float power = vIN * current;

// ================= 30 MIN TIMER =================
unsigned long t = millis() - startTime;

if (t >= 1800000) {

digitalWrite(TIMER_PIN, LOW);
}

// ================= SEND DATA TO LABVIEW =================
Serial.print(temp);
Serial.print(",");

Serial.print(filtered_ppm);
Serial.print(",");

Serial.print(vIN);
Serial.print(",");

Serial.print(current);
Serial.print(",");

Serial.println(power);

delay(500);
}

0 Kudos
Message 1 of 3
(60 Views)

For these types of questions, I just push this video: VIWeek 2020/Proper way to communicate over serial

There is a section that specifically calls out an Arduino (ASCII command structure).



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 3
(58 Views)

try chat gpt 

0 Kudos
Message 3 of 3
(49 Views)