LabVIEW DC Motor Speed Control with PID and NI DAQ

In many automated production lines, precise speed regulation of DC motors is critical for conveyor rollers and material handling. This article presents a complete closed-loop control solution using LabVIEW, an NI data acquisition card, and a PID algorithm to achieve ±5 rpm accuracy with a response time under 200 ms.

System Hardware Architecture

The hardware setup revolves around a 24V DC motor (RS-555 type) with an integrated incremental encoder (1000 pulses per revolution). The motor is driven by an L298N dual H-bridge driver, which supports PWM speed control and direction switching. For data acquisition and control signal generation, the NI USB-6009 is used—a versatile, low-cost DAQ with 8 analog inputs (14-bit, 48 kS/s), 2 analog outputs (12-bit, 150 S/s), and 12 digital I/O lines. A 24V/5A switching power supply feeds the motor and driver, while a separate 5V supply powers the encoder.

Key Hardware Connections

  • Encoder A/B signals → NI USB-6009 digital inputs (PFI0/PFI1) for pulse counting
  • LabVIEW PWM output → NI USB-6009 analog output (AO0) → L298N enable pin
  • Direction control → NI USB-6009 digital outputs (P0.0/P0.1) → L298N IN1/IN2
  • Motor armature → L298N output terminals; 0.1Ω shunt resistor in series for overcurrent sensing

LabVIEW Software Implementation

Speed Detection Module

The speed measurement relies on the pulse counting method. LabVIEW’s DAQmx Counter Input functions are configured to count rising edges of encoder phase A. A sampling period of 100 ms is chosen to balance update rate and accuracy. The speed (RPM) is calculated as:

n = (N × 60) / (P × T)

where N is the pulse count in the sampling window, P is encoder resolution (1000 PPR), and T is the sampling period (0.1 s). A moving average filter (window size 5) is applied to suppress jitter, and phase comparison between A and B channels compensates for counting errors during direction changes.

PID Speed Control Module

The core of the closed-loop system is a PID controller implemented with LabVIEW’s PID toolkit. The default gains are Kp = 0.8, Ki = 0.1, Kd = 0.05, but these can be tuned online via the user interface. The PID output (0–5 V) is sent to the analog output channel, which the L298N interprets as a PWM duty cycle (0–100%) at a fixed frequency of 1 kHz. This adjusts the motor’s armature voltage to maintain the target speed.

Parameter Value Description
Kp 0.8 Proportional gain – reduces rise time
Ki 0.1 Integral gain – eliminates steady-state error
Kd 0.05 Derivative gain – reduces overshoot
PWM Frequency 1 kHz Fixed carrier frequency for L298N
Sampling Period 100 ms Control loop update rate

Protection Logic

Safety features are implemented using LabVIEW’s case structures:

  • Overcurrent protection: If the voltage across the 0.1Ω shunt resistor exceeds 0.5 V (i.e., 5 A), the PWM output is immediately set to zero.
  • Overspeed/underspeed protection: If the measured speed goes above 3500 RPM or below 500 RPM, the system triggers an alarm and stops the motor.
  • Alarm states are indicated via digital output lines connected to LEDs on the control panel.

Human-Machine Interface (HMI)

The LabVIEW front panel provides an intuitive operator experience:

Real-Time Displays

Current speed (RPM), setpoint, and PWM duty cycle are shown on large numeric indicators.

Controls

Speed setpoint entry, Start/Stop buttons, and PID gain sliders allow on-the-fly adjustments.

Data Logging

A waveform chart plots historical speed data (100 ms update), and data can be exported to Excel.

Testing and Performance

The system was validated through static and dynamic tests:

Test Type Setpoint (RPM) Measured Error Response Time
Static 1000, 2000, 3000 ≤ ±3 RPM N/A
Dynamic (step) 500 → 3000 No overshoot 180 ms

Optimization steps included fine-tuning PID gains, adding encoder signal isolation to reduce noise, and adjusting the sampling period to balance responsiveness and stability. The final system meets the ±5 RPM accuracy requirement and responds within 200 ms, making it suitable for demanding conveyor applications.

Key Takeaway: This LabVIEW-based DC motor control system demonstrates how off-the-shelf hardware and graphical programming can deliver high-precision speed regulation. The modular design allows easy adaptation to different motors and control requirements, while the built-in protection features enhance reliability in industrial environments.

Similar Posts