Exercises - Part 1

Basic exercises on Codesys

In the following exercises, consider declaring the variables as follows.

Variable typeData typeVariable declarationRemarks

Input, Output

Bool

bStart, bStop, bReset, bInput, bSensor, bInductiveSensor, bMotor, bSolenoid etc.

The first alphabet defines the datatye followed by variable name

Value

Int or Int16

iCount, iLevel, iSpeed etc.

Value

DInt or Int32

diCount, diLevel, diOrderID etc.

Value

Real

rSpeed, rLevel, rHeight etc.

Name

String

sID, sRecipe, sPassword etc.

Parameter

Time

tDelay, tTimeout etc.

Solve the following exercises in the following programming languages: - Ladder Diagram - FBD (Funcitonal Block Diagram)

Section 1 - Series & Parallel logics

  1. Write a program when bStart is TRUE, bMotor should be TRUE, and when bStart is False, bMotor should be FALSE.

  2. Write a program to fulfill the following boolean table:

bStartA :BOOL (Input)bStartB :BOOL (Input)bOutputX : BOOL (Output)

FALSE

FALSE

FALSE

FALSE

TRUE

FALSE

TRUE

FALSE

FALSE

TRUE

TRUE

TRUE

  1. Write a program to fulfill the following boolean table:

bStartA :BOOL (Input)bStartB :BOOL (Input)bOutputX : BOOL (Output)

FALSE

FALSE

FALSE

FALSE

TRUE

TRUE

TRUE

FALSE

TRUE

TRUE

TRUE

TRUE

  1. Write a program to fulfill the following boolean table:

bStartA :BOOL (Input)bOutputX : BOOL (Output)

FALSE

TRUE

TRUE

FALSE

  1. Write a program to fulfill the following boolean table:

bStartA :BOOL (Input)bOutputX :BOOL (OUTPUT)bOutputY : BOOL (Output)

FALSE

TRUE

TRUE

TRUE

FALSE

FALSE

  1. Write a program to fulfill the following boolean table:

bStartA :BOOL (Input)bOutputX :BOOL (OUTPUT)bOutputY : BOOL (Output)

FALSE

TRUE

FALSE

TRUE

FALSE

TRUE

  1. Write a program to fulfill the following boolean table:

bStartA :BOOL (Input)bStartB :BOOL (Input)bOutputY : BOOL (Output)

FALSE

FALSE

FALSE

TRUE

FALSE

TRUE

FALSE

TRUE

TRUE

TRUE

TRUE

FALSE

Section 2 - Latching and Interlocking logics using contacts

  1. Write a program when bStart (Input) is pressed, bMotor (output) should be latched & when bStop is Pressed, bMotor (output) should be unlatched. In this case, consider bStart and bStop as NO Push Button.

  2. Write a program when bStart (Input) is pressed, bMotor (output) should be latched & when bStop is Pressed, bMotor (output) should be unlatched. In this case, consider bStart and bStop as NC Push Button.

  3. Write a program to interlock bSolenoidA (Output) and bSolenoidB (output). - When bStartA (Input) is pressed, bSolenoidA (output) should be latched, and bSolenoidB should be unlatched - When bStartB (Input) is pressed, bSolenoidB (output) should be latched, and bSolenoidA should be unlatched - When bStop (Input) is pressed, both solenoids should be unlatched Consider bStartA, bStartB as NO push button, and bStop as NC Push Button.

Section 3 - RS and SR flip-flops

  1. Using RS flipflop write a program when bStart (Input) is pressed, bMotor (output) should be latched & when bStop is Pressed, bMotor (output) should be unlatched.

  2. Using SR flipflop write a program when bStart (Input) is pressed, bMotor (output) should be latched & when bStop is Pressed, bMotor (output) should be unlatched.

  3. Realize the difference in operations when both the inputs bStart and bStop are TRUE. Which type of flipflop should be used ideally in this situation?

Section 4- Use of Timers

TON: On-delay timer; TOF: Off-delay timer

  1. Write a program such that when bStart (Input) is TRUE, bMotor (Output) gets TRUE after a delay of 5 secs. When bStartis FALSE, bMotor should be FALSE.

  2. Write a program when bStart (Input) is TRUE, bMotorA (Output) gets TRUE after 3 seconds delay, and then after a delay of 3 more seconds, bMotorB (Output) should get TRUE & after a delay of 3 more seconds, bMotorC (Output) should get TRUE.

  3. Write a program if bStart (Input) is TRUE bMotor (Output) is TRUE for 10 seconds, then gets FALSE.

  4. Write a program such that: - bStart (Input) latches bMotorA (Output) - After a delay of 10 seconds, bMotorB (Output) latches - bStop (Input) unlatches bMotorA - After a delay of 10 seconds, bMotorB should get unlatched.

  5. Write a program in which bMotorA (Output) latches only when bStartB (Input) is TRUE within 10 seconds just after the bStartA (Input) gets TRUE. Otherwise, if bStartB is TRUE after 10 seconds, nothing should happen. Unlatch bMotorA when bStartA is FALSE.

Section 5- Use of Timers with Comparators

EQ: Equal NE: Not equal LT: Less than LE: Less than or equals GT: Greater than GE: Greater or equals

  1. Use one push button bStart (Input) actuate bMotor (Output) in the following sequence: bMotor is TRUE for 2 seconds, then FALSE for 2 seconds, then TRUE for 3 seconds, then continuously FALSE.

  2. Use bStartA (Input) to blink bLamp (Output) such that the On-time is 0.5 seconds and Off-time is 1 second.

  3. Write a program such that: - When bStartA (Input) is TRUE, bLampA (Output) and bLampB should blink continuously with a delay of 1 second. - When bStartB (Input) is TRUE, bLampA (Output) and bLampB should blink continuously with a delay of 2 seconds.

  4. Write a simple traffic light program to fulfill the following conditions. The Timer should reset, and the cycle should repeat when the time is above 25 seconds.

Time (in seconds)bRed (Output)bOrange (Output)bGreen (Output)

0 ~ 10

TRUE

FALSE

FALSE

11 ~ 12

TRUE

TRUE

FALSE

13 ~ 23

FALSE

FALSE

TRUE

24 ~ 25

FALSE

TRUE

TRUE

Section 6- Use of Counters with Comparators

CTU: Count Up CTD: Count Down

  1. Write a program such that when bStart is pressed three times, bOutput should get TRUE. And when bReset is TRUE once, the bOutput should be FALSE, and the counter should RESET.

  2. Write a program to fulfill the following conditions: - When bStart is pressed once -bOutputA should be TRUE, bOutputB and bOutputC should be FALSE - When bStart is pressed again -bOutputB should be TRUE, bOutputA and bOutputC should be FALSE - When bStart is pressed again -bOutputC should be TRUE, bOutputA and bOutputB should be FALSE - When bStart is pressed again - all the outputs should be FALSE, and the cycle should repeat itself

  3. UP/down counter exercise

Section 7- Use of variables with Math Operators

ADD: Addition SUB: Subtraction MUL: Multiplication DIV: Division MOV: Move

  1. Write a program to fulfill the following conditions: - bStart (Input) should latch the lamp bLamp (output) after five seconds delay. - bStop (Input) should unlatch the bLamp - Use bTimeA (Input) to change the delay time to 10 seconds. - Use bTimeB (Input) to change the delay time to 5 seconds.

Use the Conversion command if necessary.

  1. Write the same program as the above but instead of moving the constant time of 5 and 10 seconds. Use bInc (Input) to increment the time by 100ms and bDec (Input) to decrement the time by 100ms.

  2. Add a program to the solution of Exercise 7.2 to limit the minimum and maximum time to 1 second and 3 seconds, respectively.

  3. Write a program to blink the output bLamp with a delay of 1 second when bStart is TRUE.

Section 8- Other operators

SEL: Bitwise selection MUX: Multiplexer LIMIT: Limit Conversion:

  1. Write a program using SEL such that the bAlarm (Output) should be TRUE if the iLevel (variable) is more than 80%. Take iLevel as an integer with the range of 0 to 100%.

  2. Exercise with MUX needs to be created.

  3. Use the operand LIMIT in exercise 7.3 to define the limits and realize the result.

Last updated