Comprehensive PLC Programming Algorithms Summarized by Senior Electrical Engineers - Keep for Future Reference!


 In PLC, there are essentially three major types of data: switching quantity, analog quantity, and pulse quantity. Understanding the relationship between these three types will enable you to master PLC proficiency.

  1. Switching quantity, also known as logic quantity, refers to two values: 0 or 1, ON or OFF. It is the most commonly used control, and PLC's advantage lies in controlling it, which is also PLC's most basic application.

The purpose of switching quantity control is to enable PLC to generate corresponding switching quantity outputs based on the current input combination of switching quantities and the historical input sequence, so that the system can work in a certain order. Therefore, it is sometimes also referred to as sequential control.

Sequential control can be divided into manual, semi-automatic, or automatic. The control principles adopted include decentralized, centralized, and hybrid control methods.

  1. Analog quantity refers to some continuously changing physical quantities, such as voltage, current, pressure, speed, flow rate, etc.

PLC evolved from relay control with the introduction of microprocessing technology, making it convenient and reliable to utilize switching quantity control. Since analog quantity can be converted into digital quantity, which is just a multi-bit switching quantity, PLC can also reliably handle and control converted analog quantity.

Since continuous production processes often involve analog quantity, analog quantity control is sometimes also referred to as process control.

If the measurement is not standard, it must be converted into a standard electrical signal, such as 4-20mA, 1-5V, 0-10V, etc.

At the same time, there must be analog input units (A/D) to convert these standard electrical signals into digital signals; analog output units (D/A) to convert the digital quantity processed by PLC back into analog quantity - standard electrical signals.

The conversion between standard electrical signals and digital quantities requires various calculations. This requires understanding the resolution of analog units and standard electrical signals.

For example:

If the resolution of the PLC analog unit is 1/32767, corresponding to a standard electrical quantity of 0-10V, and the temperature to be detected ranges from 0-100℃. Then 0-32767 corresponds to a temperature value of 0-100℃. Then, the digital quantity corresponding to 1℃ is calculated as 327.67. If you want to make the temperature value accurate to 0.1℃, divide 327.67 by 10.

These are the calculation processes of digital quantities within PLC. Analog quantity control includes feedback control, feedforward control, proportional control, fuzzy control, etc.

  1. Pulse quantity is a digital quantity whose value constantly alternates between 0 (continuous) and 1 (peak value). The number of times the pulse alternates per second is called the frequency.

The purpose of PLC pulse quantity control is primarily position control, motion control, trajectory control, etc. For example, the application of pulse count in angle control. If the stepper motor driver has a resolution of 10000 steps per revolution, and it is required to rotate the stepper motor 90 degrees. Then the required pulse value = 10000 / (360/90) = 2500.

PLC Programming Algorithm (2) - Calculation of Analog Quantity

I. For voltages ranging from -10V to 10V, they are converted to F448—0BB8Hex (-3000—3000) at a resolution of 6000, and E890—1770Hex (-6000—6000) at a resolution of 12000.

II. For voltages ranging from 0 to 10V, they are converted to 0—1770Hex (0—6000) at a resolution of 12000, and 0—2EE0Hex (0—12000) also at a resolution of 12000.

III. For currents ranging from 0 to 20mA, they are converted to 0—1770Hex (0—6000) at a resolution of 6000, and 0—2EE0Hex (0—12000) at a resolution of 12000.

IV. For currents ranging from 4 to 20mA, they are converted to 0—1770Hex (0—6000) at a resolution of 6000, and 0—2EE0Hex (0—12000) at a resolution of 12000.

These are just brief introductions. Different PLCs have different resolutions, and the implemented scales for the measured physical quantities may vary. Calculation results may have some differences.

Note on Wiring Requirements for Analog Inputs:

  1. Use shielded twisted-pair cables but do not connect the shield layer.
  2. When an input is not in use, short-circuit the VIN and COM terminals.
  3. Isolate analog signal lines from power lines (AC power lines, high-voltage lines, etc.).
  4. When there is interference on the power line, install a filter between the input section and the power unit.
  5. After confirming the correct wiring, first power on the CPU unit, then power on the load.
  6. When shutting down, first cut off the power to the load, then cut off the power to the CPU.

PLC Programming Algorithm (3) - Calculation of Pulse Quantity

The control of pulse quantity is often used for angle control, distance control, position control, etc., of stepper motors and servo motors. The following explanations use stepper motors as an example to illustrate various control methods.

  1. Angle Control of Stepper Motor: First, determine the subdivision of the stepper motor and then the total number of pulses required for one revolution of the stepper motor. Calculate the "angle percentage = set angle / 360° (i.e., one revolution)" and "angle action pulse count = total pulses per revolution * angle percentage."

The formula is: Angle action pulse count = Total pulses per revolution * (Set angle / 360°).

  1. Distance Control of Stepper Motor: First, determine the total number of pulses required for one revolution of the stepper motor. Then, determine the diameter of the stepper motor's roller and calculate its circumference. Calculate the distance traveled per pulse. Finally, calculate the number of pulses required to travel the set distance.

The formula is: Set distance pulse count = Set distance / [(Roller diameter * 3.14) / Total pulses per revolution].

  1. Position Control of Stepper Motor is a combination of angle control and distance control.

The above analysis of stepper motor control methods is just a brief overview and may differ from actual practice. It serves as a reference for colleagues.

The movement of servo motors is similar to stepper motors, but the internal electronic gear ratio of the servo motor and its reduction ratio should be considered.

Like my work? Consider a donation! 👇