# EMS1EP Lecture 8 Pulse Width Modulation (PWM)

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

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

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

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

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

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

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

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

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

Filtering PWM Signal Without filtering circuitry
With filtering circuitry:

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

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

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

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

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

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--;

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

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

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

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

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

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

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

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