<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rohan Singh Raghav on Medium]]></title>
        <description><![CDATA[Stories by Rohan Singh Raghav on Medium]]></description>
        <link>https://medium.com/@rohansinghraghav4?source=rss-ce63b440da26------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9PNIVvKf-G6_fTetDVG4jQ.png</url>
            <title>Stories by Rohan Singh Raghav on Medium</title>
            <link>https://medium.com/@rohansinghraghav4?source=rss-ce63b440da26------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 15:35:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rohansinghraghav4/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Tank Fill & Discharge Control System with Factory I/O, CODESYS, and OPC UA]]></title>
            <link>https://medium.com/@rohansinghraghav4/building-a-tank-fill-discharge-control-system-with-factory-i-o-codesys-and-opc-ua-da2667d51e98?source=rss-ce63b440da26------2</link>
            <guid isPermaLink="false">https://medium.com/p/da2667d51e98</guid>
            <category><![CDATA[ladder-logic]]></category>
            <category><![CDATA[plc-programming]]></category>
            <category><![CDATA[opc-ua]]></category>
            <category><![CDATA[codesys]]></category>
            <category><![CDATA[factoryio]]></category>
            <dc:creator><![CDATA[Rohan Singh Raghav]]></dc:creator>
            <pubDate>Thu, 10 Sep 2026 09:02:22 GMT</pubDate>
            <atom:updated>2026-09-10T09:02:22.064Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A hands-on process control project simulating real-world level and flow control using ladder logic and an analog signal scaling function block.</em></p><h3>Introduction</h3><p>Industrial process control almost always comes down to the same core problem: raw field signals need to be converted into meaningful engineering values, acted upon by control logic, and communicated reliably between the PLC and the operator interface or SCADA layer.</p><p>To practice this end-to-end, I built a small but complete <strong>Tank Fill and Discharge System</strong> using three tools that mirror a real plant environment:</p><ul><li><strong>Factory I/O</strong> — a 3D industrial simulation environment that acts as the “physical” plant</li><li><strong>CODESYS</strong> — a soft PLC development platform for writing and running ladder logic</li><li><strong>OPC UA</strong> — the industrial communication protocol connecting the PLC to the simulated plant</li></ul><p>This post walks through the architecture, the logic, and the key engineering concept at the heart of the project: analog signal scaling.</p><h3>The Process Overview</h3><p>The simulated plant consists of a single tank equipped with:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uTreQc52oAtOGlSubg9Jvg.png" /></figure><ul><li><strong>Tank 1_FillValve</strong> — controls the inflow into the tank</li><li><strong>Tank 1_Discharge Valve</strong> — controls the outflow from the tank</li><li><strong>Tank1_LevelMeter</strong> — measures the water level</li><li><strong>Tank 1_Flow Meter</strong> — measures the discharge flow rate</li><li>A control panel with two potentiometers (<strong>Pot1_Fill</strong>, <strong>Pot2_Discharge</strong>) that act as manual operator setpoints for the fill and discharge valves</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*WTGWGAOVr6vPD1fnZeK-xw.png" /></figure><p>In short: an operator adjusts a potentiometer, the PLC scales that raw input into a valve command, and the valve responds — while the level and flow meters continuously report back the real-time tank state.</p><h3>System Architecture</h3><p>The three layers of the system communicate like this:</p><p><strong>Factory I/O (Plant/Simulation)</strong> ⇄ <strong>OPC UA Server (opc.tcp://localhost:4840)</strong> ⇄ <strong>CODESYS (PLC Logic)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r2ZrY735aWIq7jEvQASrvw.png" /><figcaption>OPC UA SERVER SYMBOL CONFIGURATION</figcaption></figure><p>The OPC UA server exposes all the relevant tags as nodes — Pot1_Fill, Pot2_Discharge, Tank1_FillValve, Tank1_DischargeValve, Tank1_LevelMeter, Tank1_FlowMeter, Display1_WaterLevel, and Display2_FlowRate — so Factory I/O and CODESYS exchange live data with no hardware in the loop. This is essentially a <strong>digital twin</strong> setup: the same architecture used to commission or test real PLC logic before it ever touches physical equipment.</p><h3>Ladder Logic Breakdown</h3><h3>1. Direct Pass-Through with MOVE</h3><p>The fill valve command is a simple direct assignment:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*y0NqQyEVTdN8gXAmNfvTDA.png" /></figure><pre>Pot1_Fill --[MOVE]--&gt; Tank1_FillValve</pre><p>The operator’s potentiometer value is written straight to the fill valve — no scaling needed here since both sides operate on the same range.</p><h3>2. Analog Scaling with a Custom Function Block</h3><p>The discharge valve, tank level, and flow meter all need their raw signals converted into meaningful engineering units. For this, I built a reusable <strong>FC_Scaling</strong> function block.</p><p><strong>Function Block Inputs:</strong></p><ul><li>Input — the raw signal</li><li>Input_Min, Input_Max — the expected range of the raw signal</li><li>Output_Min, Output_Max — the target engineering range</li></ul><p><strong>Function Block Outputs:</strong></p><ul><li>Scaled output value</li><li>Input_Error — a boolean flag raised if the input falls outside the expected range</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*lOXPf82hOoC5Vlblxqh7dw.png" /></figure><p><strong>The Scaling Formula</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*Op8BMhsFl7UTXp87XPSCbQ.png" /></figure><p>At the core of the function block is the standard linear scaling (min-max normalization) equation:</p><pre>Output = (Input − Min(Input)) / (Max(Input) − Min(Input)) × (Max(Output) − Min(Output)) + Min(Output)</pre><p>This single formula is reused three times in the program:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*W_-gv_Ep-hFR6YubuPXEmw.png" /></figure><p>Writing it once as a function block instead of duplicating the math three times keeps the program modular, easier to test, and easier to reuse in future projects — good practice for any real PLC codebase.</p><h3>3. Display Conversion</h3><p>Since the HMI displays expect integer values, each scaled REAL output is passed through a TO_INT conversion block before being written to the display tags:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*0o8mZ4uV7P6niIiZh1pFiQ.png" /></figure><pre>WaterLevel --[TO_INT]--&gt; Display1_WaterLevel<br><br>FlowMeter  --[TO_INT]--&gt; Display2_FlowRate</pre><p>This keeps the internal calculations in full floating-point precision while presenting clean, rounded values to the operator.</p><h3>Error Handling</h3><p>Every FC_Scaling call includes an Input_Error output wired to a dedicated boolean (Error1, Error2, Error3). If a raw signal ever falls outside its expected input range — a sensor fault, wiring issue, or out-of-range potentiometer value — the corresponding error flag is raised. In a production system, these flags would typically drive an alarm, log entry, or safety interlock.</p><h3>Why This Matters</h3><p>This project intentionally mirrors patterns used in real industrial control systems:</p><ul><li><strong>Signal conditioning</strong>: raw analog values are almost never in the units you actually need</li><li><strong>Modular logic</strong>: reusable function blocks reduce duplication and bugs</li><li><strong>Fault detection</strong>: range checking on every analog input</li><li><strong>Standardized communication</strong>: OPC UA as the bridge between control logic and supervisory/plant systems</li><li><strong>Digital twin testing</strong>: validating logic in simulation before deployment to physical hardware</li></ul><p><em>Built with Factory I/O, CODESYS, and OPC UA. connect with me!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da2667d51e98" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Box-Sorting System in CODESYS + Factory IO: A PLC Programming Project]]></title>
            <link>https://medium.com/@rohansinghraghav4/building-a-box-sorting-system-in-codesys-factory-io-a-plc-programming-project-ac9f14e08a8c?source=rss-ce63b440da26------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac9f14e08a8c</guid>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[codesys]]></category>
            <category><![CDATA[plc-programming]]></category>
            <category><![CDATA[industrial-automation]]></category>
            <category><![CDATA[engineering]]></category>
            <dc:creator><![CDATA[Rohan Singh Raghav]]></dc:creator>
            <pubDate>Fri, 04 Sep 2026 08:38:41 GMT</pubDate>
            <atom:updated>2026-09-04T08:38:41.257Z</atom:updated>
            <content:encoded><![CDATA[<h3>Why I Built This?</h3><p>Most PLC tutorials teach you individual instructions in isolation — a timer here, a counter there. But real industrial control systems are about how these pieces work <em>together</em> under real constraints: safety interlocks, sensor debounce, race conditions between competing set/reset signals, and the fact that a machine never stops moving just because your logic hasn’t caught up yet.</p><p>I wanted a project that forced me to think like a controls engineer, not just write ladder rungs. So I built a <strong>height-based box sorting station</strong>: a single incoming conveyor feeds a T-junction, where each box is classified by height and diverted onto one of two output conveyors — left or right — with counters tracking throughput on each side and a digital display showing total units processed. I programmed the logic in <strong>CODESYS</strong> and simulated the physical plant in <strong>Factory IO</strong>, so the PLC program had to control a real (virtual) mechanical system, not just toggle boolean lights.</p><h3>The Physical Layout</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9PNIVvKf-G6_fTetDVG4jQ.png" /><figcaption>FACTORY IO STATION</figcaption></figure><p>The station is a T-shaped roller conveyor:</p><ul><li>Boxes enter along the main line and pass a <strong>Pallet_sensor</strong> at the junction, along with High_sensor and Low_sensor mounted right at the crossover point to measure box height as it passes through.</li><li>Depending on the classification, the junction diverts the box onto either the <strong>Transf_left</strong> or <strong>Transf_right</strong> path, each leading to its own output conveyor (Conveyor_left / Conveyor_right), fenced off for safety.</li><li>At_left_entry and At_right_entry sensors sit just past each diverter to confirm the box has actually made it onto the correct branch.</li><li>A local <strong>control panel</strong> at the junction houses the Start, Stop, and Emergency pushbuttons plus status lamps — the physical HMI for the whole cell.</li><li>Safety fencing surrounds the entire junction and both output branches, consistent with how a real diverter station would be guarded.</li></ul><h3>Connecting CODESYS to Factory IO</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xKBdhFMRNmzf4Bmn4PSniQ.png" /><figcaption>OPC UA SERVER I</figcaption></figure><p>The two applications don’t talk to each other natively — the link is an <strong>OPC UA server</strong>, hosted locally at opc.tcp://localhost:4840, which Factory IO exposes and CODESYS connects to as an OPC UA client. Every physical sensor and actuator in the Factory IO scene is mapped to a named tag on that server, which I then bind directly to the corresponding variable in my CODESYS program:</p><ul><li><strong>Sensors → PLC inputs</strong>: At_left_entry, At_left_EXIT, At_right_entry, At_right_EXIT, High_sensor, Low_sensor, Pallet_sensor, Loaded, Emergency, Start, Stop, Reset — each one a boolean tag that mirrors a sensor beam or pushbutton in the 3D scene in real time.</li><li><strong>PLC outputs → actuators</strong>: Conveyor_entry, Conveyor_left, Conveyor_right, Transf_left, Transf_right, Load, plus Digital_Display and Counter as non-boolean values feeding the panel readout.</li></ul><p>This is the same integration pattern used on a real machine: the PLC never “knows” it’s driving a simulation — it just reads and writes tags on an industrial protocol. Getting the tag names to match exactly between the Factory IO driver and the CODESYS variable list was a small but important step; a single mismatched name here means a rung that looks correct in ladder logic and simply never fires in the simulation.</p><h3>System Overview</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*xVWSlPgYVSnJEH5GMzXhCw.png" /></figure><p>The process, end to end:</p><ol><li><strong>Start/Stop/Emergency</strong> at the control panel govern a master Running state for the whole line.</li><li>A box travels up the entry conveyor and trips the <strong>Pallet_sensor</strong> at the junction.</li><li>The pallet sensor confirms the box is present and latches a Load state.</li><li>As the box passes through the junction, the <strong>High_sensor</strong> / <strong>Low_sensor</strong> pair classifies it as short or tall.</li><li>Based on that classification, the box is routed onto the <strong>right transfer</strong> or <strong>left transfer</strong> path, each driven through an off-delay timer so the diverter mechanism has time to physically complete its motion before the drive signal drops.</li><li>At_left_entry / At_right_entry confirm arrival on the correct branch and reset that side&#39;s transfer latch.</li><li>Two <strong>up/down counters</strong> track how many boxes are currently on the left and right output conveyors — incrementing on entry, decrementing on exit at the far end.</li><li>A separate <strong>count-up counter</strong> tallies total boxes processed through the Pallet_sensor and drives a <strong>digital display</strong> on the panel.</li><li><strong>Emergency</strong> cuts through nearly every rung, forcing every actuator to a safe state regardless of what else is happening — matching the physical E-stop wired into the fenced enclosure.</li></ol><h3>The Logic, Rung by Rung (LADDER LOGIC)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yLHYW9qcBgJ43juPejogKw.png" /></figure><p><strong>Master Run Permissive (RS_0)</strong> A Set-Reset latch: Start sets Running, Stop or Emergency resets it. Every other part of the program gates off this bit — nothing moves unless the system is actually running.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*vzLX5JHPBWfFN6Gnoo4Diw.png" /></figure><p><strong>Entry Conveyor Control (RS_1)</strong> The entry conveyor runs while Running is true, resetting once a box registers at either At_left_entry or At_right_entry (rising-edge, pulse-triggered so they fire once per box rather than for the entire time a sensor is blocked). It also resets on Stop, once the box is Loaded, or on Emergency — preventing the conveyor from feeding a second box into a junction that&#39;s still processing one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*QqfmbCquaFmOkQSbMNbsAA.png" /></figure><p><strong>Load Detection (RS_2)</strong> Once Running and Pallet_sensor both go true, Load latches. It resets when Loaded fires downstream, or on Stop/Emergency. This is the handoff point between &quot;a box is physically at the junction&quot; and &quot;the system has acknowledged it and is ready to classify.&quot;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1011/1*sXKravxzRoy52Hm6n970_A.png" /></figure><p><strong>Height Classification (RS_3)</strong> This is the actual sorting decision, taken right at the junction. Low_sensor sets box_Type, High_sensor resets it. Whichever sensor trips first wins the classification for that box — a simple, robust way to encode a binary decision without needing an analog height measurement.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W_u8I8qEOKm5bt32XlDO3w.png" /></figure><p><strong>Right/Left Transfer (RS_4 + TOF_0, RS_5 + TOF_1)</strong> Each branch has a Set-Reset latch gated by loaded and the appropriate box_Type value, reset by a pulse from that branch&#39;s entry sensor (At_right_entry / At_left_entry) or by Emergency. The output feeds a <strong>TOF (timer-off-delay)</strong> block set to 1 second, so the diverter stays energized for a full second after the reset condition — giving the physical mechanism in Factory IO time to complete its stroke instead of losing power mid-motion. Emergency is wired in series on the output coil as a final safety cutout, independent of the latch logic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2KvlCO5w3RGY7k_LezIw3A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*RxNMN-_HJw7TjcE05R6zPQ.png" /></figure><p><strong>Conveyor Load Counters (CTUD_0, CTUD_1)</strong> Up/down counters: count up on a pulse when a box arrives at that branch’s entry sensor, count down on a pulse when it exits the far end of that conveyor. Reset and Emergency both force the counter back to zero. This gives a live count of exactly how many boxes are sitting on each output conveyor at any moment — useful for spotting a jam or an overfilled buffer before it backs up into the junction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*wAlpH2Y4O4CqHJ9MLm4xfw.png" /></figure><p><strong>Total Throughput Counter (CTU_0)</strong> A straightforward count-up counter on Pallet_sensor, reset by the panel&#39;s Reset button, preset to 100. Its current value feeds Digital_Display on the control panel — a running total of boxes processed, the kind of KPI you&#39;d expect a line operator to glance at.</p><h3>Why Factory IO</h3><p>CODESYS logic that only ever drives simulated booleans in a test bench doesn’t tell you much about whether your program actually works on a real mechanism. Factory IO gave me a 3D physical simulation of the T-junction, complete with roller conveyors, real sensor beams, and a diverter that has actual travel time — connected to CODESYS over the standard I/O mapping. That surfaced problems a desk-check never would have:</p><ul><li>Sensor pulses that needed edge-detection (P contacts) instead of level-detection, or the counters would over/undercount every time a box lingered across a sensor beam.</li><li>The need for the off-delay timer on the transfer outputs — without it, the diverter would lose drive power and stall mid-stroke before the box fully crossed onto the branch conveyor.</li><li>Race conditions between the entry conveyor restarting and the next box reaching the height sensors, which pushed me to add pulse-gating on At_left_entry / At_right_entry rather than relying on raw sensor state.</li><li>Getting the junction geometry and sensor placement (High_sensor / Low_sensor positioned exactly at the crossover) right so classification happened reliably before the diverter had to commit to a direction.</li></ul><h3>What I’d Improve Next</h3><ul><li>Replace the two discrete height sensors with an analog distance sensor and a comparator, to support more than two sorting classes.</li><li>Expand the panel’s single digital display into a full HMI screen with fault indication and live per-branch counts.</li><li>Move from hardcoded timer/counter presets to a small parameter table, so changeover between box types doesn’t require touching the logic.</li></ul><h3>Takeaway</h3><p>This project was less about any single instruction and more about the discipline of building a control system that has to survive contact with a moving, physical (if simulated) plant: sequencing, edge cases, and safety interlocks that can’t be an afterthought. If you’re learning PLC programming, I’d strongly recommend pairing it with a simulator like Factory IO — it’s the fastest way to find out which parts of your ladder logic only <em>look</em> correct.</p><p><em>Built in CODESYS, simulated in Factory IO. Happy to share the full project files or walk through any part of the logic — feel free to reach out.</em></p><p>#PLC #Automation #Industrial Automation #CODESYS #Engineering</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac9f14e08a8c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>