OpenPLC-version1-with-LDmicro/C3/PWM-Instruction/English

From Script | Spoken-Tutorial
Revision as of 14:03, 29 December 2020 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 PWM Instruction
Slide 2:

Learning Objectives

  • PWM (Pulse Width Modulation) instruction

In this tutorial we’ll learn about working of

  • Pulse Width Modulation instruction
Slide 3: System Requirements
  • Ubuntu 18.04 OS
  • LDmicro
  • OpenPLC V1 Mainboard
  • 24V, 2A SMPS
  • USBasp programmer
  • Heater module
To record this tutorial I am using:
  • Ubuntu Linux 18.04 operating system
  • LDmicro
  • OpenPLC version 1 Mainboard
  • 24V, 2A SMPS
  • USBasp programmer
  • Heater module
Slide 4: Pre-requisites
  • Heater module
  • Working of Move instruction.

If not, please refer to the relevant tutorials from Home | spoken-tutorial.org

To follow this tutorial, you should know about
  • Heater module and
  • Working of Move instruction.

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.

Slide 6a: PWM instruction
  • Can set the duty cycle of the PWM peripheral built into certain microcontrollers
  • Duty cycle must be a number between 0 and 100
This sets the duty cycle of the PWM peripheral built into certain microcontrollers.

Duty cycle must be a number between 0 and 100.

Slide 6b: PWM instruction
  • PWM frequency specified might not be exactly achievable.
  • It depends on microcontroller's clock frequency
We can specify the PWM frequency in Hertz.

The frequency specified might not be exactly achievable.

It depends on how it divides into the microcontroller's clock frequency.

Show image of heater module Here, we will create a logic to control the speed of a fan using PWM.

We will use the fan in the Heater module.

Open the LDmicro from the launcher bar Let us open LDmicro.
Click Instructions -> Insert Contact >> Place the cursor to the right of the contact >> Click Instructions -> Insert MOV Insert a Contact.

Place the cursor to the right of the Contact and insert a Move instruction.

Double-click on Xnew >> Type switch1 in the name box >> Click the OK button Rename the Contact as ‘switch1’.
Double-click on the MOV >> Set Destination to dc >> Set Source to 20 >> Click the OK button In the move instruction set the variable dc to 20.

Click on the OK button.

Click Edit -> Insert rung below >> Click Edit -> Insert rung below Now, insert a rung below the current rung.
For both the rungs

Click Instructions -> Insert Contact >> Place the cursor to the right of the contact >> Click Instructions -> Insert MOV

In the new rung insert a Contact and a Move instruction as shown.
Double-click on Xnew >> Type switch2 in the name box >> Click the OK button

Double-click on the MOV >> Set Destination to dc >> Set Source to 60 >> Click the OK button

Rename the Contact as ‘switch2’.

In the Move instruction set the variable dc to 60.

Click Edit -> Insert rung below >> Place the cursor in the new rung Insert one more rung below.
Insert Instructions -> Analog Operations -> Insert Set PWM output Now, insert a PWM instruction from ‘Analog Operations’ as shown here.
Double-click on PWM Double-click on it.
Highlight two columns We can find two columns.
Highlight Duty cycle variable

Type dc

First one is the duty cycle variable.

Set it to variable ‘dc’.

Highlight Frequency Second is Frequency in Hertz.

By default, it is set to 1kHz i.e. 1000Hz.

Do not change it.

Click the OK button Click the OK button.
Highlight dc type in the I/O list In the I/O list we can see the variable ‘dc’ is of type PWM out.
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 state column in the IO list We can observe initially the states of all the instructions are 0.
Double click on Xswitch1 Change the state of Xswitch1 to 1 by double-clicking on it.
Highlight the value of dc The variable dc is set to 20.

Highlight the value of dc

The dc variable value is set as the duty cycle of the PWM peripheral.

This happens when the input condition of PWM instruction is true.

In our case, it is true as the PWM instruction is connected to the positive rail.

Double click on Xswitch1 Change the state of Xswitch1 back to 0.
Double click on Xswitch2

Highlight the value of dc

Double click on Xswitch2

Similarly, when Xswitch2 state is 1 the duty cycle will be set to 60.
Make sure that at a time only one of the Contacts has its state 1.
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.
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.

Highlight ‘dc’ in the I/O list We can see that variable dc is already assigned pin number 21, which is PD7.

This is the PWM pin for ATMega16 microcontroller.

Double-click on Xswitch1 in I/O list >> Select PC7 >> Click OK button

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

Now assign PC0 to Xswitch1 and PC1 to Xswitch2 as shown.
Click on Compile >> Click on Compile >> rename it as ‘pwm.hex’ >> Go to Desktop/LDmicro folder >> Click on Save.

Click OK button

Compile the logic as pwm.hex
Click on File >> Click on Save >> Rename it as ‘pwm.ld’ >> Go to Desktop/LDmicro folder >> Click on Save Save the ladder diagram as pwm.ld
We will now 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:pwm.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.

pwm.png Let us see the connection details now.
pwm.png Power the Heater module through relimate connectors from the Mainboard.

Connect PD7 to the fan pin of the MCU pins of the Heater module.

pwm.png Connect GND and 5V of the Switchboard to GND and 5V of the Mainboard respectively.

Then connect NO1 to PC0 and NO2 to PC1 of the Mainboard.

Make the connections as shown in the picture.

After making all the connections properly, turn on the power supply.
Initially the fan will be off as the duty cycle is 0.

The FANon LED on the Heater module would be glowing dim.

Press the switch NO1.

We can observe the fan is turned ON.

The duty cycle is 20 here.

Now press the switch NO2.

We can observe the speed of the fan is increasing comparatively.

This is because of the increase in the duty cycle to 60.

Thus we have controlled the speed of the fan using PWM.
Turn off the power supply.
This brings us to the end of this tutorial.

Let us summarize.

Slide 8: Summary
  • PWM instruction
In this tutorial we learnt about working of * PWM instruction
Slide 9:

Assignment:

As an assignment,

Control the brightness of the red LED of Traffic Light module.

Hints: Replace fan of Heater module with red LED of Traffic light module. Connect PD7 pin to +5V pin of the red LED of the Traffic Light module.

Slide 10:

About Spoken Tutorial project

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

Please download and watch it.

Slide 11:

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

Nancyvarkey, Nirmala Venkat