S7-200 PLC COS Instruction Returns Wrong Result Radians vs

David Krause13 min read
S7-200SiemensTroubleshooting
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

1. Problem Overview

When programming trigonometric functions in a Siemens SIMATIC S7-200 Micro PLC, the COS (Cosine), SIN (Sine), and TAN (Tangent) instructions often return values that disagree with a hand-held calculator or PC calculator application. In a typical field case, an engineer enters an angle of 0.25 expecting a cosine of approximately 0.9689, but the PLC returns a wildly different number (often a value near 0.9689 - 0.0003 or even a negative number). The mismatch is not a hardware fault, a faulty CPU, or a corrupted firmware image. The discrepancy is caused by an implicit unit assumption built into the instruction: the S7-200 trigonometric instructions always interpret the input value as a real number expressed in radians, not degrees.

This article documents the root cause, the exact conversion constant, the affected instructions, the special-memory error flags, and a verified step-by-step fix that brings the PLC result into agreement with a standard scientific calculator.

Applies to: All S7-200 CPU models - CPU 221, CPU 222, CPU 224, CPU 224XP, CPU 226 - running STEP 7 Micro/WIN V4.0 SP9 or earlier. The behavior is documented in the official S7-200 Programmable Controller System Manual, available from Siemens Industry Online Support (Manual order number 1109582).

2. Root Cause: The Input Is Always Interpreted as Radians

The COS instruction is defined in the S7-200 instruction set reference as follows:

The Cosine (COS) instruction evaluates the trigonometric function of the angle value IN and places the result in OUT. The input angle value is in radians.

This is a fixed behavior of the firmware. There is no configuration switch, no status bit, and no program-block option that will make the instruction accept an angle expressed in degrees. If the application logic, operator panel, or HMI is presenting the angle in degrees (a very common convention in mechanical, hydraulic, and positioning work), the program must explicitly convert the degrees value to radians before passing it to the COS, SIN, or TAN block.

The fundamental relationship is:

  • 2 × π radians = 360 degrees
  • 1 radian ≈ 57.2958 degrees
  • 1 degree = π / 180 radians ≈ 0.0174532925 radians

An engineer who places the literal 0.25 into the COS input is therefore computing the cosine of 0.25 radians - which is 0.9689 - and the result is, in fact, correct for that interpretation. The mismatch is between the engineer's mental model of the units and the firmware's actual unit.

3. Technical Specification: COS, SIN, TAN Instructions

The S7-200 floating-point math library implements the basic trigonometric set as direct subroutine calls. The block diagram is identical for all three:

Table 1 - Trigonometric Instruction Operands and Data Types
Instruction Mnemonic Input (IN) Output (OUT) Input Unit Output Unit
Cosine COS VD, MD, AC, constant, *VD, *AC VD, MD, AC, *VD, *AC Radians (real) Dimensionless (real, range -1.0 to +1.0)
Sine SIN VD, MD, AC, constant, *VD, *AC VD, MD, AC, *VD, *AC Radians (real) Dimensionless (real, range -1.0 to +1.0)
Tangent TAN VD, MD, AC, constant, *VD, *AC VD, MD, AC, *VD, *AC Radians (real) Dimensionless (real)

All three instructions operate on IEEE-754 32-bit single-precision real numbers. The real-number representation requires that the input variable reside in a Variable-memory Double-word (VD) location, an Accumulator (AC0-AC3), or an indirect pointer that resolves to a VD location. A 16-bit Integer (VW) cannot be used directly as the input without first converting it to a real.

4. Step-by-Step Resolution

The fix is to scale the degree value by the radians-per-degree factor immediately before calling the COS instruction. The complete, verified sequence is shown below.

4.1 Prerequisites

  • STEP 7 Micro/WIN V4.0 SP9 (or any prior service pack) installed and communicating with the S7-200 via USB-PPI or RS-232 PPI cable.
  • Project compiled for the target CPU (CPU 221 / 222 / 224 / 224XP / 226).
  • At least four bytes of free Variable memory (VD) for the input, the converted radians, the PI constant, and the output.
  • Familiarity with the Ladder (LAD), Statement List (STL), or Function Block Diagram (FBD) editor in Micro/WIN.

4.2 Network 1 - Load the Conversion Constant

The radian-per-degree multiplier is 1.745329E-2 (the binary representation of π / 180 rounded to single-precision float). Move this constant into a dedicated VD location, for example VD100, at the start of the program or in the first scan flag (SM0.1) routine.

|     SM0.1       MOVR   1.745329E-2,   VD100
|---(P)---------/-------/---------------/
 First scan       Real constant    VD100 holds the
 pulse            to real          deg→rad factor

4.3 Network 2 - Convert the Operator's Degree Value to Radians

Assume the operator-entered degree value is in VD200 (for example, a value typed on a TD200 text display, a SmartLine panel, or written by an analog-input scaling block). Multiply it by the conversion constant and store the result in VD204.

|     SM0.0       *R     VD200,   VD100,   VD204
|---( )----------/-------/--------/--------/
 Always on        Real multiplication  VD204 now holds
                  deg × (π/180)         the angle in radians
Operator order: The S7-200 *R (Multiply Real) block has the operand order IN1 × IN2 = OUT. Placing the user degrees in IN1 and the radian-per-degree constant in IN2 produces the desired result. Reversing the operands inverts the meaning and is a common commissioning mistake.

4.4 Network 3 - Evaluate the Cosine of the Converted Angle

Pass VD204 to the COS instruction and read the result in VD208.

|     SM0.0       COS    VD204,        VD208
|---( )----------/-------/-------------/
 Always on        Real angle in      Cosine result,
                  radians (IN)        dimensionless (OUT)

4.5 Verification

Load the program and force a known degree value into VD200 using the Micro/WIN Status Chart. The following table summarizes the expected COS output for several reference angles:

Table 2 - Reference Verification: Degrees → Radians → Cosine
Operator (degrees) VD200 (deg) VD204 (rad) expected VD208 (cos) expected Calculator match
0.25 0.25 0.0043633231 0.9999904814 Yes
30 30.0 0.5235987756 0.8660254038 Yes
45 45.0 0.7853981634 0.7071067812 Yes
60 60.0 1.0471975512 0.5000000000 Yes
90 90.0 1.5707963268 0.0000000000 Yes
120 120.0 2.0943951024 -0.5000000000 Yes
180 180.0 3.1415926536 -1.0000000000 Yes
270 270.0 4.7123889804 0.0000000000 Yes
360 360.0 6.2831853072 1.0000000000 Yes

If the values displayed in the Status Chart for VD208 match the "Calculator match" column to within the 7-8 significant digits of a single-precision float, the conversion is working correctly.

5. Alternative: Using the Built-In PI Value

Engineers who prefer an explicit π / 180 form can store the value of PI in a double-word real and divide by 180. The S7-200 does not have a PI constant instruction; the value must be entered as a literal real. The canonical single-precision value of π is 3.1415927 (or, written in STEP 7 Micro/WIN scientific notation, 3.141593E+0). Loading PI and dividing is shown below.

Network 1: Load PI
|     SM0.1       MOVR   3.141593E+0,   VD300
|---(P)----------/-------/---------------/
 First scan       Real constant PI    PI value stored
Network 2: Compute PI / 180 (one-time)
|     SM0.1       /R     VD300,   180.0,   VD304
|---(P)----------/-------/--------/--------/
 First scan       PI / 180            Stored in VD304
Network 3: Multiply user degrees by PI/180 every scan
|     SM0.0       *R     VD200,   VD304,   VD308
|---( )----------/-------/--------/--------/
 Always on        Real multiply       Radians result
Network 4: COS of converted angle
|     SM0.0       COS    VD308,        VD312
|---( )----------/-------/-------------/
 Always on        Radians in         Cosine out
Operand-order trap on /R: The S7-200 real-division block /R IN1, IN2, OUT performs IN1 / IN2 = OUT. The operands must be PI in IN1 and 180.0 in IN2 to obtain π / 180. Reversing them (180 / π) yields 57.2958 (the degrees-per-radian constant) and silently produces wrong angle values - one of the most common commissioning errors in trig math on the S7-200.

The two formulations are mathematically identical. The first (multiplying by 1.745329E-2) is faster (one multiplication per scan) and saves two network executions; the second (PI / 180) is more self-documenting at the cost of two extra first-scan operations.

6. Special-Memory Error Flags for COS, SIN, TAN

The COS, SIN, and TAN instructions set the standard floating-point status bits in Special Memory. These bits should be monitored in any production code that processes operator-entered angles, because input values that overflow the real-number range or that are not valid real numbers will leave the output undefined.

Table 3 - SM Bits Set by the Floating-Point Math Library
SM Bit Name Meaning
SM1.0 Zero result Set when the operation produced a result of exactly 0.0 (a valid condition for COS at 90°, 270°, etc.)
SM1.1 Overflow / illegal value Set when the result is outside the valid real-number range, the operand is not a legal real, or a NaN/Inf condition exists. When this bit is set, SM1.0 and SM1.2 are NOT valid and the original input operand is not altered.
SM1.2 Negative result Set when the operation produced a negative result (a valid condition for angles in the 90°-270° range).

The ENO (Enable Out) output of the COS block is forced to 0 under the following conditions (per the S7-200 System Manual, Section 6 - Instruction Set):

Table 4 - ENO = 0 Conditions for the COS Instruction
Error Code Trigger
0006 (decimal 6) Indirect address error - the pointer or the target address is invalid
SM1.1 set Overflow or illegal-value condition on the real input

Recommended watchdog logic for a robust COS routine:

|     SM0.0       COS    VD204,   VD208
|---( )----------/-------/--------/
 Always on        Radians in        Cosine out
|
|                MOVR   0.0,   VD208     // Clear the result
|     SM1.1------( )---------/--------/   // if overflow occurred
|---( )----------/-------/--------/
 Overflow         Force output to zero
                  and protect downstream math

7. The Inverse Functions: ASIN, ACOS, ATAN

The inverse trigonometric instructions follow the inverse unit convention - they return angles in radians. If a value is displayed to an operator in degrees, it must be multiplied by 57.29578 (180 / π) to be meaningful. This is a frequent source of bug reports, especially in motion-control and curve-fitting applications where the PLC performs both forward and inverse trig.

Table 5 - Inverse Trig Instructions: Output Units and Conversions
Instruction Input Range Output Unit Conversion to Degrees
ASIN (arc-sine) -1.0 to +1.0 Radians, range -π/2 to +π/2 Multiply by 57.29578
ACOS (arc-cosine) -1.0 to +1.0 Radians, range 0 to +π Multiply by 57.29578
ATAN (arc-tangent) Any real Radians, range -π/2 to +π/2 Multiply by 57.29578
Quadrant correction: The ATAN instruction only returns values in the first and fourth quadrants (-90° to +90°). For two-quadrant input from encoder index pulses or resolver-derived sin/cos pairs, the application code must apply a sign correction based on the sign of the input cosine to recover the true angle. This is unrelated to the radian/degree unit issue but is the next-most-common trigonometric bug on the S7-200.

8. Common Pitfalls and Edge Cases

8.1 Integer-to-Real Conversion Omitted

Sub-blocks such as the TD200 text display, an integer scaling calculation, or a counter value all produce 16-bit integers. Passing a VW (word) directly to the COS input is a common mistake - the instruction will read the bits as a real-number bit pattern and return a meaningless value. Always use MOVR or ITR (Integer-to-Real) to load a 32-bit real into the COS input location first.

|     SM0.0       ITR    VW0,    VD200
|---( )----------/-------/--------/
 Always on        16-bit int         32-bit real
                  to 32-bit real      ready for COS

8.2 Indirect-Pointer Misuse

When a loop processes an array of angles stored in consecutive V-memory double-words, the COS instruction is called repeatedly with an indirect pointer. The pointer must address a double-word boundary (VD0, VD4, VD8, ...) and must be incremented by 4 (or by a real-pointer index of 2) after each iteration. Forgetting to advance the pointer by 4 bytes causes the same memory location to be evaluated each scan, producing a stuck output that looks like a "wrong answer" but is actually a sequencing bug.

8.3 Sign and Overflow at Large Angles

COS is periodic in 2π, so reducing an angle modulo 6.2831853 before passing it to the instruction is technically unnecessary - the firmware handles wrap-around correctly. However, values greater than approximately 1.0E+7 radians may cause precision loss in single-precision float arithmetic. For angles derived from high-resolution encoder counts (for example, 16-bit counters multiplied by a per-pulse radian increment), keep the pre-multiplication within the range that yields a real-number input of less than 1.0E+5 radians to maintain 24-bit mantissa precision.

8.4 TAN Near π/2

The tangent function approaches ±∞ as the input approaches ±π/2. The S7-200 returns ±3.402823E+38 (the IEEE-754 single-precision max) and sets SM1.1 (overflow). For an angle of 89.999° (1.5707932 rad), the TAN output is approximately 57,289.96 - within the legal range. For 89.9999° (1.5707961 rad), the output is approximately 572,957.0 - still within the legal range. Beyond that, expect overflow. Add a clamping or pre-reduction routine if the input is known to dwell near the asymptote.

8.5 Using the COS Output in Integer Comparisons

The output of COS is a 32-bit real. If the application needs to compare the cosine against a threshold such as 0.5 (corresponding to 60°), the comparison must be made with a real-comparison instruction (for example, the >=R block), not an integer comparison. Using an integer compare on the high word of the real will read the IEEE-754 exponent bits and produce nonsensical results.

9. Quick-Reference Constant Table

Table 6 - Common Trig Constants for the S7-200
Symbol Real Value Micro/WIN Notation Use
π / 180 0.0174532925 1.745329E-2 Multiply degrees to get radians
180 / π 57.29577951 5.729578E+1 Multiply radians to get degrees
π 3.141592654 3.141593E+0 Generic π constant
6.283185307 6.283185E+0 One full revolution in radians
π / 2 1.570796327 1.570796E+0 Quadrant boundary

10. S7-200 Programmable Controller System Manual Reference

The complete S7-200 instruction set, including the formal specification of the COS, SIN, TAN, ASIN, ACOS, and ATAN blocks, the special-memory error-flag definitions, and the indirect-addressing rules, is documented in the official Siemens S7-200 Programmable Controller System Manual. The manual is available as a free PDF download from Siemens Industry Online Support (file 1109582, version 1, English US). Section 6 of the manual - Instruction Set - is the authoritative source for the operand restrictions, the ENO behavior, and the SM1.0/SM1.1/SM1.2 flag definitions quoted in this article.

11. Related Documentation

  • S7-200 Programmable Controller System Manual (Siemens, English US, order number 1109582) - Section 6.7 covers the floating-point math library including COS, SIN, TAN, ASIN, ACOS, ATAN, and the natural-logarithm / exponential functions.
  • STEP 7 Micro/WIN V4.0 SP9 Help File - integrated reference accessible from within the programming environment; the Help text for the COS block includes the explicit "input angle value is in radians" sentence and the SM1.1 overflow behavior described in this article.

FAQ

Why does the S7-200 COS instruction return a different value than my calculator?

The COS instruction always interprets its input in radians. A calculator or HMI may be presenting the angle in degrees. Multiply the degree value by 1.745329E-2 (π / 180) before passing it to the COS block, and the output will match the calculator to single-precision float precision.

What is the exact value of the degrees-to-radians multiplier on the S7-200?

Use 1.745329E-2, which is the single-precision IEEE-754 representation of π / 180. Loading this constant once into a VD location at first scan (SM0.1) and multiplying it by the operator's degree value each cycle is the standard, fastest implementation.

How do I monitor COS overflow and illegal-value errors?

Check SM1.1 immediately after the COS block executes. If SM1.1 is set, the operation overflowed or received an illegal real-number input, the output is not valid, and the original input operand has not been altered. SM1.0 and SM1.2 are not valid while SM1.1 is set. Clear the output to 0.0 with a MOVR instruction gated on SM1.1 to protect downstream math.

Does the S7-200 have a built-in PI constant instruction?

No. The constant π must be entered as a real-number literal (3.141593E+0 or 3.1415927). For best readability, store it in a dedicated VD on first scan, then compute and store π / 180 once. Avoid recomputing π or π / 180 in the fast scan loop - the constants only need to be loaded once.

What CPU models and STEP 7 Micro/WIN versions are affected?

The radian-input behavior is firmware-defined and is consistent across every S7-200 CPU - CPU 221, CPU 222, CPU 224, CPU 224XP, and CPU 226 - and across every STEP 7 Micro/WIN version from V3.1 through V4.0 SP9. The fix is the same on all variants: pre-multiply the operator's degree value by 1.745329E-2 before calling COS, SIN, or TAN.

Back to blog