Catto / Topic Map · Industrial Electronics II Year 7
Industrial Electronics II · 120 h · Topic 5 of 8

Basic programming applications

With half a dozen blocks—timers, counters, flags, edges and registers—the vast majority of industrial automation tasks can be solved. What sets a good program apart is not using exotic blocks, but using the usual ones well.

Timers Counters Edges Shift register

01Timers

InputTONTOFTPset timelong pulse short pulseInputThe same signal in all three cases: a long pulse and then a short one.TONOn-delay: the output appears if the input holds for the whole time. The short pulse is notlong enough.TOFOff-delay: the output follows the input and turns off the set time after the input falls.TPFixed-duration pulse for each edge, regardless of how long the input lasts.
Figure 1. The three standard timers, animated. With the same input, each one produces a different output: on-delay, off-delay and fixed-duration pulse.
TypeBehaviorExample use
TONThe output turns on after the set time, if the input holds. If the input drops earlier, the count resets.Waiting for the pressure to stabilize before enabling the process.
TOFThe output turns on with the input and turns off the set time after the input drops.Keeping an exhaust fan running for a few minutes after the oven is stopped.
TPA fixed-duration pulse for each input edge, regardless of how long the input lasts.Giving a half-second blast of air when a part is detected.
The TON resets, it does not pause

If the input of a 10-second TON drops at 8 seconds and rises again, the count starts over from zero: it does not accumulate. When time has to be accumulated—pump running hours, for example—a TON is not used; instead, a timebase pulse counter is used, stored in a retentive memory area.

02Counters, flags and edges

Counters
  • CTU: counts up and turns on its output on reaching the preset value.
  • CTD: counts down from a loaded value.
  • CTUD: does both, with separate inputs.
  • All of them need an explicit reset input, and the value should be in a retentive memory area if the count represents production.
Counting without an edge is the classic mistake

The count input must receive a one-scan pulse. If the sensor is connected directly, the counter increments on every scan while the part is in front of the sensor: a single part can be counted fifty times.

It is solved with a rising-edge detection block between the sensor and the counter. It is one line of program, and its absence is the number one cause of “crazy” counters.

Flags: the bits that go nowhere

They are used to store intermediate states: steps of a sequence, enabled conditions, fault memory. It is best to group them by function and give them symbolic names. And those that represent machine state or accumulated production go in a retentive memory area, so that a power failure does not erase what the machine has already done.

03Shift register and generators

sensorejectorencoder: one pulse every 10 mmregister1The part is inspectedThe inspection result is loaded into the first bit of the register.1The belt advancesEach encoder pulse shifts the whole register one position: the data travels with the part.1Still advancingIf the belt stops, the encoder gives no pulses and the register does not advance either: no drift.1The bit reaches the endThe defective part is in front of the ejector and the output is activated.
Figure 2. Part tracking with a shift register, animated. Each encoder pulse shifts all the information one position, so each part’s data “travels” with it to the station where it is needed.
Shift register

This is the standard way to solve tracking on a line: the part is inspected at the start, the result is stored in the first bit of the register, and each advance of the belt shifts everything one place. When the bit reaches the end, the part is in front of the ejector.

The key is that the shift is triggered by the actual movement—an encoder or a passing sensor—and not by a timer: if the belt changes speed or stops, the timer lies and the parts get sorted wrongly.

Pulse generators
  • Flashing: two chained timers, one sets the on time and the other the off time. It is the basis of every blinking indicator.
  • Clock flags: almost all PLCs offer bits that toggle at fixed frequencies —2 Hz, 1 Hz, 0.5 Hz—, ready to use.
  • Timebase: one pulse per second feeding a counter is the correct way to accumulate running hours.

04Three worked examples

Star-delta starting

15 kW motor. Sequence: the line contactor and the star contactor close together; at 6 seconds the star opens; 50 ms later the delta closes.

ElementCondition
KM linestart, with seal-in, and no fault or emergency stop.
KM starKM line on and 6 s TON not yet timed out and KM delta off.
KM delta6 s TON timed out and 50 ms TON timed out and KM star off.

The 50 ms wait is not a detail: it is the arc extinction time. Closing the delta before the star has fully opened causes a short circuit between phases. That is why, besides the interlock in the program, the two contactors carry a mechanical and electrical interlock in the panel: safety cannot depend on software.

Two-street traffic light

Cycle: green 25 s, yellow 4 s, red while the other street is in green and yellow. It is solved with a four-step Grafcet and one timer per step, not with a tangle of timers in parallel.

The advantage of the step structure shows when adding the night flashing mode: it is an OR divergence from the initial step, three rungs. With loose timers everything would have to be redone.

Conveyor sorting

A conveyor belt with a height sensor at the start and an ejector 1.8 m further along. The encoder delivers one pulse every 10 mm.

  • Distance in pulses: 1800 mm / 10 mm = 180 pulses.
  • A 180-bit shift register is used, or one counter per part.
  • Each encoder pulse shifts the register; when the bit of a defective part reaches position 180, the ejector is activated.
  • If the belt stops, the encoder gives no pulses and the register does not advance: synchronization is maintained by itself. With a timer, on the other hand, everything would drift out of step.

05In the lab

Lab 1 · The three timers

Program TON, TOF and TP with the same input and observe the three outputs in online monitoring and with a recorder. Test what happens when the input is cut before the time elapses in each case, and document the behavior.

Lab 2 · Counter with and without edge detection

Count parts by connecting the sensor directly to the counter, and note the absurd value that appears. Add edge detection and repeat. Measure how many counts per second the error generates, and relate it to the measured scan time.

Lab 3 · Star-delta

Implement the complete starting sequence on a lab motor, with interlocks in the program and in the panel. Measure with a clamp meter the starting current in star and in direct delta, and compare. Verify the switching time with the oscilloscope.

Lab 4 · Conveyor tracking

Set up a conveyor with an entry sensor, an encoder and an ejector, and implement tracking with a shift register. Change the belt speed during operation and check that the sorting is still correct. Repeat with a timer instead of an encoder to see the drift.

06Common mistakes

MistakeConsequence
Counting without edge detectionThe counter increments once per scan: one part is counted dozens of times.
Expecting a TON to accumulate timeIt resets whenever the input is cut. To accumulate, a timebase counter is needed.
Counter without reset or without retentive memoryThe count is lost on power failure, or never returns to zero.
Part tracking by timerWith the belt at a different speed, parts are sorted wrongly.
Star-delta without arc extinction timeShort circuit between phases when switching.
Interlock only in the programA software error can close both contactors. There must be a physical interlock.
Traffic light with loose timersWorks until the first change of requirements, and after that there is no way to touch it.
Unnamed flags grouped at randomThe program becomes impossible to follow.

07Self-assessment

What is the difference between TON and TOF?

The TON delays activation: the output appears after the set time. The TOF delays deactivation: the output appears with the input and stays on for the set time after the input drops.

What is a TP for?

To generate a fixed-duration pulse for each input edge, regardless of how long the input lasts. For example, a half-second blast of air.

If the input of a 10 s TON drops at 8 s, what happens?

The count restarts from zero. The TON does not accumulate: for that you need a counter fed by a timebase.

Why must edge detection be placed before a counter?

Because without it the counter increments on every scan while the signal is active, and a single part is counted many, many times.

Which variables should go in a retentive memory area?

Those that represent accumulated production, setpoints and machine state: whatever should not be lost on a power failure.

Why is the shift register triggered by the encoder?

Because that way the register’s advance follows the actual movement of the belt. With a timer, any change of speed or a stop puts the information out of step with the parts.

With an encoder giving one pulse every 5 mm and a distance of 2.4 m, how many pulses?

2400 mm / 5 mm = 480 pulses.

Why is a delay needed between star and delta?

Because the star contactor needs time to extinguish the arc. Closing the delta earlier causes a short circuit between phases.

Is an interlock in the program enough?

No. There must also be a mechanical and electrical interlock between the contactors: safety cannot depend on software.

How is a flashing indicator generated?

With two chained timers—one for the on time and one for the off time—or directly with the clock flags that most PLCs already provide.

Development of the topic “Basic programming applications” of Industrial Electronics II (Year 7), based on the “Curriculum Proposal – Second Cycle of the Technical-Vocational Track, Secondary Education – Electronics,” Ministry of Education of the Province of Córdoba, DGETyFP. Back to the Topic Map · catto.ar