Arduino/C2/Pulse-Width-Modulation/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Slide 1: Welcome to the spoken tutorial on Pulse Width Modulation.
Slide 2:

Learning Objectives

In this tutorial we will learn about:
  • PWM i.e Pulse Width modulation
  • PWM Duty Cycle
  • PWM Frequency
  • L293D Motor Driver IC
Slide 3:

Pre-requisites

To follow this tutorial, you should have basic knowledge of:
  • Electronics and
  • C or C++ programming language
Slide 4:

System Requirements

To record this tutorial, I am using
  • Arduino Uno board
  • Ubuntu Linux 16.04 OS
  • Arduino IDE
Slide 5(A):

External Components Required

We will also require some external components such as:
  • Breadboard
  • 10K Ohm Potentiometer
  • LED
  • 220 ohm Resistor
  • Jumper Wires
  • Push Button
Slide 5(B) :

Image DC-motor.jpg

DC Motor
Slide 5(C) :

Image L293D.jpg

L293D Motor Driver IC
Slide 6:

Pulse Width Modulation

  • PWM signal is a square wave signal which has a high frequency i.e (1KHz).
  • PWM is a technique by which the width of the pulse is varied.
  • It is done while keeping the frequency of wave constant.
Slide 7:

Pulse Width Modulation

  • PWM signal consists of two main properties that define its behaviour.
  • They are Duty Cycle and Frequency.
Slide 8:

Image duty_cycle.jpg

It is the percentage of time, a digital signal is on, over a period of time.

Duty cycle can be varied from 0% to 100%..

Slide 9:

Formula - Duty cycle

Image duty_cycle_formula.jpg

The formula to calculate the percentage of duty cycle is shown here.
  • tON is equal to the duration of time when signal is high.
  • tOFF is equal to the duration of time when signal is low.
  • Time Period is tON + tOFF.
  • i.e. It is equal to the sum of on time and off time of PWM signal.
Slide 10:

PWM frequency

  • Frequency determines how fast the PWM completes a cycle.
  • i.e. How fast it switches from HIGH to LOW states.
Slide 11:

Example - Duty Cycle

We will perform one simple experiment by varying duty cycle.

This will control the brightness of LED.

Slide 12:

Image Arduino_PWM_pins.png

Arduino Uno has 6 PWM channels.

Pins 3, 5, 6, 9, 10, 11 on Arduino Uno are PWM channels.

PWM channels are denoted by tilde sign.

narration Let us see the circuit connection now.
Slide 13:

Image LED_Connection.png

Connect anode leg of the LED to pin 9 of Arduino through 220 ohm resistor.


Connect cathode leg of the LED to ground.

Image Live Setup

LED_connection_setup.png

This is the live setup of the connection.


Do the connection as shown in the image.

Open Arduino IDE Let us open the Arduino IDE.

We will write a program to change the brightness of LED using PWM pin.

int LED_Pin = 9;

int duty_cycle_value = 1;

Type the code as shown.

We have assigned the PWM pin 9 to the variable LED_Pin.


We have initialized duty_cycle_value as 1 for an LED to turn ON.

void setup()

{

pinMode( LED_Pin, OUTPUT);

}

Inside void setup, we will write pinMode function.

We have declared pin 9 of the Arduino as OUTPUT.

Highlight according to narration Inside void loop function we will write this code.

Let me explain the code.

While loop executes the code till the duty_cycle_value is below 255.

analogWrite() function is used to generate PWM signal.

We are passing two parameters. i.e PWM pin number and the duty cycle value.

The duty cycle value must be between 0 to 255 i.e between (0v) and (5V)

We will keep a delay of 3000 millisecond i.e. 3 seconds

Compile and save the program Click on the compile button to verify your program.

A pop up window will appear to save the current program.

Let us save the program as LED_Brightness and click on the Save button.

Now click on upload button to upload the current program on Arduino.

Live Output Video

LED_output.mp4

We can see the brightness of LED increasing gradually.
Next, we will do an experiment to control the speed and direction of a DC motor.
Slide 14:

Image L293D_Pinout.png

This is the pinout diagram of L293D motor driver IC.

The speed of the motor is controlled by EN 1 and EN 2 of the IC.

The direction of the motor is controlled by IN1, IN2, IN3, IN4 of the IC.

We can control 2 motors at a time using this IC.

In our experiment, we will connect only one DC motor.

Let us see the circuit connection now.
Slide 15:

Image Speed-Direction-Control-Connection.png

  • Pin 1, pin 8 and pin 16 of driver IC are connected to 5V.
  • Pin 4 and pin 5 of driver IC are connected to ground.
  • Pin 2 and pin 7 of driver IC are connected to pin 11 and pin 10 of Arduino.
  • 2 push buttons are connected to pin 12 and pin 13 of Arduino.
  • These push buttons are used to control the direction of DC motor.
  • 10Kohm potentiometer is connected to control the speed of the DC motor.
  • Middle pin of potentiometer is connected to analog pin A0.
  • Pin 3 and pin 6 of driver IC are connected to DC motor.

Do the connection as shown in the image.

Slide 16:

Image Live Setup

This is the live setup of the connection, as shown in the image.

I have fixed a wheel on the shaft of the motor.

This will help to see the rotation and varying speed of motor clearly.

Now we will write a program for this circuit to work.

Switch to Arduino IDE Let’s switch to Arduino IDE.
Highlight the code


const int potPin = A0;

const int fwdbuttonPin = 13;

const int bckbuttonPin = 12;

const int ICpin2 = 11;

const int ICpin7 = 10;

Type the code as shown here.

We have initialized the connection between Arduino and driver IC.

Potentiometer pin is connected to analog pin A0.

fwdbuttonPin is the variable for push button connected to pin 13 of Arduino.

bckbuttonPin is the variable for push button connected to pin 12 of Arduino.

ICpin2 and ICpin7 are the variables which indicate pin 2 and pin 7 of IC.

They are connected to pin 11 and pin 10 of Arduino respectively.

int potValue = 0;

int motorValue = 0;

int fwdbuttonState = 0;

int bckbuttonState = 0;

First we make sure that the potentiometer, motor and push buttons are in LOW state.

For that we have initialised it to 0.

void setup()

{

pinMode(fwdbuttonPin, INPUT_PULLUP);

pinMode(bckbuttonPin,INPUT_PULLUP);

pinMode(ICpin2, OUTPUT);

pinMode(ICpin7, OUTPUT);

}

In the void setup function, we will write this code.


pinMode function defines the pins as INPUT or OUTPUT.


fwdbuttonPin and bckbuttonPin are set to INPUT_PULLUP mode.

In this mode we are using Arduino’s internal pull-up resistors.

Show the demo of the manual To know about the INPUT_PULLUP mode, refer to the manual.

Click on the Help menu in the Arduino IDE.

Then click on Reference.

This opens an offline page in your browser.

Scroll down.

Click on INPUT_PULLUP.

Switch back to Arduino IDE.

ICpin2 and ICpin7 are set to OUTPUT mode to drive the motor.

void loop()


Next we will write the code in void loop function.

analogRead command will read the analog value from the potentiometer.

This value will be given to analog pin A0.

Depending upon the potentiometer value, the speed of the motor will vary.

map command will convert the analog value to digital.

fwdbuttonState and bckbuttonState will fetch the signal if push button is pressed.

The IF command checks, if the push button connected to pin 12 or pin 13 is pressed.

This enables the motor to rotate in a clockwise or anti-clockwise direction.

else

{

digitalWrite(ICpin2, LOW);

digitalWrite(ICpin7, LOW);

}

}

Suppose we don’t press any of the two buttons.

Then the else command ensures the motor is in OFF condition.

This code is available in the Code file link of this tutorial

You can download and use it.

Click on the compile button Click on the compile button to verify the program.
Compile and save the program Let us save the program as PWM_Motor and click on the Save button.

Now click on upload button to upload the current program on Arduino.

Point to the output in the video

DC_motor_output.mp4

Now we will see the output of the above program.

I’ll press the push button connected to pin 13.

We can see the motor rotating in the clockwise direction.

Now I will release the push button.

The motor will stop rotating and it will be in OFF state.


Now again, I’ll press the push button connected to pin 12.

We can see the motor is rotating in anti-clockwise direction.


We can change the speed of motor by adjusting the potentiometer connected at A0.

This brings us to the end of this tutorial. Let us summarize.
Slide 17:

Summary

In this tutorial, we learnt about
  • Pulse Width modulation
  • PWM Duty Cycle
  • PWM Frequency and
  • How to control speed and direction of DC motor.
Slide 18:

Assignment

Buzzer_output.mp4

As an assignment:
  • Connect a Buzzer instead of LED in the above circuit connection.
  • Upload the same program and check the output.
  • You would hear a noise with different frequencies.
Here is the output of the assignment.
Slide 19:

About Spoken Tutorial project

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

Please download and watch it.

Slide 20:

Spoken Tutorial workshops

The Spoken Tutorial Project Team conducts workshops and gives certificates.

For more details, please write to us.

Slide 21:

Forum for specific questions

Please post your timed queries in this forum.
Slide 22:

Acknowledgement

Spoken Tutorial project is funded by MHRD, Government of India.
This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.

And this is Saurabh signing off.

Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat