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

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

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

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

EMS1EP Lecture 8Pulse Width Modulation (PWM)

Dr. Robert Ross

Page 2: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

Overview (what you should learn today)

• Analog v’s digital• Theory behind PWM• PWM sample circuit/Demo• Worked examples• Servo Control

Page 3: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

Digital Revision – Analog/Digital

• Analog Voltages: Continuous in time and value• Digital Voltages: Discrete in time and value

Page 4: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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

Page 5: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

Analog/Digital

Microcontroller(LArduino)

ADCAnalog

to Digital

Converter

DACDigital

to Analog

Converter

Transducer(Sensor) Actuator

Physical Variable

(Sound, light, pressure, temp, ect)

Control Physical Variable

(Sound, light, pressure, temp, ect)

Analog Voltages

Digital Voltages

Page 6: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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

Page 7: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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 = 65535 Steps

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

PWM Characteristics

• Create a square-wave• Constant period• Varying duty cycle (proportional to required

analog voltage)

5V

0V

Page 9: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

Generating Analog Voltages5V

0V

analogWrite(pin, 0); 0% Duty cycle - Average Voltage: 0V

5V

0V

analogWrite(pin, 255); 100% Duty cycle - Average Voltage: 5V

5V

0V

analogWrite(pin, 63); 25% Duty cycle - Average Voltage: 1.25V

5V

0V

analogWrite(pin, 127); 50% Duty cycle - Average Voltage: 2.5V

5V

0V

analogWrite(pin, 191); 75% Duty cycle - Average Voltage: 3.75V

Page 10: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

Filtering PWM Signal

Without filtering circuitry With filtering circuitry:

Page 11: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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

Page 12: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

PWM Applications

• Communication – length of pulse specifies 1’s and 0’s

• Voltage Regulation• LED brightness control• Controlling the speed of motors• Servo control

Page 13: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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

Page 14: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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

Page 15: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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):

Page 16: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

Sawtooth - Codeint PWMOUT1 = 5; // PIN ASSIGNMENTSvoid 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--; analogWrite(PWMOUT1, onTime); delay(10); }}

Page 17: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

Sawtooth - Results

No Filtering (Value increased and then decreased)LED connected – goes brighter then dimmer

Page 18: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

Servo Control

• Remote control aircraft (and some robotics) are operated using servos

Page 19: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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

Page 20: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

Servo Control: Hardware connections

Arduino 5V

Red – Power (4-6V)

Black (or Brown) – GND

Yellow (or White or Orange) – PWM signal pin

Page 21: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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

Page 22: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

Servo Worked Example#include <Servo.h>

Servo servo1; // create servo object to control a servoint button1 = 5;void setup(){

servo1.attach(9); // Servo on Pin 9pinMode(button1, INPUT);

}

void loop(){

if (digitalRead(button1) == LOW){ //Button pressedservo1.write(30);

}else{ //Button not pressed

servo1.write(120);}

}

Page 23: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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

Page 24: EMS1EP Lecture 8 Pulse Width Modulation (PWM) Dr. Robert Ross.

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