Home > electrical-training-simulator > PLC programming lessons > lesson 7

PLC Programming Lesson 7: Dual Air Compressor Control

Build assist control, balance compressor usage, track service hours, reset totals, and initialize the application

Lesson objective: Design a dual-compressor pressure-control application that maintains an operating pressure range while providing an assist mode, a usage-tracking and sharing mode, a service-reset mode, and first-scan initialization.
Before you begin: Access to the PLCLogix 500 or PLCLogix 5000 training simulator is required to complete the hands-on lesson series. Complete the preceding lessons first if you are new to PLCLogix. The lesson screenshots use PLCLogix 500. Focus: subroutines, JSR/RET, TON/RTO, CTU, SUB and comparison logic, MOV, OTL/OTU, cleanup scans, and first-scan initialization.

Dual Air Environment and Control Panel

 

Lesson 7: In this lesson you will design an air-pressure control application step by step. A dual-compressor system maintains tank pressure between an upper and lower limit while compressed-air demand varies throughout the day. Hysteresis pressure switches control the compressor demand signals, and a three-position selector switch lets the operator choose between Mode A - Assist Compressor, Mode B - Usage Tracking and Sharing, and Mode C - Reset Tracking.

 

The application also uses the S:1/15 First Scan Bit (FSB) to execute a one-time initialization subroutine. This bit is true for the first scan only and will be used to set or clear application variables at startup. Use the PLCLogix Help menu if you want additional details on the First Scan Bit. The project is developed in 10 steps.

 

Assign the Dual Air simulator environment before continuing. Go to the 3D Worlds menu and select DualAir from the pop-up menu to associate the Dual Air environment with your program file. Then open I/O Mapping and confirm that the physical I/O addresses used by the environment match the I/O table in this lesson. The Dual Air environment includes the control panel, two compressors, and the air tank used throughout the exercise.

Use the numbered callouts with the list below to become familiar with the Dual Air control panel before programming.

The numbered callouts identify the control-panel items used in this lesson. Items 1-3 are addressable field devices used directly by the PLC program. Items 4-6 are operator-adjusted or displayed simulator values used to configure and test the process but are not addressed as PLC I/O in the program.

The control panel includes:

  1. (1) Start and Stop pushbuttons.
  2. (2) Three-position selector switch used to choose settings A, B, or C.
  3. (3) Run, C1, and C2 indicators showing system status and which compressor motor is operating.
  4. (4) MIN PSI and SPAN settings for the two pressure switches.
  5. (5) Current PSI display.
  6. (6) % FLOW setting used by the operator to vary system demand while testing.

 

Pressure-switch operating range:

Lower Operating Limit = MIN_PSI
Upper Operating Limit = MIN_PSI + SPAN

  1. At startup, the pressure switch is open.
  2. It remains open until tank pressure reaches the upper operating limit, then closes.
  3. It remains closed while pressure stays above the lower operating limit. If pressure falls below that limit, it opens again and remains open until pressure reaches the upper limit.

 

For this exercise, set MIN_PSI to 120 and SPAN to 20, producing an operating range of 120-140 psi.

I/O, Memory, and Application Requirements

 

Physical I/O
AddressNameDevice / Function
I:1/0Stop_PBN.C. push button
I:1/1Start_PBN.O. push button
I:1/2PS1Pressure switch (hysteresis)
I:1/3PS2Pressure switch
I:1/4Mode_ASelector switch - position A
I:1/5Mode_BSelector switch - position B
I:1/6Mode_CSelector switch - position C
O:2/0Compressor 1Motor control
O:2/1Compressor 2Motor control
O:2/2Run IndicatorGreen bulb
O:2/3Compressor 1 IndicatorGreen bulb
O:2/4Compressor 2 IndicatorGreen bulb
Data File Memory
AddressFilePurpose
B3:0/0B3 Bit FileVirtual Control Relay
B3:0/1B3 Bit FileRun Compressor 1 Flag
B3:0/2B3 Bit FileRun Compressor 2 Flag
B3:0/3B3 Bit FileMode A Flag
B3:0/4B3 Bit FileMode B Flag
B3:0/5B3 Bit FilePrimary/Secondary Flag
N7:0/1N7 Integer FileDifference C2 from C1
N7:0/2N7 Integer FileDifference C1 from C2

The exercise uses TON, RTO, RES, CTU, comparison, MOV, OTL, OTU, JSR, SUB, and RET instructions. Use PLCLogix Instruction Help for any instruction you need to review.

Example Requirements:

  1. Use a standard Start/Stop seal-in rung with a virtual control relay and a Run indicator.
  2. Use the selector switch to choose two operating modes plus a third service-reset mode.
  3. Mode A: one primary compressor maintains pressure. If it cannot restore pressure to the upper limit within the defined recovery time, the second compressor assists until maximum pressure is reached.
  4. Mode B: use retentive timers and counters to track compressor run time and automatically switch primary/secondary roles based on accumulated usage. Do not allow the difference in accumulated usage to exceed 200 hours, which is 10% of the 2000-hour service interval used in this exercise.
  5. Mode C: clear the RTO and CTU usage-tracking values after service.
  6. Add an initialization subroutine to clear or initialize the working memory and tracking instructions.

Steps 1-3: Build the Mainline Program

 

Step 1: Begin by deciding how the program should be organized. The three-position selector switch on the simulator control panel uses settings A, B, and C, so it is logical to give each operating mode its own subroutine. Keep the Start/Stop seal-in rung and the physical compressor motor-control rungs in the mainline program, and allow the active subroutine to determine which compressor is permitted to run and when.

 

This structure is important for more than neatness. Only one of the three mode subroutines should execute at a time, and keeping the physical output instructions in one mainline area helps avoid a dangerous programming conflict in which the same output or control element is instructed to be in two different states during the same scan cycle. That type of conflict can create unpredictable machine behavior.

 

Step 2: Create the standard Start/Stop seal-in rung and use a virtual control relay to enable the compressor application. The virtual control relay is then used on the two compressor motor rungs. The seal-in rung also controls the Run indicator on the field control panel. These first three rungs form the basic mainline portion of the application; the subroutines will provide the operating behavior required by each mode.

 

Step 3: Add a JSR instruction that executes when the Mode Selector is in position A. Enter SBR3 in the JSR file-number field. For now, leave that subroutine empty. Repeat the process for selector positions B and C, using SBR4 and SBR5. This ties the real-world selector positions on the simulator control panel directly to the program files that will implement each operating mode.

The initial mainline contains the seal-in rung, the two compressor-output rungs, and the JSR calls for selector positions A, B, and C.

At this point you should be able to start and stop the application, operate the Run indicator, and control both compressor motor/indicator pairs. The mainline is essentially complete except for the additional control contacts that will later determine when each compressor is allowed to run according to the selected mode and subroutine logic.

 

Mainline rung 0: Standard Start/Stop seal-in rung with the virtual control relay and Run indicator. The Run indicator remains on while the seal-in is holding.

Mainline rung 1: Controls the Compressor 1 Motor and Compressor 1 Indicator. At this stage, the state of Pressure Switch PS1 determines whether this pair of outputs is energized. The pressure switch responds to the actual simulated tank pressure. Additional contacts and branches will be added later to make Compressor 1 operation mode-dependent.

Mainline rung 2: Controls the Compressor 2 Motor and Compressor 2 Indicator. It performs the same basic function as rung 1, but uses PS2 and controls the second compressor.

Mainline rungs 3, 4, and 5: Direct program execution to one of three subroutines, each providing a different operating behavior or function. The physical selector switch can occupy only one position at a time and provides a break-before-make transition between settings, helping keep these mode-select rungs mutually exclusive.

Steps 4-5: Mode A - Assist Compressor

 

Step 4: Develop the Mode A subroutine in SBR3. Build the subroutines one at a time so each mode can be understood and tested independently. Use B3 status bits as flags that are set and reset inside the routine. In this lesson, B3:0/1 is the Run C1 Flag and B3:0/2 is the Run C2 Flag.

 

Mode A also uses a separate Mode A Flag. Its purpose is to guarantee a final cleanup scan when the operator moves the selector switch away from position A. This is important because program execution will eventually move to a different subroutine, but the existing mode must first be allowed to turn off or reset anything it was controlling.

 

Open SBR3 from the Project Tree and create the Mode A logic. Add a TON timer with a two-minute preset. The first compressor is the normal pressure-control compressor. If it runs continuously for more than two minutes without restoring pressure to the upper operating limit, the timer Done bit calls the second compressor to assist. This simulates a real situation in which unusually high air demand prevents one compressor from recovering system pressure quickly enough.

SBR3 implements Mode A: Compressor 1 runs first, and Compressor 2 is called for assistance only if the two-minute recovery timer expires.

SBR3 rung 0: Contains the SBR marker instruction. It is normally included for clarity, although it is not required for execution.

SBR3 rung 1: Sets a flag indicating that execution has entered SBR3. This flag is later used in the mainline to force one final pass through SBR3 after the selector switch opens the Mode A contact.

SBR3 rung 2: Performs the pressure-control and assist logic. When the pressure switch is open, meaning system pressure is below the desired range, the TON starts timing and the Run C1 Flag is energized. If the timer reaches its preset, the timer Done bit also energizes the Run C2 Flag. Both compressor flags then remain energized while pressure is below the desired upper limit. The Run C2 Flag cannot be energized until the timer has first expired. Also notice that when the Mode A selector contact is not closed, both compressor run flags are de-energized; that behavior is useful during the cleanup pass when changing modes.

SBR3 rung 3: Performs the cleanup function. The Mode A Flag is latched while this subroutine is active and is unlatched as soon as the selector is no longer in position A.

SBR3 rung 4: Unconditional RET, returning execution to the mainline program.

Why the cleanup pass is needed: If the selector leaves Mode A, the normal Mode A contact opens. Without another condition to execute SBR3 one last time, the program could never reach the rung that unlatches the Mode A Flag. The solution is to use the Mode A Flag itself as an alternate condition for calling SBR3. That lets the routine execute one final scan, clear its flag and run commands, and then stop executing until Mode A is selected again.

Step 5: Modify the mainline program to incorporate the Mode A behavior. The Run C1 and Run C2 flags now control the physical compressor motor rungs. The Mode A Flag provides the final cleanup pass and also prevents Mode B from starting until Mode A has completely finished its cleanup.

The mainline now uses the Run C1/Run C2 flags for compressor control and the Mode A Flag to complete cleanup before Mode B can begin.

Mainline rungs 1 and 2: Add the Run C1 Flag and Run C2 Flag contacts inline with the compressor motor-control rungs. These flags are currently set by SBR3; later, SBR4 will also be capable of changing their states.

Mainline rung 3: Add a branch around the Mode A selector contact using the Mode A Flag. When the selector leaves A, the physical Mode A contact opens, but the latched Mode A Flag keeps SBR3 executing for one final scan. During that final pass, SBR3 unlatches the Mode A Flag. When execution returns to the mainline, the flag is off and SBR3 no longer executes.

Mainline rung 4: Add a normally closed Mode A Flag contact to the Mode B JSR rung. During a transition from A to B, this prevents SBR4 from executing until the final cleanup pass through SBR3 has been completed.

Steps 6-8: Mode B - Usage Tracking and Sharing

 

Step 6: Create the Mode B subroutine in SBR4. Start by adding the same latch/unlatch pattern used in Mode A, this time for the Mode B Flag, so the routine can receive a cleanup pass when the operator leaves selector position B.

 

Mode B must also track total compressor usage. Use one RTO retentive timer / CTU counter pair for each compressor. The RTO accumulates the amount of time that a compressor is running. Set each RTO for one hour. When the timer reaches one hour, its Done bit increments the associated CTU hour counter and energizes a RES instruction to clear the timer accumulator so the next hour can be measured. In this way, the CTU becomes a running total of operating hours while the RTO measures the current partial hour.

The initial SBR4 logic pairs one RTO with one CTU for each compressor so total operating hours can be accumulated.

SBR4 rung 0: Contains the SBR marker instruction.

SBR4 rung 1: Sets the Mode B Flag to show that execution has entered SBR4. The mainline later uses this flag to give SBR4 one last scan after the Mode B selector contact opens.

The final Mode B logic uses B3:0/5 to select the primary compressor and keeps the usage timers working in tandem with that selection.

SBR4 rungs 2 and 3: Track compressor usage. When the associated pressure switch calls for a compressor, the corresponding RTO accumulates run time. Once one hour has accumulated, the timer Done bit increments the CTU hour counter and resets the RTO so another hour can be accumulated. Rung 3 duplicates the same function for the second compressor. Set the hour-counter preset to 2000 hours, which represents the service interval used in this exercise.

SBR4 rung 4: Cleanup rung that unlatches the Mode B Flag when the selector switch is no longer in position B.

SBR4 rung 5: Unconditional RET.

 

At this point: Both timer/counter pairs can still accumulate because only the pressure switches are controlling them. That is intentional at this stage. The next steps add control over which compressor is primary so the timers work in tandem with the actual compressor selection.

Step 7: Add logic to compare accumulated usage between the two compressors. Use SUB instructions to calculate both possible differences and comparison instructions such as LES, GRT, or GEQ as appropriate to change the B3:0/5 Primary/Secondary Flag. Use the convention LOW = Compressor 1 primary / Compressor 2 secondary and HIGH = Compressor 2 primary / Compressor 1 secondary.

 

Insert the two new comparison rungs above the previous cleanup rung. The old rung 4 and rung 5 therefore move down as the new rungs become rungs 4 and 5. This is a useful reminder that rung numbers can change as a program is developed; the logic and instruction relationships are more important than memorizing an earlier rung number.

The two SUB/comparison rungs switch the B3:0/5 Primary/Secondary flag when accumulated usage differs by 200 hours.

SBR4 rung 4: Subtract Compressor 2 accumulated hours from Compressor 1 accumulated hours and store the result at N7:0/1. Compare the result to the allowable 200-hour difference. When Compressor 1 has accumulated 200 more hours than Compressor 2, latch the Primary/Secondary Flag so Compressor 2 becomes primary.

SBR4 rung 5: Perform the opposite subtraction, storing the result at N7:0/2. When Compressor 2 has accumulated 200 more hours than Compressor 1, unlatch the Primary/Secondary Flag so Compressor 1 becomes primary.

 

Step 8: Finish Mode B by using the Primary/Secondary Flag to determine which compressor actually operates, and by completing the mode-transition interlocks in the mainline. This step is easiest to understand in two parts.

 

Part 1 - mainline subroutine calls: Modify the SBR3 and SBR4 call rungs so Mode A and Mode B are mutually exclusive even at the single-scan level. This ensures one mode is completely wrapped up before the other starts.

The Mode A and Mode B JSR rungs now include the virtual control relay and cleanup interlocks so one mode finishes before the other begins.

Mainline Mode A call rung: Add a Mode B Flag contact so Mode A cannot execute until the final pass through SBR4 has cleared the Mode B Flag. Also add the virtual control relay contact so the operating subroutines do not execute before the Start button has been pressed; this prevents timers from accumulating while the application is not running.

Mainline Mode B call rung: Add a branch creating an OR condition with the Mode B Flag so SBR4 can receive its final cleanup pass after the selector leaves position B. Add the virtual control relay here as well so SBR4 executes only after the application has been started.

 

Part 2 - compressor selection and tracking: Add a rung in SBR4 that uses the B3:0/5 Primary/Secondary Flag to select which compressor is primary. The branch provides the two mutually exclusive choices. Then add the same flag logic to the RTO timing rungs so compressor usage tracking follows the compressor that is actually selected to run.

SBR4 rung 6 uses B3:0/5 to select the compressor according to the Primary/Secondary flag.

SBR4 rung 6: Selects the compressor to be used according to the state of the B3:0/5 Primary/Secondary Flag.

SBR4 rungs 2 and 3: Add a mutually exclusive Primary/Secondary contact pair - a normally closed contact on one rung and a normally open contact on the other - so only the selected compressor's timer/counter pair is active in Mode B.

 

At this point the mainline and the functional operating subroutines are complete. Only the RESET and INITIALIZATION subroutines remain. These two routines are intentionally similar: Reset is used after service, while Initialization runs automatically when the controller enters RUN mode. In a larger application, a programmer might avoid duplicated logic by making the RESET routine a nested subroutine called by Initialization. Because this example duplicates only a small amount of logic, the lesson keeps the two routines separate so their purpose remains easy to see.

Step 9: Mode C - Reset Usage Tracking

 

Step 9: Create a simple reset subroutine in SBR5. This routine executes whenever the selector switch is placed in Mode C. No compressors should run in this mode. Its purpose is to reset the RTO timers and CTU counters used to track Compressor 1 and Compressor 2 usage after the equipment has been serviced.

 

Because SBR5 does not control the compressor motors, no additional mainline motor-control changes are required. Use RES instructions to clear the accumulated values in the four usage-tracking instructions.

SBR5 is the service-reset routine: selecting Mode C clears the compressor usage-tracking timers and counters.

SBR5 rung 0: Subroutine-open SBR marker. It is not required for execution, but including it is good programming practice and makes the routine's structure clear.

SBR5 rung 1: Uses RES instructions to clear the four timer/counter instructions used for compressor usage tracking.

SBR5 rung 2: Unconditional RET marking the end of the subroutine. Like the SBR marker, an unconditional RET is not strictly required here, but it is good practice and makes the return point explicit.

Step 10: First-Scan Initialization

 

Step 10: Finish the application by creating an initialization routine that runs once when the controller enters RUN mode. The S:1/15 First Scan Bit is true only during the first scan, making it useful for clearing file memory and instruction accumulators to known starting values.

 

Insert a new rung ahead of the original Start/Stop seal-in rung in the mainline. Add the S:1/15 First Scan Bit contact and a JSR instruction that calls SBR6. Because this bit is true for only one scan, SBR6 executes once at startup and then stops being called.

 

Inside SBR6, add the standard SBR and RET markers. Use RES instructions to initialize the two RTO/CTU tracking pairs. Use MOV instructions with a source value of zero to clear the B3 and N7 working-memory locations used by the application, including the virtual-control/flag memory and the two usage-difference integer locations.

The first-scan JSR calls SBR6 once when RUN mode begins; SBR6 resets the usage-tracking instructions and clears the working memory to known values.

Mainline first-scan rung: The S:1/15 contact closes on the very first scan. The JSR calls SBR6 one time so startup values can be cleared before normal operation proceeds.

Initialization rung 0: SBR marker.

Initialization rung 1: RES instructions clear the timer/counter pairs used for compressor usage tracking.

Initialization rung 2: MOV instructions send a zero value to the B3 and N7 working-memory locations used in the application.

Initialization rung 3: Unconditional RET.

That completes the design. The program is now ready to be tested against changes in pressure demand and mode selection.

Testing the Dual Air Compressor Application

 

Save the completed program before testing. Confirm that the file is still associated with the DualAir 3D World so the control panel, tank pressure, compressor status, and % FLOW demand control are available during the test.

 

  1. Set MIN PSI to 120 and SPAN to 20 as used throughout the example.
  2. Initially set % FLOW to zero while the system initializes and builds pressure to the desired maximum of 140 psi.
  3. Go online and run the application. Press the normally open Start pushbutton on the Dual Air control panel and watch system pressure climb.
  4. After pressure reaches 140 psi, vary % FLOW to simulate changing air demand. In Mode A, increase demand enough that the primary compressor cannot recover pressure within the configured two-minute assist delay; verify that the secondary compressor is then called to assist.
  5. Test both operating modes. In Mode B, verify that the selected primary compressor and its usage tracking change appropriately as the Primary/Secondary flag changes. Select Mode C and confirm that the usage-tracking totals are cleared for the next service interval.
Testing tip: Use the Dual Air 3D environment shown throughout this lesson. The % FLOW control lets you change demand in real time, while the PSI display and C1/C2 indicators let you observe how the control program responds.
Self-assessment: There is no separate online test for this lesson. Your completed PLC program and correct simulated behavior are the assessment. Verify pressure control, Mode A assist operation, Mode B compressor sharing and usage tracking, Mode C reset, and first-scan initialization.
Assigned by an instructor or supervisor? Save your finished PLC program and share it with them when you complete the lesson.

Need access to the required simulator? PLCLogix 500 Simulator   |   PLCLogix 5000 Simulator