IMDHMIControls LibraryShop main store

How-to

Write an S7-1200 heartbeat in ladder logic in TIA Portal

PLC programming Published 8 min read

Short answer

Tie the light to a detector, not to the heartbeat level: time the on half with one TON and the off half with a second TON, both at three times the measured state time, and OR the two done bits into one communication-fault coil. With a PC heartbeat that changes once per second, PT is 3 s and the fault trips 3 s after the last change; add TCON and TDIAG supervision when the PLC owns the TCP connection.

On this page
  1. Decide what the heartbeat has to catch
  2. Build the two-TON detector
  3. Timing the detector for a heartbeat that changes once per second
  4. Let a counter carry the sign of life
  5. Supervise the connection with TCON and TDIAG
  6. Test it with the failures you expect
  7. Questions asked

Drive the lamp from a detector, not from the heartbeat level. A bit sitting at true proves nothing: if the PC freezes while the bit is high, the level never changes and the light never moves. Run one TON timer on the heartbeat's on-state, a second TON on its off-state, preset both to three times the measured state time, and OR the two done bits into one communication-fault coil. With a heartbeat that changes once per second, both presets are T#3S and the fault picks up one preset time after the last change, whether the bit froze high or low. When the PLC owns the TCP connection to the PC, add TCON and TDIAG supervision so a dropped socket trips as well. The steps below build the ladder, and the worked example sizes both timers for a 1 s heartbeat.

Decide what the heartbeat has to catch #

First decide who creates the pulse, because that choice shapes the rest of the program. If the PC writes a block of process data every cycle, let it toggle a bit or step a counter inside that block and let the PLC time the received copy: the pulse then proves the application is alive, not merely that a socket exists. If the S7-1200 is the TCP client that dials the PC, the PLC can instead supervise the connection itself with TCON and TDIAG and learn when the transport dies, even while stale data sits in the receive buffer.

Run both checks when you can, because they catch different failures: a diagnosed connection catches a pulled cable, and a changing value catches a PC program that stopped writing. Stale data is worth catching early — on an operator panel it shows up as frozen numbers or question marks, the symptom chain traced in Why HMI values show question marks: PLC comms timeouts.

The writing program needs a computer at the machine, and in a cabinet-door or console setup that is typically the SCADA client or logger on an industrial panel PC. A sign of life between two PLCs rather than PLC to PC keeps the same receiver rule but changes the sender, and the message-based pairing in PLC-5 heartbeat without the PLC-5 sending messages covers the case where no PC peer exists at all.

Build the two-TON detector #

The detector times both halves of the toggling bit, so a bit frozen at true and a bit frozen at false trip the same fault coil.

  1. Put HeartbeatFromPC, a Bool with initial value false, inside the DB the PC's cyclic write fills, so the bit can only change while that transfer runs.
  2. In the next network place a TON box, name the instance TON1_High, drive IN from a normally open contact of HeartbeatFromPC, and enter the preset at PT — T#3S for the case sized below.
  3. In the network after it, place a second TON named TON2_Low, driven by a normally closed contact of the same bit, with the same preset. Exactly one timer runs at any moment, and it resets the instant the bit toggles.
  4. In the fault network, feed a CommsFault coil, a Bool with initial value false, from TON1_High.Q and TON2_Low.Q contacts on parallel branches: either timer completing means the heartbeat stopped alternating, which means communication lost.
  5. Drive the lamp from CommsFault through the contact polarity the machine needs, then prove the result at the terminals: a normally closed contact on a normally open lamp relay energizes the lamp on fault, and swapping either device silently inverts what you programmed. This fault bit is a diagnostic alarm, not a safety function; anything that must stop the machine safely keeps its own safety-rated channel.
  6. Decide the recovery. As wired, CommsFault drops on the first real toggle after traffic returns; seal the coil through the lamp output if the fault must stay visible until an operator acknowledges it, because the same toggle that proves recovery will otherwise clear the alarm on its own.

Timing the detector for a heartbeat that changes once per second #

Inputs. The heartbeat holds each state for 1 s — true 1 s, false 1 s — the illustrative case for this article, chosen to sit at the same once-per-second request cadence the TCON and TDIAG supervision below runs at; on your machine, measure the PC's real period at the receive buffer and substitute that number. The margin factor is 3, the rule this detector uses, picked so one missed update cannot trip. The timer is a TON with a TIME preset, and scan time adds at most one program cycle next to a 1 s state.

Rule. A TON accumulates only while its input is true, and each input here is true only while the heartbeat holds one state, so the preset must outlast the longest normal stretch of a single state, but not by much, because detection delay equals the preset. Preset each timer at three times the state time: one missed update stretches a state to twice normal and still passes, a second consecutive miss reaches the preset and trips.

Arithmetic.

  • Preset: PT = 3 × 1 s = 3 s, entered as T#3S in both instances.
  • Detection: a frozen state began at the last good change, and its timer completes PT after that change, so CommsFault picks up PT after the last heartbeat change, plus one scan.
  • Healthy exchange: every toggle resets both timers after at most 1 s of accumulation, and 1 s is well under PT, so a running link never trips.
  • Missed updates: one missed toggle stretches a state to 1 s + 1 s = 2 s, still under PT; a second consecutive miss reaches PT and trips.

Result. Both presets end at T#3S, and the lamp reacts one preset time after the last change whether the bit froze high or froze low.

Decision. For a 1 s-per-state heartbeat, set both PT values to T#3S. For any other measured state time, PT = 3 × state time — 2 s per state gives T#6S. The trip threshold reads: any single state lasting longer than three times its normal duration means communication loss. Detection delay equals the preset, so margin and speed trade one for one, and a preset under three state times starts tripping on ordinary missed updates. Three is the setting unless your PC's measured write jitter is worse than one skipped update per interval.

Let a counter carry the sign of life #

A level bit can be defeated by one stuck line; a counting value cannot. Have the PC write an INT alongside the process data that steps 0, 1, 2 and so on up to 30,000, then wraps to 0 and climbs again, once per update. In the PLC, keep LastReceived, an INT shadow of the previous scan: each cycle, compare the fresh value against the shadow before moving it in, and run the equal comparison into a third TON at the same three-times preset, feeding its Q into CommsFault beside the timer contacts. Any of the sixteen bits changing proves a fresh write, which also catches a sender whose toggle logic died while its data task lives on. Clear the fault on the next change, or seal it in exactly as the bit version does.

Supervise the connection with TCON and TDIAG #

When the S7-1200 dials the PC as the TCP client, add supervision of the socket itself. Drive Tcon.Req from the CPU's 1 Hz system memory clock bit Clock1Hz AND NOT %M0.0, so the PLC requests the connection once per second until Tcon.Done sets %M0.0, the connected marker. Then drive Tdiag.Req from %M0.0 AND Clock1Hz, and drop %M0.0 again whenever Tdiag.Done returns with Tdiag.result other than 4; in that scheme, result 4 means the connection is OK. Treat 4 as a convention to confirm rather than a law: the S7-1200 manual documents the ERROR and STATUS codes for TCON and TDISCON, including the network error TCON reports when it cannot reach the remote partner, and those meanings belong to your CPU's firmware, so confirm them in the installed instruction help.

A diagnosed connection is transport health, not application health: the socket can stay up while the PC program stops writing, which is exactly the failure the toggling bit or the counter catches. Run both when the data matters. After a drop, the port may need TDISCON to re-initialize before the next TCON succeeds, so exercise the reconnect path once deliberately instead of assuming the first retry lands. TSEND_C and TRCV_C fold connection handling into the data call if you prefer, and TRCV_C can establish and terminate a TCP or ISO-on-TCP connection by itself. Route the fault so an operator sees it: How to create a SCADA PLC communication alarm for link loss lays out the link-loss alarm from the SCADA side.

Test it with the failures you expect #

  • Put the machine in a safe state first: nothing the fault bit drives may actuate process equipment during the test.
  • Normal exchange: the heartbeat alternates, both timers sit below preset, CommsFault stays off, and the lamp holds its healthy state — noted at the terminal, not read off the contact symbol.
  • Frozen sender: pause the PC program's writes while leaving the link up; CommsFault picks up one PT after the last change.
  • Dropped link: pull the cable with the PC program still running; the TDIAG path drops %M0.0, and the Tcon.Req pulses resume.
  • Recovery: restart the PC side, confirm the fault clears by your chosen rule, and time the reconnection so operators know what a normal outage lasts.
  • Nuisance check: a PC that skips one write per interval stays quiet at the example preset; if the fault still trips, measure the worst real state time and set the preset to three times that.

Questions asked

Where should the PC heartbeat bit live in the PLC data?

Put the heartbeat bit in the same buffer the cyclic process data lands in, so it can only change when that whole transfer updates. A heartbeat produced by a separate code path can keep toggling after the process data has gone stale, and the detector then watches the wrong thing. Time the value the PLC receives, never a local echo of what the PLC itself wrote.

Why does my communication fault come on for a few seconds at CPU startup?

Until the PC's first write arrives, the received bit sits at false, so the off-state timer runs and the fault coil picks up when it reaches its preset with the example's settings. Gate CommsFault with the CPU's first-scan system memory bit, which lives in the same system memory area as Clock1Hz, or hold the fault off until the first complete update of the receive buffer has landed.

What preset do I use if the PC updates every 100 ms?

Apply the same three-times rule to the measured state time. At 100 ms per update, 3 × 100 ms = 300 ms, so both presets become T#300MS: the fault trips 300 ms after the last change, one missed update, a 200 ms state, still passes, and a second consecutive miss trips. Detection delay always equals the preset, so tighten it only as far as the PC's update jitter allows.

Does the same heartbeat work between two PLCs?

Yes. Each controller toggles a bit or increments a counter in the data it produces, and each side runs the same two-timer check on the copy it receives. Produced and consumed tags, network messages or TCP change only how the value travels; the receiving rule, preset each timer at three times the longest normal state time, stays the same on both ends.

Keep reading

  • How-to

    PLC-5 heartbeat without the PLC-5 sending messages

    Let the ControlLogix do all the sending: it either writes an incrementing heartbeat word to the PLC-5 over Ethernet MSG or pulses a spare output into a spare PLC-5 input, and the PLC-5 resets a watchdog timer only on a genuinely new value or pulse edge — never on message-complete status. Set the preset above two update periods plus measured transfer delay; in the worked example, 1500 ms detects a dead link within 1.5 s.

    PLC programming

  • Troubleshooting

    Why Modbus cannot transfer a PLC ladder program over TCP

    No Modbus address reaches ladder memory: the application protocol defines four data tables — coils, discrete inputs, input registers, holding registers — and program storage is not in any of them. Swapping RTU for TCP changes transport, not protocol. On DirectLogic PLCs the program moves only over K-Sequence or DirectNET through the vendor's programming tools; Modbus stays for mapped user data.

    PLC programming

  • Explainer

    What STEP 7 is in a PLC project, and which one you need

    STEP 7 is the engineering software you use to program, configure, diagnose and download to SIMATIC PLCs — a workstation tool, not a part of the PLC itself. SIMATIC Manager, also called STEP 7 Classic, configures S7-300 and S7-400 CPUs; STEP 7 inside TIA Portal configures S7-1200 and S7-1500. Read the CPU family from the module label or the existing project, open the matching environment, and confirm the project's hardware configuration matches the installed CPU before you edit or download.

    PLC programming