Difference between revisions of "Arduino/C2/Pulse-Width-Modulation/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| style="border-spacing:0;" | style="border-top:1.5pt solid #000000;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| ''...")
 
 
Line 6: Line 6:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| Slide 1:
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| Slide 1:
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Welcome to the '''Spoken Tutorial''' on '''Pulse Width Modulation.'''
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Welcome to the spoken tutorial on '''Pulse Width Modulation.'''
  
 
|-
 
|-
Line 55: Line 55:
  
 
Image '''DC-motor.jpg'''
 
Image '''DC-motor.jpg'''
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| * '''DC Motor'''
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"|'''DC Motor'''
  
 
|-
 
|-
Line 61: Line 61:
  
 
Image '''L293D.jpg'''
 
Image '''L293D.jpg'''
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| * '''L293D Motor Driver IC'''
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"|'''L293D Motor Driver IC'''
  
 
|-
 
|-
Line 67: Line 67:
  
 
Pulse Width Modulation
 
Pulse Width Modulation
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| * '''PWM signal '''is a square wave signal which has a high frequency i.e (1KHz).
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"|
* '''PWM''' is a technique by which the width of the pulse is varied.
+
*'''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.
 
* It is done while keeping the frequency of wave constant.
  
Line 75: Line 76:
  
 
Pulse Width Modulation
 
Pulse Width Modulation
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| * PWM signal consists of two main properties that define its behaviour.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"|
 +
*'''PWM signal''' consists of two main properties that define its behaviour.
 
* They are '''Duty Cycle''' and '''Frequency.'''
 
* They are '''Duty Cycle''' and '''Frequency.'''
  
Line 83: Line 85:
 
Image '''duty_cycle.jpg'''
 
Image '''duty_cycle.jpg'''
  
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| It is the percentage of time, a digital signal is on, over a period of time.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| 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%..
+
'''Duty cycle''' can be varied from 0% to 100%..
  
 
|-
 
|-
Line 93: Line 95:
  
 
Image''' duty_cycle_formula.jpg'''
 
Image''' duty_cycle_formula.jpg'''
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| The Formula to calculate the percentage of duty cycle is shown here.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| The formula to calculate the percentage of '''duty cycle''' is shown here.
* '''tON '''is equal to the''' '''duration of time when signal is high.
+
* '''tON '''is equal to the duration of time when '''signal''' is high.
* '''tOFF '''is equal to the''' '''duration of time when signal is low.
+
* '''tOFF '''is equal to the duration of time when '''signal''' is low.
* '''Time Period'''is''' tON + tOFF'''.
+
* '''Time Period''' is''' tON + tOFF'''.
* i.e. It is equal to the sum of on time and off time of PWM signal.
+
* i.e. It is equal to the sum of '''on''' time and '''off''' time of '''PWM signal'''.
  
 
|-
 
|-
Line 103: Line 105:
  
 
PWM frequency
 
PWM frequency
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| * Frequency determines how fast the PWM completes a cycle.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"|
* i.e. How fast it switches from HIGH to LOW states.
+
*Frequency determines how fast the '''PWM''' completes a '''cycle'''.
 +
* i.e. How fast it switches from '''HIGH''' to '''LOW''' states.
  
 
|-
 
|-
Line 110: Line 113:
  
 
Example - Duty Cycle
 
Example - Duty Cycle
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| * We will perform one simple experiment by varying duty cycle.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"|We will perform one simple experiment by varying '''duty cycle'''.
* This will control the brightness of LED.  
+
 
 +
This will control the brightness of '''LED'''.  
  
 
|-
 
|-
Line 118: Line 122:
 
Image '''Arduino_PWM_pins.png'''
 
Image '''Arduino_PWM_pins.png'''
  
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| * '''Arduino Uno''' has 6 '''PWM channels.'''
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"|'''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.
+
Pins 3, 5, 6, 9, 10, 11 on '''Arduino Uno''' are '''PWM channels.'''
 +
 
 +
'''PWM channels''' are denoted by''' tilde '''sign.
  
 
|-
 
|-
Line 130: Line 136:
  
 
Image '''LED_Connection.png'''
 
Image '''LED_Connection.png'''
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| * Connect '''anode''' leg of the '''LED''' to '''pin 9 '''of Arduino through 220 ohm resistor.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"|Connect '''anode''' leg of the '''LED''' to pin 9 of '''Arduino''' through '''220 ohm resistor.'''
* Connect '''cathode''' leg of the '''LED''' to ground.
+
 
 +
 
 +
Connect '''cathode''' leg of the '''LED''' to '''ground'''.
  
 
|-
 
|-
Line 137: Line 145:
  
 
'''LED_connection_setup.png'''
 
'''LED_connection_setup.png'''
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| This is the '''live setup''' of the connection, as shown in the image.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| This is the '''live setup''' of the connection.
  
  
Line 146: Line 154:
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Let us open the''' Arduino IDE'''.
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Let us open the''' Arduino IDE'''.
  
We will write a program to change the brightness of '''LED''' using '''PWM''' pin.
+
We will write a '''program''' to change the brightness of '''LED''' using '''PWM''' pin.
  
 
|-
 
|-
Line 155: Line 163:
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Type the code as shown.
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Type the code as shown.
  
We have assigned the '''PWM pin 9''' to the variable '''LED_Pin.'''
+
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.
+
 
 +
We have initialized '''duty_cycle_value''' as 1 for an '''LED''' to turn '''ON'''.
  
 
|-
 
|-
Line 167: Line 176:
  
 
}
 
}
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Inside void setup, we will write '''pinMode''' function.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Inside '''void setup''', we will write '''pinMode function'''.
 
+
We have declared '''pin 9''' of the Arduino as output.
+
  
 +
We have declared '''pin 9''' of the '''Arduino''' as '''OUTPUT'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| Highlight according to narration
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| Highlight according to narration
  
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Inside void loop function we will write this code.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Inside '''void loop function''' we will write this code.
  
 
Let me explain the code.
 
Let me explain the code.
Line 181: Line 189:
 
'''While''' loop executes the code till the '''duty_cycle_value''' is below 255.
 
'''While''' loop executes the code till the '''duty_cycle_value''' is below 255.
  
'''analogWrite'''() function is used to generate PWM signal.
+
'''analogWrite() function''' is used to generate '''PWM signal'''.
  
We are passing two parameters. i.e PWM pin number and the duty cycle value.
+
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)
+
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  
+
We will keep a '''delay''' of 3000 millisecond i.e. 3 seconds  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| Compile and save the program
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| Compile and save the program
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Click on the '''compile button '''to verify your program.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Click on the '''compile''' button to verify your '''program'''.
  
A pop up window will appear to save the current 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.
+
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'''.
+
Now click on '''upload''' button to upload the current '''program''' on '''Arduino'''.
  
 
|-
 
|-
Line 203: Line 211:
  
 
'''LED_output.mp4'''
 
'''LED_output.mp4'''
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| We can see, the brightness of '''LED''' increasing gradually.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| We can see the brightness of '''LED''' increasing gradually.
  
 
|-
 
|-
Line 214: Line 222:
 
Image '''L293D_Pinout.png'''
 
Image '''L293D_Pinout.png'''
  
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| This is the pinout diagram of '''L293D motor driver IC.'''
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| 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 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.
+
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.
+
We can control 2 '''motors''' at a time using this '''IC'''.
  
In our experiment, we will connect only one DC motor.
+
In our experiment, we will connect only one '''DC motor'''.
  
 
|-
 
|-
Line 233: Line 241:
 
Image '''Speed-Direction-Control-Connection.png'''
 
Image '''Speed-Direction-Control-Connection.png'''
  
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| * '''Pin 1, Pin 8 and Pin 16 '''of driver IC are connected to''' 5V.'''
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"|  
* '''Pin 4 '''and''' pin 5 '''of driver IC are connected to ground.
+
* Pin 1, pin 8 and pin 16 of '''driver IC''' are connected to''' 5V.'''
* '''Pin 2 and Pin 7''' of driver IC are connected to''' Pin 11''' and '''Pin 10''' of Arduino.
+
* Pin 4 and pin 5 of '''driver IC''' are connected to '''ground'''.
* 2 push buttons are connected to '''pin12''' and '''pin13''' of arduino.
+
* Pin 2 and pin 7 of '''driver IC''' are connected to pin 11 and pin 10 of '''Arduino'''.
* These push buttons are used to control the direction of DC motor'''.'''
+
* 2 '''push buttons''' are connected to pin 12 and pin 13 of '''Arduino'''.
* '''10Kohm''' potentiometer is connected to control the speed of the DC motor.
+
* These '''push buttons''' are used to control the direction of '''DC motor'''.
* Middle pin of '''potentiometer''' is connected to analog '''pin A0.'''
+
* '''10Kohm potentiometer''' is connected to control the speed of the '''DC motor'''.
* '''Pin 3''' and '''Pin 6''' of driver IC are connected to DC motor.
+
* Middle pin of '''potentiometer''' is connected to '''analog''' pin A0.
* Do the connection as shown in the image.
+
* Pin 3 and pin 6 of '''driver IC''' are connected to '''DC motor'''.
 +
 
 +
Do the connection as shown in the image.
  
 
|-
 
|-
Line 250: Line 260:
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| This is the '''live setup''' of the connection, as shown in the image.
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| This is the '''live setup''' of the connection, as shown in the image.
  
I have fixed a wheel on the shaft of the motor.
+
I have fixed a wheel on the shaft of the '''motor'''.
  
This will help to see the rotation and varying speed of motor clearly.
+
This will help to see the rotation and varying speed of '''motor''' clearly.
  
Now we will write a program for this circuit to work
+
Now we will write a '''program''' for this circuit to work.
  
 
|-
 
|-
Line 275: Line 285:
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Type the code as shown here.
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Type the code as shown here.
  
We have initialized the connection between Arduino and driver IC.
+
We have initialized the connection between '''Arduino''' and '''driver IC'''.
  
Potentiometer pin is connected to '''analog pin A0'''.
+
'''Potentiometer''' pin is connected to '''analog''' pin A0.
  
'''fwdbuttonPin '''is the variable for push button connected to pin 13 of Arduino.  
+
'''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.  
+
'''bckbuttonPin '''is the '''variable''' for '''push button''' connected to pin 12 of '''Arduino'''.  
  
'''ICpin2''' and''' ICpin7 '''are the variables which indicates '''Pin 2 and Pin 7''' of IC.
+
'''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.
+
They are connected to pin 11 and pin 10 of '''Arduino''' respectively.
  
 
|-
 
|-
Line 295: Line 305:
  
 
int bckbuttonState = 0;
 
int bckbuttonState = 0;
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| First we make sure that the potentiometer, motor and push buttons are in LOW state.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| First we make sure that the '''potentiometer, motor''' and '''push buttons''' are in '''LOW''' state.
  
 
For that we have initialised it to 0.
 
For that we have initialised it to 0.
Line 313: Line 323:
  
 
}
 
}
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| In the''' void setup''' function, we will write this code.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| In the''' void setup function''', we will write this code.
 +
 
 +
 
 +
'''pinMode function''' defines the pins as '''INPUT''' or '''OUTPUT'''.
  
pinMode function defines the pins as INPUT or OUTPUT.
 
  
 
'''fwdbuttonPin''' and '''bckbuttonPin''' are set to '''INPUT_PULLUP '''mode.  
 
'''fwdbuttonPin''' and '''bckbuttonPin''' are set to '''INPUT_PULLUP '''mode.  
  
In this mode we are using arduino’s internal pull-up resistors.
+
In this mode we are using '''Arduino’s''' internal '''pull-up resistors'''.
  
 
|-
 
|-
Line 327: Line 339:
 
Click on the '''Help menu''' in the '''Arduino IDE.'''
 
Click on the '''Help menu''' in the '''Arduino IDE.'''
  
Then click on '''reference.'''
+
Then click on '''Reference.'''
  
 
This opens an offline page in your browser.  
 
This opens an offline page in your browser.  
Line 339: Line 351:
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Switch back to '''Arduino IDE.'''
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Switch back to '''Arduino IDE.'''
  
'''ICpin2 '''and''' ICpin7 '''are set to '''OUTPUT''' mode to drive the motor.
+
'''ICpin2 '''and''' ICpin7 '''are set to '''OUTPUT''' mode to drive the '''motor'''.
  
 
|-
 
|-
Line 345: Line 357:
  
  
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Next we will write the code in '''void loop''' function.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Next we will write the code in '''void loop function'''.
  
'''analogRead''' command will read the analog value from the potentiometer.
+
'''analogRead command''' will read the '''analog''' value from the '''potentiometer'''.
  
This value will be given to analog pin A0.
+
This value will be given to '''analog''' pin A0.
  
Depending upon the potentiometer value, the speed of the motor will vary.
+
Depending upon the '''potentiometer''' value, the speed of the '''motor''' will vary.
  
'''map''' command will convert the analog value to digital.
+
'''map command''' will convert the '''analog''' value to '''digital'''.
  
'''fwdbuttonState''' and '''bckbuttonState''' will fetch the signal if push button is pressed.  
+
'''fwdbuttonState''' and '''bckbuttonState''' will fetch the '''signal''' if '''push button''' is pressed.  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"|  
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"|  
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| The IF command checks, if the push button connected to pin12 or pin13 is pressed.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| 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.
+
This enables the '''motor''' to rotate in a clockwise or anti-clockwise direction.
  
 
|-
 
|-
Line 375: Line 387:
  
 
}
 
}
 +
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Suppose we don’t press any of the two '''buttons'''.
  
 +
Then the '''else command''' ensures the '''motor''' is in '''OFF''' condition.
  
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Suppose we don’t press any of the two buttons.
+
This code is available in the '''Code file''' link of this tutorial
 
+
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.
 
You can download and use it.
Line 387: Line 397:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| Click on the '''compile button'''
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| Click on the '''compile button'''
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Click on the '''compile button '''to verify the program.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Click on the '''compile''' button to verify the '''program'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| Compile and save the program
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"| Compile and save the program
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Let us save the program as '''PWM_Motor''' and click on the save button.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| 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'''.
+
Now click on '''upload''' button to '''upload''' the current '''program''' on '''Arduino'''.
  
 
|-
 
|-
Line 399: Line 409:
  
 
'''DC_motor_output.mp4'''
 
'''DC_motor_output.mp4'''
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Now we will see the output of the above program.
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| 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.
  
I’ll press the '''push button''' connected to '''pin13'''.
+
Now I will release the '''push button'''.
  
We can see the motor rotating in the '''clockwise''' direction.
+
The '''motor''' will stop rotating and it will be in '''OFF''' state.
  
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'''.
  
Now again, I’ll press the '''push button''' connected to '''pin12'''.
+
We can see the '''motor''' is rotating in anti-clockwise direction.
  
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.
+
We can change the speed of '''motor''' by adjusting the '''potentiometer''' connected at A0.
  
 
|-
 
|-
Line 440: Line 452:
  
 
* Connect a '''Buzzer''' instead of '''LED''' in the above circuit connection.
 
* Connect a '''Buzzer''' instead of '''LED''' in the above circuit connection.
* Upload the same program and check the output.
+
* Upload the same '''program''' and check the output.
 
* You would hear a noise with different frequencies.
 
* You would hear a noise with different frequencies.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"|  
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:none;padding:0.176cm;"|  
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Here is the output of the assignment .
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| Here is the output of the assignment.
  
 
|-
 
|-
Line 461: Line 473:
 
Spoken Tutorial workshops
 
Spoken Tutorial workshops
  
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team:
+
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team conducts '''workshops''' and gives certificates.
 
+
* conducts '''workshops''' and
+
* gives '''certificates'''.
+
  
 
For more details, please write to us.
 
For more details, please write to us.
Line 484: Line 493:
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.  
 
| style="border-top:none;border-bottom:1.5pt solid #000000;border-left:1.5pt solid #000000;border-right:1.5pt solid #000000;padding:0.176cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.  
  
And this is saurabh''' '''signing off.  
+
And this is Saurabh signing off.  
  
 
Thanks for joining.
 
Thanks for joining.
  
 
|}
 
|}

Latest revision as of 10:59, 3 January 2020

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