Arduino Hardware PWM for stepper motor drives




With 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 for people 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 to have 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 use the 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 for use with stepper motors.

Arduino Hardware PWM for stepper motor drives




Step 1: Pulse Width Modulation

Pulse 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 states
  • Frequency – The number of times the signal repeats in a given time frame
  • Duty 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% duty cycle. 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 without delay 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 it is hard to guarantee that each loop will take exactly the same amount of time. Modern microcontrollers have dedicated hardware modules to generate a code independent square wave. Values for Duty cycle and Frequency are written to registers within the controller and the hardware generates the appropriate signal on the desired 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 written a library to simplify that process. The TimerOne library provides control over the HardwarePWM module connected to timer 1 (aptly). This library has easy functions for setting 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 the 0% and 100% values respectively.

Step 2: PWM for servo control

A servo is a small mechanical actuator that typically consists of a motor a potentiometer and some control electronics. The potentiometer allows the controller to determine 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 drive to 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 that indicates 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. There are better methods for controlling the servo to provide more accurate resolutions but they are beyond the scope of this instructable.

Arduino Hardware PWM for stepper motor drives schematic

Step 3: PWM for stepper motors

A 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 of steps 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 and simplify 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 direction of 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 is running can produce acceleration in the motor which will help the stepper reach it’s maximum top speed.




This Post / Project can also be found using search terms:

  • arduino microcontroller pwm to stepper motor
  • model elevator stepper motor pulse width modulation
  • pid controller with stepping motor
  • rısıng kontrolu step motor arduino
Clip to Evernote

Leave a Comment

(Spamcheck Enabled)

Read previous post:
Time Fountain
How to build your very own Time Fountain using Arduino

We have all seen the sweet videos on the tube! You know the magical water fountains that seems to "freeze"...

Close
Scroll to top