Siemens SCL Code Examples for S7-1200 and S7-1500
Four complete SCL blocks that come up in almost every TIA Portal project. Each one is an external source you can import as-is, and each one passes the SCLcode validator for S7-1200 and S7-1500.
Use them to learn SCL patterns, or as a starting point you adapt. If you need a variation, describe it to the AI SCL generator instead of rewriting it by hand.
1. TON on-delay timer in SCL
On S7-1200 and S7-1500 the IEC timer is declared as a static variable of type TON_TIME (a multi-instance), so it needs no separate instance DB. Call it on every scan with IN and PT, then read Q and ET.
FUNCTION_BLOCK "FB_DelayedStart"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_INPUT
Start : Bool; // Request from the operator or sequence
Delay : Time := T#5S; // On-delay before the output switches
END_VAR
VAR_OUTPUT
Running : Bool; // TRUE once the delay has elapsed
Remaining : Time; // Time left until Running switches on
END_VAR
VAR
StartDelay : TON_TIME; // Multi-instance IEC timer, no extra DB
END_VAR
BEGIN
// Call the timer on every scan so it sees the falling edge of Start
#StartDelay(IN := #Start,
PT := #Delay);
#Running := #StartDelay.Q;
IF #Start AND NOT #StartDelay.Q THEN
#Remaining := #Delay - #StartDelay.ET;
ELSE
#Remaining := T#0MS;
END_IF;
END_FUNCTION_BLOCK
- Do not call the timer only inside an
IF: it would never seeINgo FALSE and would not reset. - Time literals are written as
T#5S,T#500MSorT#2M. Subtracting twoTimevalues is allowed. - There is no
TIME()function in SCL. Elapsed time always comes from a timer'sEToutput.
More on timer behaviour: TON vs TOF explained.
2. State machine with CASE
A CASE statement on an integer step is the standard way to write sequences in SCL. This tank sequence fills, heats for a set time, drains and has a watchdog that latches a fault if filling takes too long.
FUNCTION_BLOCK "FB_TankSequence"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_INPUT
Start : Bool;
Stop : Bool;
Reset : Bool;
LevelHigh : Bool; // High level switch, TRUE = tank full
LevelLow : Bool; // Low level switch, TRUE = liquid present
HeatTime : Time := T#30S;
END_VAR
VAR_OUTPUT
InletValve : Bool;
Heater : Bool;
OutletValve : Bool;
Fault : Bool;
Step : Int; // Current step for the HMI
END_VAR
VAR
State : Int; // 0 Idle, 10 Fill, 20 Heat, 30 Drain, 99 Fault
HeatTimer : TON_TIME;
FillWatchdog : TON_TIME;
END_VAR
BEGIN
// Timers are called every scan, outside the CASE
#HeatTimer(IN := #State = 20,
PT := #HeatTime);
#FillWatchdog(IN := #State = 10,
PT := T#2M);
// Stop always returns to Idle, except from a latched fault
IF #Stop AND #State <> 99 THEN
#State := 0;
END_IF;
CASE #State OF
0: // Idle: wait for a start command
IF #Start AND NOT #Stop THEN
#State := 10;
END_IF;
10: // Fill until the high level switch, fault after 2 minutes
IF #LevelHigh THEN
#State := 20;
ELSIF #FillWatchdog.Q THEN
#State := 99;
END_IF;
20: // Heat for HeatTime
IF #HeatTimer.Q THEN
#State := 30;
END_IF;
30: // Drain until the low level switch clears
IF NOT #LevelLow THEN
#State := 0;
END_IF;
99: // Fault: wait for an operator reset
IF #Reset THEN
#State := 0;
END_IF;
ELSE
// Unknown step number: fail safe
#State := 99;
END_CASE;
// Outputs are derived from the state, never set inside the CASE
#InletValve := #State = 10;
#Heater := #State = 20;
#OutletValve := #State = 30;
#Fault := #State = 99;
#Step := #State;
END_FUNCTION_BLOCK
- Step numbers in tens (0, 10, 20…) leave room to insert steps later without renumbering.
- Timers are called outside the
CASE, withINtied to the step, so they reset automatically when the step changes. - Outputs are assigned from the state after the
CASE. That avoids outputs that stay set when a step is skipped. - The
ELSEbranch catches corrupted step numbers and goes to the fault step.
A deeper walkthrough: the SCL state machine pattern.
3. FIFO ring buffer
A first-in, first-out buffer is useful for tracking parts on a conveyor, queuing orders or storing the last N measurements. This version uses a fixed array with head and tail indices, so no data is shifted on each call.
FUNCTION_BLOCK "FB_Fifo"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_INPUT
Push : Bool; // Rising edge stores InValue
Pop : Bool; // Rising edge returns the oldest value
Clear : Bool; // Empties the buffer
InValue : DInt;
END_VAR
VAR_OUTPUT
OutValue : DInt;
Count : Int;
Empty : Bool;
Full : Bool;
END_VAR
VAR
Buffer : Array[0..15] of DInt;
Head : Int; // Next slot to read
Tail : Int; // Next slot to write
Used : Int; // Number of stored values
PushEdge : R_TRIG;
PopEdge : R_TRIG;
END_VAR
BEGIN
#PushEdge(CLK := #Push);
#PopEdge(CLK := #Pop);
IF #Clear THEN
#Head := 0;
#Tail := 0;
#Used := 0;
END_IF;
// Write at the tail, wrap around at the end of the array
IF #PushEdge.Q AND #Used < 16 THEN
#Buffer[#Tail] := #InValue;
#Tail := (#Tail + 1) MOD 16;
#Used := #Used + 1;
END_IF;
// Read from the head, oldest value first
IF #PopEdge.Q AND #Used > 0 THEN
#OutValue := #Buffer[#Head];
#Head := (#Head + 1) MOD 16;
#Used := #Used - 1;
END_IF;
#Count := #Used;
#Empty := #Used = 0;
#Full := #Used >= 16;
END_FUNCTION_BLOCK
R_TRIGturnsPushandPopinto one-scan pulses, so holding a button does not fill the buffer.MOD 16wraps the indices. If you change the array size, change the constant in all three places, or declare it as a constant.- Use a
UDTinstead ofDIntto queue complete records, such as a part ID with its measurements.
Arrays and structures in detail: SCL array and struct guide.
4. Analog input scaling
Siemens analog input modules deliver a nominal range of 0 to 27648 for 0–10 V or 4–20 mA. This function converts the raw value to engineering units and flags over- and underrange.
FUNCTION "FC_ScaleAnalog" : Real
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_INPUT
Raw : Int; // Analog input word, nominal range 0..27648
LowLimit : Real; // Engineering value at raw 0, e.g. 0.0 bar
HighLimit : Real; // Engineering value at raw 27648, e.g. 10.0 bar
END_VAR
VAR_OUTPUT
OutOfRange : Bool; // Overrange, underrange or wire break
END_VAR
VAR_TEMP
Clamped : Int;
END_VAR
BEGIN
// Values outside 0..27648 are over- or underrange on Siemens analog cards
#OutOfRange := (#Raw < 0) OR (#Raw > 27648);
#Clamped := LIMIT(MN := 0, IN := #Raw, MX := 27648);
#FC_ScaleAnalog := #LowLimit + (INT_TO_REAL(#Clamped) / 27648.0) * (#HighLimit - #LowLimit);
END_FUNCTION
- It is a
FUNCTION(FC) because it keeps no state between calls. Call it like#Pressure := "FC_ScaleAnalog"(Raw := "AI_Pressure", LowLimit := 0.0, HighLimit := 10.0, OutOfRange => #PressureFault);. - TIA Portal also offers
NORM_XandSCALE_X. The explicit formula above shows the maths and works the same way on S7-1200 and S7-1500. - Check your module manual: bipolar ranges such as ±10 V use −27648 to 27648.
Using these examples in TIA Portal
Save a block as a .scl file, add it under *External source files* and choose *Generate blocks from source*. The import guide covers the steps and the common compile errors. For more patterns, browse the S7-1200 SCL examples article or the templates.
Generate your own SCL block
Describe the logic in plain language and get a Siemens SCL function block you can import into TIA Portal. Free account, no credit card.