Introduction to ST Language Programming for Huichuan PLC: Learn Programming from Scratch

Click the blue text

PLC Automation Hub

Follow us

Master ST language to make your PLC programming more efficient and intuitive

What is ST Language?

ST (Structured Text) is a high-level PLC programming language that resembles Pascal or C language, adopting a structured programming approach. Compared to Ladder Diagram, ST language is more convenient and efficient for handling complex calculations, loop control, and data structures.

Huichuan PLC fully supports the ST language defined in the IEC 61131-3 standard, providing engineers with powerful programming tools.

Basic Syntax of ST Language

1. Variable Declaration (Using Chinese Variable Names)

Before programming, we need to declare variables. Using Chinese variable names makes the program more readable:

VAR

Counter: INT; // Integer variable

CurrentTemperature: REAL; // Real variable

MotorStatus: BOOL; // Boolean variable

Message: STRING; // String variable

StartButton: BOOL; // Input signal

StopButton: BOOL; // Input signal

END_VAR

2. Assignment Statements

Counter := 100; // Integer assignment

CurrentTemperature := 25.6; // Real assignment

MotorStatus := TRUE; // Boolean assignment

Message := ‘System is ready!’; // String assignment

3. Basic Operators

  • Arithmetic Operators:<span><span>+</span></span>, <span><span>-</span></span>, <span><span>*</span></span>, <span><span>/</span></span>

  • Comparison Operators:<span><span>></span></span>, <span><span><</span></span>, <span><span>=</span></span>, <span><span>>=</span></span>, <span><span><=</span></span>, <span><span><></span></span>

  • Logical Operators:<span><span>AND</span></span>, <span><span>OR</span></span>, <span><span>NOT</span></span>, <span><span>XOR</span></span>

First ST Program: Motor Control

Let’s learn ST programming through a simple motor control example.

PROGRAM MainProgram

VAR

StartButton: BOOL; // Start button input

StopButton: BOOL; // Stop button input

MotorRunning: BOOL; // Motor running status

RunTime: INT; // Runtime counter

FaultSignal: BOOL; // Fault detection signal

END_VAR

// Main program logic – Motor start/stop control

IF StartButton AND NOT StopButton AND NOT FaultSignal THEN

MotorRunning := TRUE;

ELSIF StopButton OR FaultSignal THEN

MotorRunning := FALSE;

END_IF;

// Runtime statistics

IF MotorRunning THEN

RunTime := RunTime + 1;

END_IF;

// Fault detection logic

IF RunTime > 10000 THEN

FaultSignal := TRUE;

Message := ‘Motor running timeout, please check!’;

END_IF;

Advanced Example: Temperature PID Control

Next, we will look at a more complex example – a temperature PID control system.

PROGRAM TemperatureControlProgramVAR    SetTemperature: REAL := 100.0;  // Set temperature value    ActualTemperature: REAL;           // Actual detected temperature    HeaterPower: REAL;         // Heater output power    TemperatureError: REAL;           // Current temperature error    LastError: REAL;           // Last temperature error    IntegralTerm: REAL;             // PID integral term    ProportionalGain: REAL := 0.8;    // P parameter    IntegralGain: REAL := 0.01;   // I parameter    DerivativeGain: REAL := 0.1;    // D parameter    SystemStatus: STRING;         // System status descriptionEND_VAR
// PID control algorithmTemperatureError := SetTemperature - ActualTemperature;
// Integral term calculation (with anti-windup protection)IF ABS(IntegralTerm) < 1000 THEN    IntegralTerm := IntegralTerm + TemperatureError;END_IF;
// PID output calculationHeaterPower := ProportionalGain * TemperatureError +               IntegralGain * IntegralTerm +               DerivativeGain * (TemperatureError - LastError);
// Limit output range to 0-100%IF HeaterPower > 100.0 THEN    HeaterPower := 100.0;ELSIF HeaterPower < 0.0 THEN    HeaterPower := 0.0;END_IF;
// Update last errorLastError := TemperatureError;
// System status monitoringIF TemperatureError < 5.0 AND TemperatureError > -5.0 THEN    SystemStatus := 'Temperature Stable';ELSE    SystemStatus := 'Temperature Adjusting';END_IF;

Timer Application Example

PROGRAM TimerControlProgramVAR    StartSignal: BOOL;          // Start signal    DelayTimer: TON;         // On-delay timer    RunIndicator: BOOL;        // Run indicator light    TimerDuration: TIME := T#5S;  // Set delay timeEND_VAR
// Timer applicationDelayTimer(IN := StartSignal, PT := TimerDuration);
// Output controlRunIndicator := DelayTimer.Q;
// Timer status monitoringIF DelayTimer.ET > T#3S THEN    Message := 'Timer is about to complete';END_IF;

Array and Loop Application Example

PROGRAM DataCollectionProgramVAR    TemperatureArray: ARRAY[1..10] OF REAL;  // Temperature data array    AverageTemperature: REAL;                  // Average temperature value    LoopIndex: INT;                   // Loop counter    TotalTemperature: REAL;                  // Total temperatureEND_VAR
// Calculate average temperatureTotalTemperature := 0.0;FOR LoopIndex := 1 TO 10 DO    TotalTemperature := TotalTemperature + TemperatureArray[LoopIndex];END_FOR;AverageTemperature := TotalTemperature / 10.0;
// Temperature anomaly detectionIF AverageTemperature > 80.0 THEN    Message := 'High temperature warning!';ELSIF AverageTemperature < 20.0 THEN    Message := 'Low temperature warning!';ELSE    Message := 'Temperature normal';END_IF;

Advantages of ST Language

  1. Strong Mathematical Calculation Capability: Suitable for complex mathematical calculations and algorithm implementations

  2. Clear Code Structure: Facilitates writing and maintaining large programs

  3. High Execution Efficiency: Fast speed when processing large amounts of data

  4. Easy Portability: Code can be migrated between different brands of PLCs

  5. Good Readability: Using Chinese variable names aligns with the reading habits of Chinese engineers

Learning Suggestions

  1. Start Simple: First master the basic syntax and common instructions

  2. Practice More: Deepen understanding through practical projects

  3. Reference Manuals: Always refer to the Huichuan PLC programming manual

  4. Naming Conventions: Use meaningful Chinese variable names to improve code readability

  5. Complete Comments: Add detailed comments for complex logic

Conclusion

As an important tool for PLC programming, ST language plays a significant role in Huichuan PLC. By using Chinese variable names, the readability and maintainability of the program can be greatly improved. In practical applications, flexibly utilizing various features of ST language according to specific project requirements will greatly enhance programming efficiency and quality.

Master ST language and embark on an efficient PLC programming journey!

Welcome to follow our public account for more PLC programming tips and industrial automation knowledge!

Recommended Reading:

  1. Use all your strength! Nine steps to ensure stable operation of Siemens PLC
  2. Siemens S7-1500 PLC Troubleshooting: Transform into an industrial “doctor” to quickly “cure” production line downtime!
  3. [10-Year Siemens PLC Veteran Upgrade Path] From screwing modules to mastering communication architecture: My blood and tears technical stack guide
  4. Three “weapons” of servo motors: Detailed explanation of position/velocity/torque control (with practical Siemens SCL code)
  5. Core secrets of Siemens PLC programming: What are FB, FC, DB, OB? You will understand after reading this!
  6. Why can’t PLC engineers with a monthly salary of 20,000 stay? The “career burnout” behind high salaries is consuming this industry!
  7. Siemens PLC Programming: Ladder Diagram vs SCL, which one is for you? A comprehensive beginner’s guide + practical code!
  8. Say goodbye to “spaghetti code”! Siemens PLC step programming “three-step method”, double efficiency without pitfalls!
  9. Siemens SCL communication heartbeat monitoring: Practical guide for industrial-grade heartbeat programs
  10. Siemens 1200 and Weintek tag communication: Say goodbye to manual address filling, this “smart translator” makes debugging three times faster!
  11. Siemens SCL Practical: Step-by-step guide to writing station control function blocks
  12. Siemens S7-1200 Mixed Programming Practical: The golden combination rules of SCL and LAD
  13. Electrical automation encirclement: Ten years of hard work for this heartfelt industry experience summary
  14. Siemens SCL workstation control program: Start/stop control + three-color light + buzzer alarm
  15. Mitsubishi FX3U PLC dual pump constant pressure water supply explained: Frequency + power frequency intelligent switching + safety emergency stop system
  16. Electrical automation major graduation guide:
  17. Employment direction and salary prospects fully analyzed
  18. Electrical automation college students: Starting salary not inferior to undergraduates? Full disclosure of the technical counterattack roadmap!
  19. Siemens S7-200 SMART free port communication explained: Introduction to flexible serial communication solutions
  20. Ultimate solution for Siemens SCL full-function servo control: 8 motion modes + 5 levels of safety protection
  21. Let data speak! A scientific guide for choosing majors for science vs. liberal arts students (with employment salary & trend analysis)

Share to let more people see

Introduction to ST Language Programming for Huichuan PLC: Learn Programming from Scratch

Like

Collect

Share

Leave a Comment