Arduino/C2/Pulse-Width-Modulation/English-timed

From Script | Spoken-Tutorial
Revision as of 11:20, 23 January 2020 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken tutorial on Pulse Width Modulation.
00:06 In this tutorial we will learn about: PWM i.e Pulse Width modulation
00:13 PWM Duty Cycle
00:16 PWM Frequency

L293D Motor Driver IC

00:24 To follow this tutorial, you should have basic knowledge of:

Electronics and C or C++ programming language

00:35 To record this tutorial, I am using

Arduino Uno board

00:40 Ubuntu Linux 16.04 OS

Arduino IDE

00:46 We will also require some external components such as:

Breadboard

00:53 10K Ohm Potentiometer

LED

00:58 220 ohm Resistor
01:01 Jumper Wires

Push Button

01:05 DC Motor
01:08 and L293D Motor Driver IC
01:14 PWM signal is a square wave signal which has a high frequency i.e 1KHz.
01:22 PWM is a technique by which the width of the pulse is varied.
01:28 It is done while keeping the frequency of wave constant.
01:33 PWM signal consists of two main properties that define its behaviour.
01:40 They are Duty Cycle and Frequency.
01:44 It is the percentage of time, a digital signal is on, over a period of time.
01:50 Duty cycle can be varied from 0% to 100%..
01:55 The formula to calculate the percentage of duty cycle is shown here.
02:01 tON is equal to the duration of time when signal is high.
02:06 tOFF is equal to the duration of time when signal is low.
02:11 Time Period is tON + tOFF.

i.e. It is equal to the sum of on time and off time of PWM signal.

02:24 Frequency determines how fast the PWM completes a cycle.
02:29 i.e. How fast it switches from HIGH to LOW states.
02:34 We will perform one simple experiment by varying duty cycle.
02:39 This will control the brightness of LED.
02:43 Arduino Uno has 6 PWM channels.
02:48 Pins 3, 5, 6, 9, 10, 11 on Arduino Uno are PWM channels.
02:58 PWM channels are denoted by tilde sign.
03:02 Let us see the circuit connection now.
03:05 Connect anode leg of the LED to pin 9 of Arduino through 220 ohm resistor.
03:13 Connect cathode leg of the LED to ground.
03:17 This is the live setup of the connection.
03:20 Do the connection as shown in the image.
03:23 Let us open the Arduino IDE.

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

03:32 Type the code as shown.
03:35 We have assigned the PWM pin 9 to the variable LED_Pin.
03:42 We have initialized duty_cycle_value as 1 for an LED to turn ON.
03:51 Inside void setup, we will write pinMode function.
03:56 We have declared pin 9 of the Arduino as OUTPUT.
04:01 Inside void loop function we will write this code.

Let me explain the code.

04:08 While loop executes the code till the duty_cycle_value is below 255.
04:17 analogWrite() function is used to generate PWM signal.
04:22 We are passing two parameters. i.e PWM pin number and the duty cycle value.
04:30 The duty cycle value must be between 0 to 255 i.e between 0 volts and 5 Volts
04:40 We will keep a delay of 3000 millisecond i.e. 3 seconds
04:46 Click on the compile button to verify your program.
04:51 A pop up window will appear to save the current program.
04:55 Let us save the program as LED_Brightness and click on the Save button.
05:03 Now click on upload button to upload the current program on Arduino board.
05:09 We can see the brightness of LED increasing gradually.
05:15 Next, we will do an experiment to control the speed and direction of a DC motor.
05:22 This is the pinout diagram of L293D motor driver IC.
05:28 The speed of the motor is controlled by EN 1 and EN 2 of the IC.
05:36 The direction of the motor is controlled by IN1, IN2, IN3, IN4 of the IC.
05:45 We can control 2 motors at a time using this IC.
05:50 In our experiment, we will connect only one DC motor.
05:55 Let us see the circuit connection now.
05:58 Pin 1, pin 8 and pin 16 of driver IC are connected to 5V.
06:05 Pin 4 and pin 5 of driver IC are connected to ground.
06:11 Pin 2 and pin 7 of driver IC are connected to pin 11 and pin 10 of Arduino.
06:20 2 push buttons are connected to pin 12 and pin 13 of Arduino.
06:27 These push buttons are used to control the direction of DC motor.
06:33 10Kohm potentiometer is connected to control the speed of the DC motor.
06:39 Middle pin of potentiometer is connected to analog pin A0.
06:45 Pin 3 and pin 6 of driver IC are connected to DC motor.
06:51 Do the connection as shown in the image.
06:55 This is the live setup of the connection, as shown in the image.
07:00 I have fixed a wheel on the shaft of the motor.
07:04 This will help to see the rotation and varying speed of motor clearly.
07:10 Now we will write a program for this circuit to work.
07:14 Let’s switch to Arduino IDE.
07:18 Type the code as shown here.

We have initialized the connection between Arduino and driver IC.

07:28 Potentiometer pin is connected to analog pin A0.
07:33 fwdbuttonPin is the variable for push button connected to pin 13 of Arduino.
07:40 bckbuttonPin is the variable for push button connected to pin 12 of Arduino.
07:47 ICpin2 and ICpin7 are the variables which indicate pin 2 and pin 7 of the IC.
07:57 They are connected to pin 11 and pin 10 of Arduino respectively.
08:04 First we make sure that the potentiometer, motor and push buttons are in LOW state.

For that we have initialised it to 0.

08:15 In the void setup function, we will write this code.
08:20 pinMode function defines the pins as INPUT or OUTPUT.
08:25 fwdbuttonPin and bckbuttonPin are set to INPUT_PULLUP mode.
08:32 In this mode we are using Arduino’s internal pull-up resistors.
08:38 To know about the INPUT_PULLUP mode, refer to the manual.
08:44 Click on the Help menu in the Arduino IDE.

Then click on Reference.

08:50 This opens an offline page in your browser.

Scroll down.

08:55 Click on INPUT_PULLUP.
09:00 Switch back to Arduino IDE.
09:03 ICpin2 and ICpin7 are set to OUTPUT mode to drive the motor.
09:10 Next we will write the code in void loop function.
09:14 analogRead command will read the analog value from the potentiometer.
09:20 This value will be given to analog pin A0.
09:24 Depending upon the potentiometer value, the speed of the motor will vary.
09:30 map command will convert the analog value to digital.
09:35 fwdbuttonState and bckbuttonState will fetch the signal if push button is pressed.
09:43 The IF command checks, if the push button connected to pin 12 or pin 13 is pressed.
09:50 This enables the motor to rotate in a clockwise or anti-clockwise direction.
09:56 Suppose we don’t press any of the two buttons.
10:00 Then the else command ensures the motor is in OFF condition.
10:05 This code is available in the Code file link of this tutorial

You can download and use it.

10:13 Click on the compile button to verify the program.
10:17 Let us save the program as PWM_Motor and click on the Save button.
10:25 Now click on upload button to upload the current program on Arduino.
10:31 Now we will see the output of the above program.
10:35 I’ll press the push button connected to pin 13.
10:39 We can see the motor rotating in the clockwise direction.
10:43 Now I will release the push button.
10:47 The motor will stop rotating and it will be in OFF state.
10:52 Now again, I’ll press the push button connected to pin 12.
10:57 We can see the motor is rotating in anti-clockwise direction.
11:02 We can change the speed of motor by adjusting the potentiometer connected to A0.
11:14 This brings us to the end of this tutorial. Let us summarize.
11:20 In this tutorial, we learnt about

Pulse Width modulation

11:26 PWM Duty Cycle
11:29 PWM Frequency and How to control speed and direction of DC motor.
11:38 As an assignment:

Connect a Buzzer instead of LED in the above circuit connection.

11:45 Upload the same program and check the output.
11:49 You would hear a noise with different frequencies.
11:53 Here is the output of the assignment.
12:01 The video at the following link summarizes the Spoken Tutorial project.

Please download and watch it.

12:09 The Spoken Tutorial Project Team conducts workshops and gives certificates.

For more details, please write to us.

12:19 Please post your timed queries in this forum.
12:23 Spoken Tutorial project is funded by MHRD, Government of India.
12:29 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

PoojaMoolya