Implementing Night Period Time Logic on ET200S IM 151-8 PN/DP

David Krause15 min read
SiemensTIA PortalTutorial / How-to
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 Definition: Triggering a Boolean Across the Midnight Boundary

The functional requirement is the canonical irrigation/water-conservation pattern that comes up on most greenfield ET200S sites: a single TRUE/FALSE tag must be TRUE for every second between 21:00:00 and 06:59:59 local time, and FALSE outside that band, with the boolean driving a low-flow valve so that the night-period trickle does not overlap the daytime high-consumption cycle. The interval crosses midnight, which makes the trivial ladder formulation (hour ≥ 21 AND hour ≤ 7) wrong: that AND evaluates false across midnight. The correct reading is hour ≥ 21 OR hour < 7, and the comparison itself must be derived from the PLC's wall-clock rather than any user-entered shift table.

The platform is the ET200S IM 151-8 PN/DP (catalog 6ES7151-8AB01-0AB0, firmware V3.x) programmed in TIA Portal V15.1 (Update 4) using LAD. With those constraints fixed, the question is which instruction set to use and which library blocks are guaranteed to compile.

00:00 03:00 06:00 09:00 12:00 15:00 18:00 21:00 23:00 24:00 NIGHT = TRUE (07:00-21:00) NIGHT = FALSE 10-hour window, midnight-crossing

2. Why the LGF Library LGF_TimerSwitch Does Not Compile on ET200S

The first instinct on TIA Portal V15.1 is to drop in LGF_TimerSwitch from the Siemens LGF Library. The block compiles cleanly on an S7-1500 target and immediately returns the desired boolean. On ET200S IM 151-8 PN/DP, however, the same block produces:

Compile error: Data type DTL not supported by the CPU
Block LGF_TimerSwitch cannot be used in this device family

The reason is structural, not a version mismatch.

LGF_TimerSwitch internal symbol Native type used Native to ET200S instruction set
inputTime DTL (16-byte Date_And_Time Long) S7-1500 (V2.0+), S7-1200 (FW V4.2+) Not supported
i_switchTime / i_durationMSec ULInt / LInt S7-1500 (V2.0+), S7-1200 (FW V4.2+) Not supported
Time-format helper variables UDInt arithmetic S7-300/400/1500 Supported

Per the official LGF Library Manual (Siemens Support Entry 109479728), every block in the LGF library targets the SIMATIC S7-1200 (firmware V4.2 and above), the SIMATIC S7-1500 (firmware V2.0 and above), and the S7-PLCSIM simulator (V14 and above). ET200S IM 151-8 PN/DP, despite carrying its own CPU, executes the S7-300 instruction set and so cannot declare DTL tags, cannot perform ULInt arithmetic, and cannot load the LGF library. Upgrading TIA Portal to Update 4 does not relax the type coverage of the CPU class.

Hardware-revision constraint: TIA Portal V15.1 Update 4 patches the offline library manager and the online diagnostics, not the IM 151-8 PN/DP firmware. CPU class is bound to firmware revision 3.x; the type catalogue that the compiler loads is fixed by the device description of the family, and that catalogue is intentionally a strict subset of S7-300/ET200S data types.

3. ET200S IM 151-8 PN/DP Hardware and Software Constraints

Item Specification
Catalog (most common) 6ES7151-8AB01-0AB0
Catalog (legacy) 6ES7151-8AB00-0AB0, 6ES7151-8FA00-0AB0 (F-variant)
Firmware range V1.x, V2.x, V3.x
CPU class Functionally equivalent to a SIMATIC S7-314 / S7-315
Native time types DATE_AND_TIME (8-byte BCD), TIME (signed DINT ms), S5TIME
NOT supported DTL, LTime, ULInt, LInt, LWord
Wall-clock read SFC1 READ_CLK outputs DATE_AND_TIME
Wall-clock write SFC0 SET_CLK
TOD comparison Manual INT compare on extracted DBB 3 (BCD hour), or full FC20/TimeDB from Siemens FAQ 43566349
Time sync PROFINET NTP client (via CP), or CPU RTC backed by capacitor (no battery)

The practical consequence is that any object uses the S7-300 instruction subset: SFC and SFB calls, BCD byte slicing, and classic comparator/accumulator ladder. Anything that builds on the advanced bit/byte types of S7-1500 has to be ported to S7-300 primitives by hand.

4. Solution Overview: Three Engineering Paths

Three deterministic approaches solve the same problem on ET200S, in increasing order of code reuse and decreasing order of bespoke development.

Path Mechanism LOC Maintenance burden Suitable for
1. Siemens FAQ 43566349 (FC20/TimeDB) Pre-built turn-on/turn-off comparator imported from the official support FAQ ~20 Low (single-source) Projects that already have a maintained copy of the FAQ library
2. Native SFC1 + element compare SFC1 READ_CLK into a tagged DATE_AND_TIME, then element access on .HOUR ~15 Very low New projects; minimalist code, no external dependency
3. Port LGF_TimerSwitch Side-project an S7-1500 instance of LGF_TimerSwitch, replace DTL with DATE_AND_TIME and ULInt with UDInt ~200 Medium (version-tracked port) Multi-site deployments that want a single source of truth for a richer timer-switch library

5. Path 1: FC20 / TimeDB From Siemens FAQ 43566349

5.1 Download and import

The Siemens support FAQ 43566349 provides a ST/LAD source bundle containing two artefacts:

  • FC20 — a comparator FB that accepts a current DATE_AND_TIME input and produces a boolean output that latches between configurable turn-on and turn-off timestamps.
  • TimeDB — an instance DB holding all parameter tunables (on time, off time, enable, status).

Both blocks were written before DTL existed and therefore operate exclusively on the 8-byte DATE_AND_TIME BCD layout, which makes them drop-in compatible with ET200S.

5.2 Wire-up

// Pre-conditions in TIA Portal V15.1
// - Add the FC20 and TimeDB blocks to the project tree
// - Open TimeDB and set:
//     TimeDB.OnHour   := 21
//     TimeDB.OnMinute := 0
//     TimeDB.OffHour  := 7
//     TimeDB.OffMinute:= 0

// Network 1 — Read wall clock
CALL "READ_CLK"                 // SFC1
   RET_VAL := MW100
   CDT     := "myDB".runtime    // DATE_AND_TIME (8 bytes)

// Network 2 — Invoke the timer comparator
CALL FC20
   currentDT  := "myDB".runtime
   timeParams := "TimeDB"
   out        := "myDB".night_period_active

With OnHour = 21 and OffHour = 7, FC20 produces TRUE from 21:00 through 06:59, exactly matching the requirement. The FAQ source places the comparison in TIME_OF_DAY (TOD, unsigned milliseconds since 00:00) arithmetic; this internalises the midnight-crossing logic into the supplied FC and avoids ladder overload.

Validation note: The original FAQ body declares a 21:00–06:00 example. If the boundary behaviour at 06:00:00 versus 07:00:00 matters in the field, inspect the comparator inside FC20: it tests currentTOD ≥ onTime AND currentTOD < offTime, so the ending second at OffHour:OffMinute:00 is excluded. To make the window inclusive of both endpoints (TRUE at 07:00:00 exactly), add one millisecond to OffHour:OffMinute so the FALSE transition lands at 07:00:01.

6. Path 2: Native LAD Logic Using SFC1 READ_CLK

This path keeps the implementation entirely inside TIA Portal’s standard S7-300 instruction set and avoids external libraries. It is the option most field engineers pick on first contact with ET200S and is the easiest to defend during a code review.

6.1 DB declaration (TIA Portal V15.1 LAD)

DATA_BLOCK "myDB"   // global DB, retainable attributes can be set per element
  STRUCT
    runtime      : DATE_AND_TIME;    // 8 bytes BCD, populated by SFC1
    hour         : INT;             // 0..23, mirror of .HOUR element
    minute       : INT;             // 0..59
    second       : INT;             // 0..59
    ret_val      : INT;             // SFC1 status, retain last error
    night_high   : BOOL;            // hour >= 21 latch
    night_low    : BOOL;            // hour < 7 latch
    night_active : BOOL;            // final driver to the valve
  END_STRUCT;
END_DATA_BLOCK

6.2 Network 1 — Read wall clock

CALL "READ_CLK"                          // SFC1
   RET_VAL := "myDB".ret_val
   CDT     := "myDB".runtime

6.3 Network 2 — Mirror HOUR and clear latches each cycle

      L     "myDB".runtime.HOUR           // implicit INT element access
      T     "myDB".hour                   // local INT mirror
      L     "myDB".runtime.MINUTE
      T     "myDB".minute
      L     "myDB".runtime.SECOND
      T     "myDB".second

      CLR                                     // reset latches (avoid sticky ON
      =     "myDB".night_high                 //  if the previous cycle went RUN
      =     "myDB".night_low                  //  without an updated SFC1 read)

6.4 Network 3 — Hour ≥ 21

      L     "myDB".hour
      L     21
      >=I
      S     "myDB".night_high

6.5 Network 4 — Hour < 7

      L     "myDB".hour
      L     7
      <I
      S     "myDB".night_low

6.6 Network 5 — Combine and drive the valve

      O     "myDB".night_high
      O     "myDB".night_low
      =     "myDB".night_active
      =     %Q0.0                          // example mapping to the valve output

6.7 SCL variant for cleaner review

"myDB".ret_val := READ_CLK(CDT := "myDB".runtime);
"myDB".hour    := "myDB".runtime.HOUR;

IF ("myDB".hour >= 21) OR ("myDB".hour < 7) THEN
    "myDB".night_active := TRUE;
ELSE
    "myDB".night_active := FALSE;
END_IF;

This code does not depend on DTL, ULInt, or any S7-1500 class type, so it compiles on every ET200S IM 151-8 PN/DP firmware revision 1.x through 3.x. The SFC1 call runs in roughly 35–60µs per scan on an IM 151-8 firmware V3.x CPU, and element access via .HOUR is fully supported on TIA Portal V15.1 against a S7-300 instruction target.

7. Path 3: Porting LGF_TimerSwitch From S7-1500 to ET200S

7.1 Create a temporary S7-1500 project

The LGF library blocks are verified only on S7-1200/1500, so opening the block source on a stub S7-1500 project preserves the original test assertions and gives the compiler target to validate the port.

7.2 Replacement map

Symbol in LGF_TimerSwitch Original (S7-1500) Replacement (ET200S) Notes
inputTime DTL DATE_AND_TIME Element access remains identical for HOUR/MINUTE/SECOND; YEAR/MONTH/DAY exist in both.
i_switchTime (ms offset) ULInt UDInt Max TOD = 86 399 999 ms (~86M, fits in 32-bit unsigned range).
i_durationMSec LInt DInt Limit duration to < 2 147 483 647 ms (~24.8 days), sufficient for daily windows.
tempIndex Int Int No change required.
Helper ENO usage Implicit Explicit S7-300 still supports EN/ENO but the compiler flag is stricter; pin the call sites.

7.3 Convert element access

Where the original LGF block reads DTL fields such as inputTime.HOUR, the ported version simply substitutes a DATE_AND_TIME variable with the same named fields. The element metadata is identical because both DTL and DATE_AND_TIME encode HOUR/MINUTE/SECOND as BCD values accessible by symbolic name.

7.4 Replace ULInt / LInt arithmetic

The critical arithmetic block in LGF_TimerSwitch subtracts two timestamps to compute an unsigned millisecond offset. With ULInt, the wraparound semantics are intentional; switching to UDInt reproduces the same behaviour as long as the inputs stay in 0..86 399 999. Use a guard line to clamp the inputs before subtraction:

IF i_switchTime < 0 THEN i_switchTime := 0; END_IF;
IF i_switchTime > 86399999 THEN i_switchTime := 86399999; END_IF;

Compile the ported source in the S7-1500 stub project first to verify logic equivalence, then download the same FC and DB to the ET200S IM 151-8 PN/DP target.

8. Handling the Midnight-Boundary Edge Cases

8.1 Inclusive vs exclusive endpoints

The interval 21:00–07:00 has multiple interpretations. The most common convention in irrigation control is exclusive of the end, i.e., TRUE from 21:00:00 through 06:59:59 and FALSE from 07:00:00. If the END should be inclusive, the comparison becomes hour ≥ 21 OR (hour ≥ 0 AND hour ≤ 7), which simplifies to the same logic at hour granularity but diverges for minute-second resolutions where the boundary at 07:00:00 must be captured. Validate which convention the process needs before writing the ladder; the wrong default at one second boundary can cost a litre of water per night per valve.

8.2 Daylight saving and NTP drift

If the PLC receives NTP via PROFINET (CP), the wall clock jumps by one hour at the DST transition. The boolean does not glitch because the comparison runs every scan and the transition takes effect immediately. The watch item to validate is the date display in HMI: .DATE element will roll over at midnight regardless. If the site has no NTP source, the IM 151-8 PN/DP RTC drifts about ±1–2 ppm per month, so the latch can shift by minutes per year; ladder should be reviewed annually.

8.3 PLC mode transitions

After STOP→RUN, the runtime DB element .runtime retains its previous value until the first SFC1 call returns. To avoid a 24-hour stale output during restart, place SFC1 READ_CLK in OB1 (or OB35 cyclic interrupt at 1 s) without conditional gate. ET200S IM 151-8 PN/DP firmware V3.x calls SFC1 reliably even after a full power-cycle, but the first read after STOP→RUN can arrive up to one OB1 cycle late if SFC1 is parked in a slow-priority execution level.

9. Step-by-Step Commissioning Checklist

  1. Open the TIA Portal V15.1 project that contains the ET200S station with IM 151-8 PN/DP as head module.
  2. Confirm the device configuration online via "Online & Diagnostics → Module Information" and record the catalog number and firmware revision.
  3. Add a global DB (e.g., myDB) with the structure from §6.1 if Path 2 is used.
  4. If Path 1 is used, import the FC20 / TimeDB bundle from FAQ 43566349 as external sources and recompile inside the project.
  5. Insert the SFC1 READ_CLK call as the first network of OB1 (Path 2) or as the clock provider (Path 1).
  6. Wire the final boolean (night_active) to the valve output word; for a fail-safe valve, route through the assigned F-channel rather than direct %Q.
  7. Set the on-board time using "Online & Diagnostics → Set Time" or via SFC0 if the cabinet has no NTP.
  8. Force the runtime HOUR element to 21 via the watch table; verify the boolean asserts within one scan.
  9. Force the runtime HOUR element to 6; verify boolean is TRUE.
  10. Force the runtime HOUR element to 7; verify boolean releases.
  11. Force the runtime HOUR element to 23:59 and step the clock by 1 minute at a time using SFC0; verify the latch turns ON at exactly 21:00 local time.
  12. Observe the boolean for at least 24 hours of real wall-clock transitions before releasing to operations.

10. Verification and Acceptance Test

10.1 Simulation with PLCSIM

S7-PLCSIM V15 supports the IM 151-8 PN/DP station type since V15.0. Use a 24-hour accelerated simulation by calling SFC0 in OB35 every 100 ms with a clock that advances 1 min per execution. Log the boolean transition points to a trace and confirm 21:00:00 and 07:00:00 transitions occur on the expected scan.

10.2 Field test with SFC0 SET_CLK

For acceptance on the live CPU, an alternative is to temporarily set the system time to 20:59:50 and let the clock run to 21:00:00 naturally. Use an HMI tag or watch table that increments the timestamp by 1 minute every 60 seconds and log the boolean change; this generates an audit trail for regulatory or quality audits.

10.3 Long-duration trace

Many facilities run the logic for 7–30 days before sign-off. Place a trace on night_active with a 1 s sample rate and a 30-day buffer (~2.6 MB of trace data). Confirm zero unexpected transitions, in particular during DST crossover (where applicable) and during power outages.

11. Troubleshooting Matrix

Symptom Likely root cause Diagnostic step Fix
Compile error: Data type DTL not supported Hardware family constraint; LGF library used on ET200S Check "Project tree → Devices → Properties → Family" Move to Path 1 (FC20) or Path 2 (native SFC1)
Compile error: Block LGF_TimerSwitch cannot be used in this device family Same as above; library metadata rule Project tree check Same; or complete Path 3 port
night_active stuck FALSE on every scan runtime DB element never updates; SFC1 not called or RET_VAL ≠ 0 Watch table on myDB.runtime and myDB.ret_val Verify SFC1 is invoked unconditionally in OB1; verify CPU in RUN
night_active stuck TRUE all day Sticky latch from earlier cycle; CLR in §6.3 missing Watch night_high / night_low Add the CLR network to clear latches every cycle before compare
RET_VAL of SFC1 = 8081 (hex) CPU clock not synchronised; RTC not set Check "Diagnostics → Time" Set time via SFC0 or NTP
runtime shows 1990-01-01 00:00 First time after de-energised for > capacitor hold period Same Re-set time; verify capacitor or battery is intact
Signal flickers at exactly 21:00 / 07:00 HMI script writing time via SFC0 every cycle Cross-reference projects for SFC0 calls Remove the SFC0 write; rely on NTP or single set
Window shifts after DST crossover No NTP sync configured Online diagnostics → time status Enable NTP via PROFINET or attach a CP with NTP client
LAD with DBB 3 shows BCD hex (0x21 = 21 decimal) Direct byte access used instead of element access Watch table on raw DBB Switch to symbolic myDB.runtime.HOUR
Ported LGF block compiles on S7-1500 but not ET200S Missed ULInt / LInt replacement Cross-reference symbols in source Complete the §7.2 substitution table

12. Performance, Scan-Time, and Memory Footprint

Path Code footprint (DB + FB instances) Estimated scan-time cost Notes
1. FC20 / TimeDB ~1.2 KB of FB + DB instance + 0.2 KB of DB interface ~80µs per OB1 cycle Imported source, traces cleanly to FAQ lineage
2. Native LAD ~24 bytes DB ~50µs per OB1 cycle (SFC1 + 5 comparators) Zero external dependencies
3. Ported LGF ~3 KB of block code + DB ~120µs per OB1 cycle Useful for multi-site deployments that share a library

For an irrigation application, the OB1 cycle budget for an ET200S IM 151-8 PN/DP is typically 5 ms or more, so 50–120µs of interval-detection logic is well below 3% of the cycle and does not require acceleration to a higher-priority OB.

Frequently Asked Questions

Can I use RD_SYS_T instead of SFC1 on ET200S?

No. RD_SYS_T is an S7-1200/1500 instruction that outputs a DTL tag; the ET200S IM 151-8 PN/DP cannot declare DTL. The equivalent on ET200S is SFC1 READ_CLK, which returns DATE_AND_TIME (8-byte BCD) and is supported on every S7-300 class CPU including the IM 151-8.

Why does the LGF library refuse to compile on ET200S even on TIA Portal V15.1 Update 4?

Because the IM 151-8 PN/DP executes the S7-300 instruction set; it does not support DTL, ULInt, or LInt. The LGF library manual (109479728) explicitly lists S7-1200 from firmware V4.2 and S7-1500 from firmware V2.0 as the supported families, so the block cannot be brought across without porting the data types.

Which solution is fastest to deploy on a single ET200S site with no NTP?

Path 2 (SFC1 READ_CLK + element access on .HOUR) requires only a few networks of native LAD, compiles in seconds, and has zero external library dependency. It is the recommended first deployment on any single-site retrofit where maintenance teams are unfamiliar with the legacy FC20/TimeDB source from FAQ 43566349.

How do I make the boolean transition at exactly 07:00:00 if my fields use minute precision?

Replace 7 with a more precise lower bound: compare ("myDB".runtime.HOUR < 7) OR ("myDB".runtime.HOUR = 7 AND "myDB".runtime.MINUTE = 0 AND "myDB".runtime.SECOND = 0). At minute granularity it usually suffices to compare (HOUR < 7) OR (HOUR = 7 AND MINUTE < 1). At hour granularity alone the legal convention is that 07:00:00 falls inside the FALSE side of the window.

What does SFC1 RET_VAL 8081 mean on ET200S and how do I recover?

RET_VAL W#16#8081 means the internal CPU clock is invalid or has never been synchronised. The IM 151-8 PN/DP retains time across power cycles for a limited period (days to weeks, depending on the capacitor in the head module). Call SFC0 SET_CLK with the current wall-clock time, or enable PROFINET NTP via a CP in the same station, then re-run SFC1. The boolean should resume correct transitions within one OB1 cycle.

Will the clock survive a power-cycle on the IM 151-8 PN/DP without NTP?

The IM 151-8 PN/DP uses an internal capacitor-backed RTC (no user-replaceable battery). Typical hold-up is 1–4 weeks depending on firmware and ambient temperature. For sites where the time could be lost silently, use Path 2 with an OB1 watchdog that asserts a maintenance bit when the runtime tag drifts more than 24 hours from the expected wall-clock value, so the night interval never operates on a stale 1990-01-01 timestamp.

Back to blog