6
http://www.instructables.com/id/Arduino-Hardware-PWM-for-stepper-motor-drives/ technology workshop living food play outside Arduino Hardware PWM for stepper motor drives by msraynsford on March 15, 2014 Table of Contents Arduino Hardware PWM for stepper motor drives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Intro: Arduino Hardware PWM for stepper motor drives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Step 1: Pulse Width Modulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Step 2: PWM for servo control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Step 3: PWM for stepper motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Step 4: Putting it into a project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Arduino Hardware PWM for Stepper Motor Drives

Embed Size (px)

DESCRIPTION

Arduino Hardware PWM for Stepper Motor Drives

Citation preview

  • http://www.instructables.com/id/Arduino-Hardware-PWM-for-stepper-motor-drives/

    technology workshop living food play outside

    Arduino Hardware PWM for stepper motor drivesby msraynsford on March 15, 2014

    Table of Contents

    Arduino Hardware PWM for stepper motor drives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    Intro: Arduino Hardware PWM for stepper motor drives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

    Step 1: Pulse Width Modulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

    Step 2: PWM for servo control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    Step 3: PWM for stepper motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    Step 4: Putting it into a project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

  • http://www.instructables.com/id/Arduino-Hardware-PWM-for-stepper-motor-drives/

    Intro: Arduino Hardware PWM for stepper motor drivesWith our new project, JustAddSharks , we are very keen to support the development of open source control software for laser cutters. We are more than happy forpeople to modify our laser cutters , replace the control system with something suitably open source and then let us know how it goes. Realistically though we're going tohave to figure these things out for ourselves so we can guide other people through the process.

    There are a lot of tutorials online detailing the hardware PWM features of the arduino and how it relates to servo motor control. This instructable will show you how to usethe hardware PWM to control a stepper motor instead. We'll start with the basics of PWM, show briefly how it relates to servo motors and then show the differences foruse with stepper motors.

    Step 1: Pulse Width ModulationPulse Width Modulation (PWM) is a technique for creating a digital square wave signal. A square wave has 3 main characteristics

    Amplitude - The amount the signal changes between On and Off statesFrequency - The number of times the signal repeats in a given time frameDuty Cycle - The proportion of On time to Off time usually expressed as a percentage

    In these digital systems the amplitude is fixed between 0 and 1, between 0V and VCC. In my testing this was +5V.

    In it's simplest form a PWM square wave can be created with a few lines of code. The following code would produce a signal with a 1 second Frequency and a 10% dutycycle. This would be functional but the Arduino would be dedicated to producing this signal the whole time and would not do anything else. This is why the blink withoutdelay example is so important to learn.

    digitalWrite(IOPin, HIGH);delayMicroseconds(100);digitalWrite(IOPin, LOW);delayMicroseconds(900);With a software generated signal any variation in the code execution time can cause variations in the output. Conditional statements may not execute on every loop so itis hard to guarantee that each loop will take exactly the same amount of time. Modern microcontrollers have dedicated hardware modules to generate a codeindependent square wave. Values for Duty cycle and Frequency are written to registers within the controller and the hardware generates the appropriate signal on thedesired output pin.

    The registers for the Hardware PWM module can be written to directly as described here , but the joy of the Arduino environment is that somebody is likely to have writtena library to simplify that process. The TimerOne library provides control over the HardwarePWM module connected to timer 1 (aptly). This library has easy functions forsetting the frequency and duty cycle of the square wave being generated

    pwm(pin, duty, period) - Generates a PWM waveform on the specified pin.The minimum freqeuncy supported by the library is 1ms and the maximum is 8.3s. The duty is specified as a 10 bit value, between 0 and 1023, and this correlates to the0% and 100% values respectively.

  • http://www.instructables.com/id/Arduino-Hardware-PWM-for-stepper-motor-drives/

    Step 2: PWM for servo controlA servo is a small mechanical actuator that typically consists of a motor a potentiometer and some control electronics. The potentiometer allows the controller todetermine what angle the output is at and provide a closed loop servomechanism. The servo controller receives input in the form of a square wave signal so it can driveto the desired angle.

    For servo control the Frequency of the signal is fixed, and the duty cycle varies.Servos accept a common control signal, a square wave with a repeat frequency of 20ms and an on period of 1ms (5%) to 2ms (10%). It is the width of the on pulse thatindicates what angle the servo should be at.

    Here is the code to set up the two extreme signals using the Timer one libraries

    pwm(9, 51, 20000);pwm(9, 102, 20000);

    You'll note that using this method there are only 50 steps between the minimum angle and the maximum angle, this is sufficient resolution for most basic systems. Thereare better methods for controlling the servo to provide more accurate resolutions but they are beyond the scope of this instructable.

    Image Notes1. Motor2. Servo Controller3. Potentiometer

    Step 3: PWM for stepper motorsA stepper motor is a brushless DC electric motor that divides a full rotation into a number of steps. This motors position can be determined by counting the number ofsteps that have been commanded providing open loop control of the system. A stepper motor driver is typically added to the system to power the stepper motor andsimplify the control into step and direction pulses. Each pulse on the step line causes the motor to move a step, or part step, in a give direction.

    For stepper motor driver control the duty cycle can be fixed and the Frequency varied.The stepper motor driver expects a series of input pulses to move the motor to any given angle. The driver moves the motor one step for each input pulse. The directionof the motor is set by the second input.

    Here is the code to set up the two different speeds using the Timer one libraries

    pwm(9, 512, 20000);pwm(9, 512, 40000);

    A smaller value for the period produces a higher frequency which gives more pulses per second and makes the motor turn faster. Changing the period while the motor isrunning can produce acceleration in the motor which will help the stepper reach it's maximum top speed.

  • http://www.instructables.com/id/Arduino-Hardware-PWM-for-stepper-motor-drives/

    Image Notes1. Stepper motor2. Stepper motor driver3. Pulse and Direction inputs

    Step 4: Putting it into a projectI discovered all this information with a goal in mind. I wanted to drive the stepper motor connected to the Z axis of my laser cutter . Manually generating a software PWMsignal on an Arduino pro mini just wasn't making the axis move fast enough and the occasional jitter in code had a habit of making the motor stall. I needed this cleanoutput signal to drive the motor faster and more reliably, the results were pretty promising.

    There is one major drawback to this method of control over stepper motors. A stepper motor uses open loop control to determine where it is, this is done by counting thenumber of steps the motor has made in any given direction. When using the hardware module to generate the PWM like this, nothing is actually counting the steps. Thismethod of control is only really suitable when there is another feedback mechanism. In my case the Z axis travels to the end switches and stops when it gets there. I don'tcare how many steps it takes to do that.

    I have another project in development that will use this same control method, in that system the user will provide the feedback and will stop the motor when it is in thecorrect position. This method is not really suitable for controlling the laser cutter as it traces out a design so I will move on to look at the next thing, expect more ibles inthe future.

    Image Notes1. Arduino Pro mini

  • http://www.instructables.com/id/Arduino-Hardware-PWM-for-stepper-motor-drives/

    Related Instructables

    2 axisautomatedcrane bysharkdanger

    Dot MatrixPrinter from aCD/DVD Readerwith Arduino byRobson Couto

    Arduino steppermotor andservos shield -AW GCSEBoard byAntMan232

    Arduino basedEtch A SketchLaser Cutter. bymsraynsford

    Motorizedcamera slidercontroled byAndroid phoneby hesamh

    3W 4'x4'Arduino LaserCutter/Engraverby FamousMods

    Advertisements

    Comments4 comments Add Comment

    Joe_M says: Jul 29, 2014. 1:20 PM REPLYI found this while looking for others who thought that PWM was a good way to run stepper controllers. Losing the count might be a good trade off in manysituations. Even printers use other ways to position the head than counting pulses. Pulse counting in printers and so many other things can fall short ofexpectations when the motor skips, stutters, or slips. I don't mean a lot, I mean just a tiny bit, just enough so the count is wrong, and that error will becumlative, and increase over time. I expect that I could use the interrupts to control limits. My thought on using PWM to control Steppers is that it offloads theprocess from the processor, and gives it time to do other things faster than it would if it was counting pulses, as well as generating them. This is a greatinstructable. I downloaded the library too, Thanks

    racataca says: Mar 28, 2014. 4:54 AM REPLY?

    I need to convert 2 PWM inputs (RC signal) and turn it into 2 analog outputs 5VDC. (0v-to 5 Vdc)..

    stratos13pao says: Mar 19, 2014. 11:36 AM REPLYim sure you know it but frequency idoes not mesure time, i think that would be the period, frequency is the number of occurrences of a repeating event perunit time

    Orngrimm says: Mar 18, 2014. 12:55 AM REPLYRecently i had to make a much faster PWM for a project but still preserve the capability of 1%-steps in the duty.In fact, i had to reach FULL speed --> 16MHz clock of the Arduino --> 160kHz frequency with 1% stepping of dutycycle.A lot of forums and boards said: No dice. That wont work, too fast.I fiddled around a bit and got it finally working as intended.

    The important parts are those setting of Timer1 and registers to it:

    // The definitions of the PWM. Borders, initial dutys and such stuffint duty1 = 80;// The desired dutycycle #1 initialisation.int duty2 = 1;// The desired dutycycle #2. Lets make 1% // Not needed... // Also here: 0..49 = 50 stepsint PWM_maximal = 100; // 100% is the TOP limitint PWM_minimal = 0; // 0% is the lower limit. However 0% is a tad > than 0. In fact, 0% == 60ns high (1 clockcycle). ATM noway around this. Sorry.int PWMfreq = 160; // kHz of the PWMint period = 16000 / PWMfreq; // How many clockticks shall = one period? (16000kHz Quarz / desired PWMfreq in kHz)

    // Set the Pins to output. Pins 13-2 are PWM-capable on my Arduino mega (Atmega1280)pinMode(11, OUTPUT);// pinMode(12, OUTPUT); // enable this if yu want to use the second PWM!// Init timer1// STOP & clearTCCR1B = TCCR1A = B00000000; // Clear TCCR1A (for a known start) andTCCR1B (to stop timer1 clock for register updates)// Define the pins and theyr behaviour / linkingTCCR1A = TCCR1A | B10000000; // Clear OC1A on match, set OC1A at BOTTOM (Non-Inverting-Mode) --> Pin11// TCCR1A = TCCR1A | B00100000; // Clear OC1B on match, set OC1B at BOTTOM (Non-Inverting-Mode) --> Pin12 // Enable this to get asecond PWM on Pin 12! His duty is set by OCR1B further below in this setup

    // Waveform Generation Mode: I want WGM12-WGM10 set to binary 110 --> Fast PWM, Top = ICR1 , Immediate update of OCR1ATCCR1A = TCCR1A | B00000010; // WGM11 = 1 & WGM10 = 0TCCR1B = TCCR1B | B00001000; // WGM12 = 1// Define TOP and the CompareICR1 = period;// ICR1 = TOP --> FastPWM with TOP = ICR1: Counts 0 to ICR1: --> drive PWM periodTCNT1 = duty1 - 1;// force immediate OCR1x compare on next tick

  • http://www.instructables.com/id/Arduino-Hardware-PWM-for-stepper-motor-drives/

    // Set the prescaler and an unknown (to me) bit which seems importantTCCR1B = TCCR1B | B00000001;// last 3 bits = B001 --> prescaler = 1TCCR1B = TCCR1B | B00010000;// Seems to be a read-only-bit? why set it? dont know. was this way online...// Without it, PWM-Duty HIGH is OK, but LOW is WAY too long... 30.4us instead of 4.6us.

    // Define the dury-cycles (Those can be set later on in te loop() as well!)proportional_duty1 = (unsigned long)period * (unsigned long)duty1 * 1UL;proportional_duty1_osca = (unsigned long)proportional_duty1 / 100 * 1UL;OCR1A = proportional_duty1_osca; // ON duration = drive pulse width of OCR1A --> PIN11// OCR1B = duty2; // ON duration = drive pulse width of OCR1B --> PIN12 // enable this to define the duty of the second PWM. See above forthe TCCR1A-setting to enable the PWM as it.

    I hope this code helps some people who need to make a FAST pwm.