OpenPLC-with-LDmicro/C2/Implementing-OR-and-XOR-logic-gates/English

From Script | Spoken-Tutorial
Revision as of 13:34, 2 July 2021 by Nirmala Venkat (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue
Narration
Slide 1: Welcome to the spoken tutorial on Implementing OR & XOR logic gates.
Slide 2: Learning Objectives

How to implement logic gates

  • OR
  • XOR
In this tutorial, we will implement OR & XOR logic gates in LDmicro.
Slide 3: System Requirements
  • Ubuntu 18.04 OS
  • LDmicro
  • OpenPLC Mainboard
  • 24V, 2A SMPS
  • USBasp programmer
  • Traffic Light module
  • Switchboard module
To record this tutorial I am using:
  • Ubuntu Linux 18.04 operating system
  • LDmicro
  • OpenPLC Mainboard
  • 24V, 2A SMPS
  • USBasp programmer
  • Traffic Light module and
  • Switchboard module
Slide 4: Pre-requisites
  • Normal Contact and Coil
  • Negated Contact and Coil
  • To follow this tutorial, you should know the working of
  1. Normal Contact and Coil
  2. Negated Contact and Coil
  • If not, please refer to the relevant tutorials in this series on this website.
Slide 5: Prerequisites - Hardware setup


hardware-prerequisite.jpg

Connect SMPS and USBasp to the Mainboard as shown in the picture.

Keep these connections throughout this tutorial.

First we will see how to implement OR gate.
Slide 6: OR gate

OR gate outputs 0 only when all of its inputs are 0

We know that OR gate outputs 0 only when all of its inputs are 0.
Open LDmicro Let us open LDmicro.
Click Instructions -> Insert Contact

Place the cursor to the right of Xnew >> Click Instructions -> Insert Coil

Insert a Contact and a Coil as shown.
Double-click on Xnew

>> Type ‘switch1’ in the name box >> Click OK button

Rename the Contact as ‘switch1’.
Double-click on Ynew

>> Type ‘LED’ in the name box >> Click OK button

Then, rename the Coil as ‘LED’.
Place the cursor to the below Xswitch1

>> Click Instructions -> Insert Contact

Now place the cursor below Xswitch1 and insert a Contact.
Double-click on Xnew

>> Type ‘switch2’ in the name box >> Click OK button

Rename the new Contact as ’switch2’.
We will now check the working of this logic.
Click Simulate -> Simulation mode

>> Click Simulate -> Real-time simulation

Let us turn on the simulation mode.

For that, click Simulate and then on Simulation mode.

Next, start real-time simulation as shown.

Highlight the state of Xswitch and YLED Initially the states of both the Contacts and Coil are 0.
Double-click on Xswitch1 >> Highlight the state of the ‘YLED'

>> Double-click on Xswitch1

Change the state of Xswitch1 to 1.

We can observe the state of YLED is 1.

Change the state of Xswitch1 back to 0.

Double-click on Xswitch2

>> Highlight the state of the ‘YLED'

Now, change the state of Xswitch2 to 1.

We can observe the state of YLED is 1.

Double-click on Xswitch1

>> Highlight the state of the ‘YLED'

Now again change the state of Xswitch1 to 1.

We can observe that the state of YLED is 1.

That is, output state is 0 only when both the inputs states are 0.

Thus, resembling the truth table of an OR gate.

Click Simulate -> Halt Simulation

>> Click Simulate -> Simulation Mode

Now, turn off the simulation mode.

For that, click Simulate and then on Halt Simulation.

Then click Simulate and Simulation Mode.

Now let us compile the logic.

The detailed steps on how to compile and save the logic are explained in the earlier tutorials.

Click on Settings >> Click on Microcontroller

>> Select AVR ATmega16 40-PDIP

Click on Settings >> Click on MCU parameters >> Change Crystal frequency to 16

Click on Settings and select the microcontroller AVR ATmega16 40-PDIP.

Adjust its parameters as shown here.

Double-click on Xswitch1 in the I/O list>> Select PC0

>> Click OK button Double-click on Xswitch2 in the I/O list >> Select PC1 >> Click OK button


Double-click on YLED in the I/O list>> Select PA0 >> Click OK button

Assign pin PC0 to Xswitch1 and PC1 to Xswitch2.

Assign PA0 to YLED.

Compile Compile the logic as ‘orgate.hex’.
Save the file Save the file as ‘orgate.ld’ as shown.
Now we will see the working of this logic on hardware.
Connect Mainboard to PC using USBasp

laptop-usbasp.jpg


Connect the Mainboard to your laptop using USBasp.

Turn on the power supply.

Open terminal >> Type cd Desktop/LDmicro >> Press ENTER >>


type ‘avrdude -c usbasp -p m16 -U flash:w:orgate.hex’ >> Press ENTER

Open the Terminal by pressing CTRL+ALT+T keys simultaneously.

Go to the folder where you have saved the hex file.

Type the command as shown to upload the hex file to the Mainboard.

Remove the USBasp connection from the laptop. Turn off the power supply.

Remove the USBasp connection from the laptop.

This will prevent any hardware damage.

Display the image orgate.png Let us see the connection details now.
Display the image orgate.png Connect GND pin of the red LED of Traffic Light module to GND pin of the Mainboard.

Then connect the +5V pin of the red LED to PA0 pin of the Mainboard.

Display the image orgate.png Connect GND and 5V of Switchboard to GND and 5V of the Mainboard respectively.

Connect NO1 to PC0 of the Mainboard.

Connect NO2 to PC1 of the Mainboard.

Make the connections as shown in the picture.

Switch on the power Turn on the power supply.
Point to red LED We can see that the red LED is off initially.
Press any one switch

Press both the switches

It will start glowing when either one of the switches is pressed.

It will also glow when both of the switches are pressed at the same time.

Thus, for an OR gate the inputs i.e. Contacts should be parallel to each other.

The output i.e. Coil should be in series with both the inputs.

Switch off the power Turn off the power supply.

Remove the connections made for this example.

We will now learn about XOR gate.
Slide 6: XOR gate

Boolean equation:

  • A’B+A’B - ((NOT A) AND B) OR (A AND (NOT B))
  • It is a combination of NOT, AND and OR gates
We know the boolean equation of an XOR gate with inputs A and B will be AB’+A’B.

Thus it is a combination of NOT, AND and OR gates.

We will now see how to implement it on LDmicro.
Switch back to LDmicro Switch back to LDmicro.
Instead of creating a new file, we will make changes in the previous file itself.
Place cursor to the right of Xswitch1 >> Click Instructions -> Insert Contact >> Double-click on Xnew >> Check the Negated box >> Type switch2 in the name box >> Click OK button Insert a Contact to the right of Xswitch1.

Configure it as Negated and rename it as switch2 as shown.

Place cursor to the right of Xswitch2 >> Click Instructions -> Insert Contact >> Double-click on Xnew >> Check the Negated box >> Type switch1 in the name box >> Click OK button Then insert a Contact to the right of Xswitch2 which is in the parallel rung.

Configure it as Negated and rename it as switch1 as shown.

We will now check the working of this logic.
Click Simulate -> Simulation mode >> Click Simulate -> Real-time simulation Start real-time simulation as shown.
Highlight the state of Xswitch and YLED Initially the state of both the Contacts and Coil are 0.
Double-click on Xswitch1 >> Highlight the state of the ‘YLED' >> Double-click on Xswitch1 Change the state of Xswitch1 to 1.

We can observe the state of YLED is 1.

Change the state of Xswitch1 back to 0.

Double-click on Xswitch2 >> Highlight the state of the ‘YLED' Now, change the state of Xswitch2 to 1.

We can observe the state of YLED is 1.

Double-click on Xswitch1 >> Highlight the state of the ‘YLED' Now again change the state of Xswitch1 to 1.

We can observe that the state of YLED changes to 0.

That is the output state is 0 when both the inputs have the same state.

Thus, resembling the truth table of an XOR gate.

Click Simulate -> Halt Simulation >> Click Simulate -> Simulation Mode Turn off the simulation mode as shown.
Now let us compile the logic.
Highlight I/O list and status bar We can observe that the microcontroller and its parameters are already set.

Pin assignment is also already done.

Click on Compile >> Click on Compile As >> Go to Desktop/LDmicro folder >>


Rename it as ‘xorgate.hex’ >> Click on Save.

Click OK button

Compile the logic as xorgate.hex using ‘Compile As’ option.
Click on File >> Click on Save As >> Go to Desktop/LDmicro folder >>

Rename it as ‘xorgate.ld’ >> Click on Save

Then save the ladder diagram as ‘xorgate.ld’ using the ‘Save As’ option.
Now we will see the working of this logic on hardware.
Connect Mainboard to PC using USBasp

laptop-usbasp.jpg


Connect the Mainboard to your laptop using USBasp.

Turn on the power supply.

Open terminal >> Type cd Desktop/LDmicro >> Press ENTER >>


type ‘avrdude -c usbasp -p m16 -U flash:w:xorgate.hex’ >> Press ENTER

Switch back to the terminal.

Type the command as shown to upload the hex file to the Mainboard.

Remove the USBasp connection from the laptop. Turn off the power supply.

Remove the USBasp connection from the laptop.

This will prevent any hardware damage.

Display the image xorgate.png Let us see the connection details now.
Display the image xorgate.png

Switch on the power

The connections will be same as in the OR gate example.

Turn on the power supply.

Point to red LED Initially the red LED is off.
Press any one switch >> Press both the switches It will start glowing only when either of the switches are pressed, but not both.
Switch off the power Turn off the power supply.
This brings us to the end of this tutorial.

Let us summarize.

Slide 7: Summary

How to implement logic gates

  • OR
  • XOR
In this tutorial we learnt to implement logic gates
  • OR and
  • XOR
Slide 8: Assignment:

Implement 2 input XNOR logic gate

As an assignment, try to implement 2 input XNOR logic gate.

Truth table of XNOR gate is shown here.

Slide 9:

About Spoken Tutorial project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

Slide 10:

Spoken Tutorial workshops

The Spoken Tutorial Project team:
  • conducts workshops using spoken tutorials and
  • gives certificates on passing online tests.

For more details, please write to us.

Slide 11:

Forum for specific questions:

Please post your timed queries in this forum
Slide 12:

Forum for specific questions:

  • Do you have any general / technical questions on OpenPLC?
  • Please visit the FOSSEE forum and post your question.
Slide 13:

Acknowledgement

Spoken Tutorial Project is funded by MHRD, Government of India.
Slide 14:

Thank you slide

This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.


And this is Harsha Priyanka from FOSSEE team, signing off.


Thanks for watching.

Contributors and Content Editors

Nirmala Venkat