IMDHMIControls LibraryShop main store

How-to

Program startup, flasher and digital-use analog inputs on an M172

PLC programming Published 8 min read

Short answer

Declare a BOOL, set it TRUE in an ST program assigned to the Init Task, compute the flasher as x1Sec := TO_BOOL((sysTimer/1000) MOD 2) in cyclic code, and convert each of AI5-AI8 with TO_BOOL before ladder accepts it as a contact. The object not found error means the example names were never declared.

On this page
  1. How do you make a contact that turns on when the M172 starts?
  2. Why does DOstart give an object not found error?
  3. How do you reference DOstart and x1Sec in the rest of the program?
  4. How do you build a one-second flasher from sysTimer?
  5. Which task should run the flasher and the AI conversions?
  6. Why does ladder say only BOOL inputs can be used on AI5?
  7. How do you convert AI5 to AI8 into BOOL contacts?
  8. What proves all three pieces work at commissioning?
  9. Questions asked

Build all three pieces the same way: declare your own variables, run them from the right task, and hand ladder finished BOOL values. On the M172 in EcoStruxure Machine Expert - HVAC, the always-on contact is one line, DOstart := TRUE;, in a program assigned to the Init Task. The flasher is one line of cyclic code, x1Sec := TO_BOOL((sysTimer/1000) MOD 2);, holding each state for one second. The analog inputs used as digital contacts need TO_BOOL as well, because the 1=DI BIOS mode does not retype the application symbol. And the object not found error has one cause: DOstart and x1Sec are example names, and neither exists until you declare it.

How do you make a contact that turns on when the M172 starts? #

Declare a BOOL in the Configuration Perspective, create one small ST program, assign that program to the Init Task, and put a single line in it, DOstart := TRUE; — the bit reads TRUE from the end of initialization onward.

The Init Task executes during controller startup, so whatever it writes is already in place when the cyclic programs first run. DOstart is an example name for whatever you declare, not a controller object. Use an internal BOOL when ladder simply needs a permanent-on condition; declare the variable as a physical digital output instead when hardware must energize at boot.

  1. In the Configuration Perspective, declare the variable the rest of the application will address.
  2. Create an ST program for initialization.
  3. Assign that program to the Init Task.
  4. Write DOstart := TRUE; using the exact declared name.
  5. Compile until the name resolves with no errors.
  6. Restart the controller and confirm the variable reads TRUE.

If the bit stays FALSE after a Logic Builder download rather than a restart, the initialization may not have re-executed; M172 Init Task not executed on a Logic Builder download works through that case before you touch the code. One caution: a bit that is TRUE from power-up is ordinary logic, not a safety function, so it must never be what keeps a bollard or any other machine from moving on restore.

Why does DOstart give an object not found error? #

Because DOstart and x1Sec are example names, not built-in M172 objects: the compiler raises object not found for any identifier never declared in scope, so the fix is to declare them or substitute your own names.

Separate compile-time symbol errors from runtime behavior before debugging either. A task assignment cannot rescue an undeclared name, and a clean compile does not prove the intended task executed or that nothing overwrites the value later. Once your declared variable replaces the example name in both the declaration and the assignment, the error clears. The declaration, the assignment and every ladder contact must address the same identifier, in a scope the consuming program can see.

How do you reference DOstart and x1Sec in the rest of the program? #

Use the exact names you declared: a contact addressed to DOstart reads the startup bit and one addressed to x1Sec reads the flasher, because xDOStart with a prefix is a different identifier, not a naming style.

Declare both variables in a scope shared with the consuming logic; a declaration local to one program is invisible to a ladder POU elsewhere. The same name then works unchanged in ST, FBD and ladder. If you prefer a prefix convention, declare the variable as xDOstart from the start and write xDOstart := TRUE; in the Init Task program — what matters is that all three places agree.

How do you build a one-second flasher from sysTimer? #

Compute it from the system millisecond counter in cyclic ST — x1Sec := TO_BOOL((sysTimer/1000) MOD 2); — which is FALSE for one second, TRUE for one second, and repeats a full cycle every two seconds.

sysTimer counts controller runtime in milliseconds. Integer division by 1000 collapses it into whole seconds, MOD 2 alternates the quotient between 0 and 1 as each second completes, and TO_BOOL turns that into a ladder-compatible state. Declare x1Sec as a BOOL, put the line where it executes cyclically, and monitor both variables online.

Worked example with the arithmetic:

  • sysTimer = 3,742,517 ms: 3,742,517 / 1000 = 3,742 with integer division dropping the fraction; 3,742 MOD 2 = 0 because 3,742 is even; x1Sec = FALSE.
  • sysTimer = 3,743,600 ms: 3,743,600 / 1000 = 3,743; 3,743 MOD 2 = 1 because 3,743 is odd; x1Sec = TRUE.
sysTimer (ms) sysTimer / 1000 MOD 2 x1Sec
3,742,517 3,742 0 FALSE
3,743,600 3,743 1 TRUE
3,744,250 3,744 0 FALSE

Each state lasts exactly 1000 ms, from one whole-second boundary to the next, which is why the cycle is 2000 ms. This is a free-running indicator whose phase comes from the counter, not from your process. When the flashing must start and restart on a machine event, build it from a timer and counter instead — the pattern behind siren ring count ladder logic with a timer and counter.

Which task should run the flasher and the AI conversions? #

The flasher belongs in the continuously running cyclic task and the AI conversions in the Timed Task — never the Init Task, which runs once at startup, executes each expression a single time, and leaves the results frozen.

An expression updates only when its task executes. A task slower than the state it samples steps over transitions entirely, the same sampling limit that explains why a PLC misses short input pulses. As an illustrative figure, a cyclic scan of 10 ms executes the flasher line 1000 / 10 = 100 times inside each one-second state; choose a Timed Task period of the same order, well under one second, so no field transition is skipped.

Why does ladder say only BOOL inputs can be used on AI5? #

Because the 1=DI BIOS setting configures the channel's electrical mode, not the application symbol's data type: the compiler still holds a numeric value for that input, and a ladder contact accepts only a Boolean operand.

Seeing 1=DI in the BIOS and again in the commissioning tab confirms the channel works as a digital input at the hardware level; neither screen rewrites the type the compiler sees, so the contact still rejects the symbol. The explicit conversion is the missing step, and it does not have to live in the editor that reported the error — FBD handles it as well as ST.

How do you convert AI5 to AI8 into BOOL contacts? #

Declare four BOOLs and convert each channel with TO_BOOL, either as FBD wiring between the input value and its BOOL or as one small ST program assigned to the Timed Task, xAI5asDI := TO_BOOL(iAI5input);, then address ladder contacts only to the converted variables.

  1. Confirm each of AI5 to AI8 reads 1=DI in the BIOS and in the commissioning tab.
  2. Note the configured application symbol of each channel; iAI5input stands in for whatever your configuration actually names it.
  3. Declare xAI5asDI through xAI8asDI as BOOL variables in a shared scope.
  4. Convert either in FBD, with a TO_BOOL block between each channel value and its BOOL, or in a small ST program assigned to the Timed Task.
  5. Map every destination to its own channel's symbol, not the first channel's.
  6. Address ladder contacts to the converted BOOLs only.
  7. Operate each field input alone and confirm only its corresponding BOOL changes.

The example mapping is often copied with iAI5input still on the xAI8asDI line; left that way, all four contacts follow AI5, because every assignment faithfully converts the source you gave it. Computing in ST and consuming in ladder is also the tidy way around language-boundary trouble of the kind that appears when a CODESYS TON works in structured text but not ladder.

What proves all three pieces work at commissioning? #

A clean compile plus four online checks: the startup BOOL is TRUE after a restart, sysTimer advances while x1Sec holds each state for one second, each converted BOOL follows only its own channel, and ladder accepts every contact without a type error.

  1. Build the application with no unresolved-object or type errors.
  2. Restart the controller and confirm the startup BOOL reads TRUE.
  3. Monitor sysTimer and x1Sec together: the counter advances and the BOOL holds each state for one second.
  4. Recheck AI5 to AI8 for 1=DI in the BIOS and the commissioning tab.
  5. Operate each field input separately and confirm exactly one converted BOOL responds.
  6. Confirm the ladder contacts display the same online states as the converted variables.

When you watch a bound controller, the debug session itself can mislead you before the program does; troubleshooting live debug on bound M172 controllers covers that layer.

Symptom Cause Correction
object not found on DOstart or x1Sec Name never declared, or outside the scope Declare it or use the exact existing variable
only BOOL inputs can be used at a contact Operand is the numeric input symbol Apply TO_BOOL and address the destination BOOL
x1Sec never changes state Expression not executing cyclically, or wrong variable monitored Check the task assignment; watch sysTimer beside x1Sec
Every converted contact follows AI5 First channel's symbol copied onto every line Map each destination to its own channel symbol
Startup bit never TRUE Program not on the Init Task, or a later write overwrites it Verify the assignment; search every write to the bit

With those rows clear, the decision holds per case: one line in the Init Task for the startup bit, one expression in a cyclic task for the flasher, and one TO_BOOL conversion per channel in the Timed Task for digital duty on AI5 to AI8.

Questions asked

Is DOstart a built-in M172 system bit I can reference directly?

No. DOstart and x1Sec are example names from the pattern, not controller objects, and the object not found error is the compiler telling you the identifier was never declared in scope. Declare your own BOOL under any name, use that exact name in the Init Task program and at the ladder contact, and the error clears on the next compile.

Does the flasher give a one-second cycle or a one-second state?

The expression gives one second FALSE and one second TRUE, so the complete cycle is two seconds. If you need a full one-second cycle, divide by 500 instead of 1000. The state width always equals the divisor in milliseconds and the cycle is twice that, which is the whole tuning rule for this formula.

Why does the commissioning tab show 1=DI but ladder still rejects the input?

The BIOS setting and the commissioning tab confirm the channel behaves as a digital input at the hardware level. The application symbol the compiler sees is still the numeric input value, and ladder contacts accept Boolean operands only. Converting the symbol with TO_BOOL into a declared BOOL variable gives ladder the Boolean it demands.

Should the AI to BOOL conversion be done in FBD or ST?

Either works and the result is identical. FBD with a TO_BOOL block per channel suits one or two conversions where you want the mapping visible as a diagram. A short ST program assigned to the Timed Task stays tidier once all four channels need converting, and it keeps the ladder page nothing but clean BOOL contacts.

Will the Init Task program run again after an online download?

The program assigned to the Init Task executes during controller startup. After an online change or a Logic Builder download, do not assume it re-executed: restart the controller, or verify online that the bit is actually TRUE before you debug anything built on top of it.

Keep reading

  • Troubleshooting

    When a CODESYS TON works in structured text but not ladder

    The structured text routine counts because its four statements run in sequence inside one scan; the ladder version fails when the reset network clears Timer.Q before the addition samples it, or an unconditional reset restarts the timer every cycle. Put the call, the Q test, the addition and the reset of the same TON instance in one ladder network, left to right in that order, and it counts exactly one B per interval.

    PLC programming

  • Troubleshooting

    Start a PID ramp soak profile from ladder logic

    The profile never starts from ladder because the JMP stage handoff leaves the old stage on for three more scans, which swallows the one-scan PD pulse that must set the ramp/soak start bit. Replace the jump with SET and RST stage logic, then verify bit 11, the +33 octal control word and the +35 error flags before touching the PID instruction or table values.

    PLC programming

  • How-to

    Latch two level inputs for pump start and stop control

    Use one reset-dominant latch: set the pump when the low level probe reads dry and reset it when the high probe reads wet, so the pump holds its state between the probes. A rung that only compares the inputs drops out the moment water covers the low probe; the latch turns the probe spacing into hysteresis.

    PLC programming