Presentation is loading. Please wait.

Presentation is loading. Please wait.

EMS1EP Lecture 8 Pulse Width Modulation (PWM)

Similar presentations

Presentation on theme: "EMS1EP Lecture 8 Pulse Width Modulation (PWM)"— Presentation transcript:

1 EMS1EP Lecture 8 Pulse Width Modulation (PWM)
Dr. Robert Ross

2 Overview (what you should learn today)
Analog v’s digital Theory behind PWM PWM sample circuit/Demo Worked examples Servo Control

3 Digital Revision – Analog/Digital
Analog Voltages: Continuous in time and value Digital Voltages: Discrete in time and value

4 Analog/Digital The real world is analog
Most things we measure or control can be described my continuous values Pressure, temperature, acceleration, sound, light Computers are digital Need to convert these analog voltages as digital values

5 Analog/Digital ADC DAC Control Physical Variable Physical Variable
Digital Voltages Control Physical Variable (Sound, light, pressure, temp, ect) ADC Analog to Digital Converter Microcontroller (LArduino) DAC Digital to Analog Converter Physical Variable (Sound, light, pressure, temp, ect) Transducer (Sensor) Actuator Analog Voltages

6 ADC and DAC Analog to Digital Converters (ADCs) are used to convert the analog voltages into a digital value Digital to Analog Converters (DACs) are used to generate analog voltages using digital values The LArduino board has internal ADCs and has pins which can be configured as analog outputs using Pulse Width Modulation

7 Quantisation The analog values produced by a DAC look analog but are actually quantised values (not continuous) These values are in small steps The LArduino has 256 steps (0-255) over the range 0-5V 19.6mV Steps (5V/255=0.0196) To improve resolution: Lower the voltage range (0-3V) Increase the number of bits: 8Bits = 255 Steps 16Bits = Steps

8 PWM Characteristics Create a square-wave Constant period
Varying duty cycle (proportional to required analog voltage) 5V 0V

9 Generating Analog Voltages
analogWrite(pin, 0); % Duty cycle - Average Voltage: 0V 5V 0V analogWrite(pin, 63); % Duty cycle - Average Voltage: 1.25V 5V 0V analogWrite(pin, 127); % Duty cycle - Average Voltage: 2.5V 5V 0V analogWrite(pin, 191); % Duty cycle - Average Voltage: 3.75V 5V 0V analogWrite(pin, 255); % Duty cycle - Average Voltage: 5V

10 Filtering PWM Signal Without filtering circuitry
With filtering circuitry:

11 Pulse Width Modulation (PWM)
Use: analogWrite(pin, <AnalogVoltage>); AnalogVoltage can vary from 0-255 Will toggle the pin really fast high and low – to produce something more like an analog voltage need to smooth this out Use a Low Pass Filter to smooth the voltage out to approximate an analog voltage

12 PWM Applications Communication – length of pulse specifies 1’s and 0’s
Voltage Regulation LED brightness control Controlling the speed of motors Servo control

13 PWM on the LArduino PWM can be performed on any of the pins marked with a ‘P’ Use pins 5, 6, 9, 10, 11 Pins used for PWM don’t need to be setup – analogWrite does this automatically

14 Class Challenge Write some code to do the following:
A switch is connected to Pin 8 A LED is connected to Pin 6 If the button is pressed the LED should be at 75% brightness If the button is not pressed the LED should be at 25% brightness

15 Worked example – Sawtooth signal
Create a sawtooth signal on Pin 5 When connected to an LED the LED should successively fade brighter and dimmer When connected to a scope should give output (when connected to filter):

16 Sawtooth - Code int PWMOUT1 = 5; // PIN ASSIGNMENTS void setup()
{ //PWM pins don't require initialisation } void loop() { int onTime = 0; while(onTime < 255){ onTime++; analogWrite(PWMOUT1, onTime); delay(10); while(onTime > 0){ onTime--;

17 Sawtooth - Results No Filtering (Value increased and then decreased)
LED connected – goes brighter then dimmer

18 Servo Control Remote control aircraft (and some robotics) are operated using servos

19 Servo Control Servos need to be attached to pin 9 or 10 on the LArduino board Servos are controlled using a PWM signal Period: 20ms On time: 1.25 to 1.75ms On time sets the position of the servo arm

20 Servo Control: Hardware connections
Red – Power (4-6V) Black (or Brown) – GND Yellow (or White or Orange) – PWM signal pin Arduino 5V

21 Servo Worked Example If button on Pin 5 is pressed servo to go to 30O
If button is not pressed servo to go to 120O Servo connected to Pin 9 Using Arduino Servo library Allows desired angle of servo to be specified

22 Servo Worked Example #include <Servo.h>
Servo servo1; // create servo object to control a servo int button1 = 5; void setup() { servo1.attach(9); // Servo on Pin 9 pinMode(button1, INPUT); } void loop() if (digitalRead(button1) == LOW){ //Button pressed servo1.write(30); else{ //Button not pressed servo1.write(120);

23 Continuous Rotation Servos
By default servo motors only have about 180O of motion that they can travel over Servos can be modified for continuous rotation (they can go right around) PWM signal controls speed not position of continuous rotation servos Involves: Removing feedback potentiometer (variable resistor) Soldering in resistors Cutting notch out of gears

24 Summary (What you learnt in this session)
The real world is analog, but our microcontrollers are digital PWM is one type of DAC to generate analog like voltages from a microcontroller Use analogWrite() to set PWM value Servos use PWM to set the position of the servo arm – can use the servo library

Download ppt "EMS1EP Lecture 8 Pulse Width Modulation (PWM)"

Similar presentations

Ads by Google