Sorting Machine PLC Program & Ladder Diagram
This page covers the full sorting machine PLC program and ladder diagram — metal-versus-plastic part sorting using an inductive sensor, a pneumatic reject gate, a CTU production counter, and a safety interlock that keeps the gate from firing while a part still breaks the entry beam. It is grounded in a live scenario you can run directly in your browser: write the ladder, watch parts travel and sort, and get instant pass/fail feedback — no PLC hardware, no software install. It is runnable and auto-graded free.
sensorscounterssorting
How a sorting machine PLC program works
A sorting machine moves mixed parts along a belt and diverts one category onto a reject lane while letting the rest pass straight through. In this scenario the discriminator is material: metal parts must be rejected, plastic parts pass. Object sorting using a PLC comes down to four jobs — run the belt, detect the part type, fire a diverter at the right moment, and count what was sorted.
The belt is the foundation. START energises BELT and STOP de-energises it — a standard motor start/stop you can build with a SET/RESET pair on BELT driven by START (set) and STOP (reset).
Detection uses two sensors in sequence. PART_PE (%I0.2) is an entry photo-eye whose beam is broken by any arriving part — metal or plastic. Further down the line, METAL_SENSOR (%I0.3) is an inductive sensor that fires only for metal: plastic parts pass it without ever producing a signal. That is the whole material discrimination — no separate plastic branch is needed, because plastic simply never trips the inductive sensor.
The diverter is REJECT_GATE (%Q0.1), a pneumatic solenoid that pushes a part onto the reject lane. REJECT_LIMIT (%I0.4) is the limit sensor at the reject lane that confirms a part has been pushed across. Finally, METAL_COUNT_LAMP (%Q0.2) lights once five metal parts have been rejected. Five inputs, three outputs — all pre-wired for you.
Metal-vs-plastic sorting with an inductive sensor and reject gate
The sort logic is one latch. SET REJECT_GATE when METAL_SENSOR detects metal; RESET REJECT_GATE when REJECT_LIMIT confirms the part has reached the reject lane. While the gate is set, the pneumatic arm is extended and pushes the metal part off the main belt; once REJECT_LIMIT trips, the part is clear and the gate retracts ready for the next one.
This is the elegant part of inductive sensor PLC sorting: the same single rung handles both materials. A metal part trips METAL_SENSOR, so the gate fires and diverts it. A plastic part never trips METAL_SENSOR, so the gate stays off and the part rides straight through to the end of the belt. You do not write a 'plastic' rung at all — the absence of an inductive signal is the plastic case.
Two test cases pin this down. The discriminating 'metal-part-diverts' case injects a metal part and checks the gate is still off before the part reaches METAL_SENSOR (~2 s in), energised after METAL_SENSOR fires (~4 s), and dropped again after REJECT_LIMIT (~6 s) — and that the physics counted exactly one rejected metal part. The 'plastic-part-passes' case injects a plastic part, lets it traverse the entire belt, and asserts REJECT_GATE never energised and the metal-rejected count stayed at zero. Get the SET sensor wrong — fire on PART_PE instead of METAL_SENSOR — and the plastic test fails because the gate would divert everything.
Counting rejects with a CTU — lighting the lamp after five metal parts
Once the gate is sorting correctly, the metal sorting PLC program needs a production count: light METAL_COUNT_LAMP after five metal parts have been rejected. This is a textbook CTU (count-up) application.
Add a CTU with its preset (PV) set to 5. Drive the count-up input from REJECT_LIMIT — but through a rising-edge detector. REJECT_LIMIT is a level signal: it stays true for as long as a part is physically over the limit sensor. Feed that level straight into the CTU and the counter would increment on every scan the sensor is blocked, adding dozens of spurious counts per part. An R_TRIG (or equivalent one-shot) converts the level to a single pulse, giving you exactly one count per rejected part.
Wire the CTU's done output (CTU.Q in IEC, CTU.DN in Allen-Bradley) to METAL_COUNT_LAMP. When the accumulator reaches 5 the done bit goes true and the lamp lights.
The 'counter' test injects five metal parts spaced seven seconds apart — wide enough that each part clears the entry beam before the next reaches the inductive sensor. It asserts METAL_COUNT_LAMP is still off after the fourth part has been rejected and on after the fifth, and it checks the physics engine's metalRejectedCount is exactly 5. Because plastic parts never reach the reject lane, they never trip REJECT_LIMIT and never inflate the count — the counter inherently tallies only metal.
The safety interlock — never fire the gate while the entry beam is broken
Every diverter on a sorting line has the same mechanical hazard: if the reject gate extends while a part is still partly over the arm's pivot, the arm slams into the part and jams the machine. The safety rule, stated as an objective, is that REJECT_GATE must never energise while PART_PE — the entry photo-eye — is still broken.
The scenario's belt spacing makes the safe design natural rather than fiddly. By the time a part travels far enough down the belt to reach METAL_SENSOR, it has already cleared the entry photo-eye, so PART_PE is no longer broken. That is exactly why the sort latch is built to SET REJECT_GATE on METAL_SENSOR and not on PART_PE: firing on the inductive sensor guarantees the part is already past the entry beam. If instead you fired the gate the instant PART_PE detected a part, the arm could collide with the part while it is still entering — the violation the interlock exists to prevent.
The 'safety-invariant' test injects a metal part, lets it run the full length of the belt past REJECT_LIMIT, and then asserts the physics flag rejectWhileEntryViolation is false — i.e. at no point during the run was the gate energised while PART_PE was still broken. A solution that sorts correctly on count and material but fires the gate too early will pass the divert and counter tests yet fail this one.
The whole sorting machine PLC ladder diagram is runnable and auto-graded on this page. Write it, press Run, and watch metal parts divert onto the reject lane while plastic parts pass — with five automated test cases grading the belt, the metal divert, the plastic pass-through, the five-count lamp, and the entry-beam safety interlock. No physical PLC, conveyor or sensors required.