18
Community College of Allegheny County Unit 4 Page #1 Timers and PWM Motor Control Revised: Dan Wolf, 3/1/2018

Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #1

Timers and PWM Motor Control

Revised: Dan Wolf, 3/1/2018

Page 2: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #2

OBJECTIVES:

• Timers: Astable and Mono-Stable

• 555 Timers

• Pulse Width Modulation for Motor Control

DELIVERABLES THAT YOU MUST SUBMIT

1. Graphs #1 and #2, Tables #1 and #2

2. Practice Problems

On-Line Reading Material:

Required:

a) http://www.engin.swarthmore.edu/~ejaoudi1/datasheets/555 b) http://www.ocfreaks.com/pulse-width-modulation-pwm-

tutorial/

Optional:

a) How to Build a 555 Timer Mono-stable Circuit:

http://www.learningaboutelectronics.com/Articles/555-timer-

monostable-circuit.php

b) How to Build a 555 Timer Bi-stable Circuit:

c) http://www.learningaboutelectronics.com/Articles/555-timer-bistable-circuit.php

INTRODUCTION TO THE 555 TIMER:

The IC 555 has three operating modes:

a) Astable (free-running) mode: As per Figure #1, in this mode, the 555 operates as an electronic oscillator where

the square wave output frequency is given by the equation:

𝑓 = 1

0.693 ∗ 𝐶 ∗ (𝑅1 + 2𝑅2)

Where R1 and R2 are in ohms and C is the value of the

capacitor in farads.

The High time from each pulse is given by:

ℎ𝑖𝑔ℎ = 0.693 ∗ 𝐶 ∗ (𝑅1 + 𝑅2)

Page 3: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #3

And the Low time from each pulse is given by:

𝑙𝑜𝑤 = 0.693 ∗ 𝐶 ∗ 𝑅2

b) Monostable mode: As per Figure #2, in this mode, the 555 functions as a "one-shot" pulse generator. Once the input

on Pin 2 is triggered, the output on Pin 2 will go high for

a period of time determined by R and C according to the

equation:

𝑡 = 1.1 ∗ 𝑅𝐶

Where R1 and R2 are in ohms and C is the value of the

capacitor in farads.

c) Bistable mode or Schmitt trigger: As per Figure #3, the 555 can operate as a flip-flop. Uses include bounce-free

latched switches.

INTRODUCTION TO PULSE-WIDTH MODULATION (PWM):

Pulse-width modulation (PWM) uses digital pulses to generate an

analog voltage. A digital output pin may be either high (+5 V)

or low (0 V). But what if you switched the pin rapidly between

high and low so that it was high half the time and low half the

time? As per Figure #5, the average voltage over time would be

halfway between 0 and 5 V (2.5 V). PWM emits a burst of ones and

zeros whose ratio is proportional to the duty value you specify.

The proportion of ones to zeros in PWM is called the duty cycle.

The duty cycle controls the analog voltage in a very direct way;

the higher the duty cycle the higher the voltage. Duty Cycle is

the proportion of ones to zeros output by the PWM command. To

determine the proportional PWM output voltage, use this formula:

𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒 =𝑇𝑖𝑚𝑒 𝐻𝑖𝑔ℎ 𝑝𝑒𝑟 𝑝𝑢𝑙𝑠𝑒 𝑐𝑦𝑐𝑙𝑒

𝑇𝑖𝑚𝑒 𝑝𝑒𝑟 𝑝𝑢𝑙𝑠𝑒 𝑐𝑦𝑐𝑙𝑒

𝑃𝑟𝑜𝑝𝑜𝑟𝑡𝑖𝑜𝑛𝑎𝑙 (𝐴𝑣𝑒𝑟𝑎𝑔𝑒) 𝑂𝑢𝑡𝑝𝑢𝑡 𝑉𝑜𝑙𝑡𝑎𝑔𝑒 = 𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒 ∗ 5𝑉𝑜𝑙𝑡𝑠

For example, if the duty cycle is 50%, 0.50 x 5V = 2.5V; the

PWM will output a train of pulses whose average voltage is 2.5V.

Page 4: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #4

Although we could slow down a motor by wiring it in series to a

variable resister using a constant power supply, this approach

wastes the power being dropped across the resister. PWM has the

advantage in that no power is being wasted across such a

resister.

Page 5: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #5

Experiment #1 - Astable (free-running) Timer:

1. Figure #2 shows a typical 555 Astable circuit. EQUIPMENT REQUIRED:

CCAC 555 Timer Lab box or the following:

a. LED and 330Ω load resister b. R1 = 2000Ω (you may substitute a 10K pot)

c. R2 = 2000Ω (you may substitute a 10K pot) d. C = 1uF e. 10nF capacitor f. +5 Volt power supply

2. Connect the circuit shown in Figure #2 and add the LED and

330Ω resister to the output on Pin 3. Calculate the expected frequency, period, time high and time low then

update Table #1. Ask the instructor to check the circuit

before you apply power!

3. Use the oscilloscope to monitor the output on Pin 3. Assuming that the oscilloscope confirms the expected

frequency value, does the LED light? Explain?

4. Use Graph #1 to sketch this waveform and include the voltage level, period and frequency. Update Table #1

5. Calculate new values for R1, R2, and C which will allow you to see the LED blink (i.e. about once per second). Test

with the new components and then update Table #1.

Table #1

R1 R2

C F Period Thigh Tlow Duty

Cycle

Calculated 2K 2K 1uF

Measured 2K 2K 1uF

Calculated

Measured

Calculated

Measured

Page 6: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #6

Experiment #2 - Monostable Timer:

1. Figure #3 shows a typical 555 Monostable circuit. EQUIPMENT REQUIRED:

a. Misc. wire

b. LED and 330Ω load resister c. R = 10000Ω (ok to substitute a 10K potentiometer) d. C = 100uF e. 10nF capacitor f. +5 Volt power supply

2. Connect the circuit shown in Figure #3. Add the LED and

330Ω resister to the output on Pin 3 and a switch to the trigger input on Pin 2. Calculate the expected pulse time

and update Table #2. Ask the instructor to check the

circuit before you apply power!

3. Use the oscilloscope to monitor the output on Pin 3 and update Table #2.

4. Use Graph #1 to sketch this waveform showing the voltage level and pulse period and then pdate Table #2.

5. Calculate new values for R and C for a different time period. Test with the new components and then update Table

#2.

Table #2

R C Pulse Period

Calculated 10K 100uF

Measured 10K 100uF

Calculated

Measured

Calculated

Measured

Page 7: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #7

Experiment #3 – 555 Pulse Width Modulation:

1. Figure #4 shows a 555 circuit that will generate a PWM duty cycle that will control the brightness of an LED or the

speed of a small motor. Figure #5 shows the PWM waveforms

that you would expect to see for different power levels.

You will need the following parts:

a) 555 timer IC b) R1 = 10K variable resistor c) C1 = 10nF capacitor d) C2 = 1uF capacitor

e) LED and 330Ω load resister

2. Connect the circuit shown in Figure #4 and add an LED and

330Ω resister to the output on Pins 7 and 8. Ask the instructor to check the circuit before you apply power!

6. Test the circuit while changing the variable resister: use both an oscilloscope and a volt meter to monitor the PWM

output to the LED.

7. Use Graph #2 to sketch at least two duty cycles (slow and fast) and include the peak voltage level, proportional PWM

output voltage and duty cycle.

8. Figure #6 shows a modified 555 circuit which will drive a bigger motor. This is being shown for information only and

you are not asked to build it.

Table #3

Peak

Voltage

Duty

Cycle

Calculated

PWM Voltage

Measured PWM

Voltage

(Volt Meter)

Slow Calculated n/a

Slow Measured

Fast Calculated n/a

Fast Measured

Calculated n/a

Measured

Experiment #4 – Arduino Pulse Width Modulation:

Page 8: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #8

1. The 555 timer works fine but it requires the assembly of discrete components on a printed circuit board. Using

today’s technology, engineers would more likely use a

microcontroller to perform PWM. In this experiment, you

will see how easy it is to use the Arduino to generate a

PWM motor signal. You will need the following parts:

a. Arduino Uno microcontroller, power supply, cable, and USB Hub.

b. AdaFruit Motor Shield v2.3 c. Small 12Volt Dc motor

2. Connect the circuit shown in Figure #7 and #8. Ask the instructor to check the circuit before you apply power!

3. Start the Arduino software: a) Start the Arduino interface by clicking on the file

named: DCMotorTest_CCAC.ino

b) Specify the correct Arduino serial port from the menu bar: Tools | Port | “COMx (Arduino/Genuine Uno”

c) Start the serial monitor from the menu bar: Tools |Serial Monitor

d) Upload the load cell software to the Arduino using the menu bar: Sketch |Upload

e) Observe that the Serial Monitor window is displaying the startup message.

4. In Mode 1, the motor will ramp up and down in forward then again in reverse. You may change the mode by reassigning

the value of intSelect_Mode which is located near the top

of the software file (and then uploading). The other

options are shown in the code.

5. Test the circuit while using an oscilloscope and volt meter to monitor the PWM output to the motor.

6. In this experiment, you have seen that PMW can be implemented rather quickly in software with a

microcontroller which is also likely to be doing other

tasks as well. If you are interested in software and want

to explore in more detail, the Arduino supports the use of

the AnalogWrite() function to do PWM and is explained here:

https://www.arduino.cc/en/Reference/AnalogWrite and

https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM

Figure 1 – The 555 Timer

Page 9: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #9

Figure 2 – 555 Astable (Free-running) Timer

Page 10: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #10

Figure 3 – 555 Monostable Timer

+5V

1K ohm

+5V

1K ohm

Figure 4 – 555 Timer PWM LED Brightness

+5V

LED

555

C1

C2

10K

Page 11: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #11

Figure 5 – PWM Waveforms

Figure 6 – 555 Timer PWM Motor Control

Page 12: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #12

Figure 7 – Arduino PWM Motor Control

Arduino Uno

Adafruit Motor Shield v2.3

Computer

M

USB

M1

M1

External

Power Supply

Connect a DC motor to motor port 1 - it does not matter which wire goes

into which terminal block as motors are bi-directional. Connect to the top two

terminal ports, do not connect to the middle pin (GND) See the next photo for

the red and blue wire example.

Figure 8 – Arduino Motor Shield Connections

M1 Motor

wires

Monitor here for excessive heat.

Page 13: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #13

Figure #9 – DCMotorTest_CCAC.ino - Arduino PWM Software

#include <Mouse.h>

/*

This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2

It won't work with v1.x motor shields! Only for the v2's with built in PWM control

For use with the Adafruit Motor Shield v2

----> http://www.adafruit.com/products/1438

Requires the Adafruit Motor Library to be installed: Adafruit_Motor_Shield_V2_Library-

master

BE CAREFUL OF THE MOTOR CURRENT!! 3.0A MAX BUT 2.0A REQUIRES A HEATSINK ON THE ARDUINO

MAX MOTOR CURRENT SHOULD BE 1.2A PER MOTOR OR LESS!!

SMALL MOTORS CAN PULL QUITE HIGH CURRENT LEVELS, ESPECIALLY AT STALL. */

*/

#include <Wire.h>

#include <Adafruit_MotorShield.h>

#include "utility/Adafruit_MS_PWMServoDriver.h"

// Create the motor shield object with the default I2C address

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

// Or, create it with a different I2C address (say for stacking)

// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

// Select which 'port' M1, M2, M3 or M4. In this case, M1

Adafruit_DCMotor *myMotor = AFMS.getMotor(1);

// You can also make another motor on port M2

//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

// Pick one of these to set the mode

int iSelect_Mode = 1; // Varying forward and reverse

// int iSelect_Mode = 2; // forward slow

// int iSelect_Mode = 3; // forward fast

// int iSelect_Mode = 4; // reverse slow

// int iSelect_Mode = 5; // reverse fast

void setup()

Serial.begin(9600); // set up Serial library at 9600 bps

Serial.println("\nAdafruit Motorshield v2 - DC Motor Test - vJan_12_2017");

Serial.println("High motor current will burn out the Arduino, ");

Serial.println("even for small motors or at stall conditions! \n");

AFMS.begin(); // create with the default frequency 1.6KHz

//AFMS.begin(1000); // OR with a different frequency, say 1KHz

// Set the speed to start, from 0 (off) to 255 (max speed)

myMotor->setSpeed(150);

myMotor->run(FORWARD);

// turn on motor

myMotor->run(RELEASE);

void loop()

uint8_t i;

delay(2000);

switch (iSelect_Mode)

Page 14: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #14

case 1:

Serial.print("Forward ");

delay(250);

myMotor->run(FORWARD);

for (i = 0; i < 255; i++)

myMotor->setSpeed(i);

delay(10);

for (i = 255; i != 0; i--)

myMotor->setSpeed(i);

delay(10);

Serial.print("Backward ");

delay(250);

myMotor->run(BACKWARD);

for (i = 0; i < 255; i++)

myMotor->setSpeed(i);

delay(10);

for (i = 255; i != 0; i--)

myMotor->setSpeed(i);

delay(10);

Serial.print("Release ");

delay(250);

myMotor->run(RELEASE);

delay(1000);

break;

case 2:

Serial.print("Run Forward Slow \n");

delay(250);

myMotor->run(FORWARD);

myMotor->setSpeed(50);

delay(10000);

Serial.print("Release \n");

delay(250);

myMotor->run(RELEASE);

delay(5000);

break;

case 3:

Serial.print("Run Forward Fast \n");

delay(250);

myMotor->run(FORWARD);

myMotor->setSpeed(100);

delay(10000);

Serial.print("Release \n");

Page 15: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #15

delay(250);

myMotor->run(RELEASE);

delay(5000);

break;

case 4:

Serial.print("Run Reverse Slow \n");

delay(250);

myMotor->run(BACKWARD);

myMotor->setSpeed(100);

delay(10000);

Serial.print("Release \n");

delay(250);

myMotor->run(RELEASE);

delay(5000);

break;

case 5:

Serial.print("Run Reverse Fast \n");

delay(250);

myMotor->run(BACKWARD);

myMotor->setSpeed(100);

delay(10000);

Serial.print("Release \n");

delay(250);

myMotor->run(RELEASE);

delay(5000);

break;

default:

Serial.print("Invalid Mode Selected - Defaulting to Mode 1 \n\n");

iSelect_Mode = 1;

break;

Page 16: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #16

Graph#1 – Astable and Monostable Waveforms

Page 17: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #17

Graph#2 – PWM Waveforms

Page 18: Timers and PWM Motor Controlweb.acd.ccac.edu/~dwolf/files_to_download/MIT240_Unit4...Community College of Allegheny County Unit 4 Page #3 And the Low time from each pulse is given

Community College of Allegheny County Unit 4 Page #18

PRACTICE PROBLEMS: 1. Design a 555 mono-stable circuit which provides a 750mS pulse when the input is triggered

by a SPST switch.

2. Design a 555 Astable circuit which provides an output signal of 500Hz.

3. Design a 555 Astable circuit which will provide a PWM output voltage of 3.0volts. The

entire circuit should be powered by +5volt. Specify the time high, time low, and duty cycle

for the output.

4. Research the internet for equations to calculate the Thigh and Tlow for an Astable timer.

Apply the equations to Table #1 and compare the results to the measured values.

5. Modify Figure #3 to use the trigger switch configuration shown below. Explain why it will

not work.

+5V

1K ohm

+5V

1K ohm