Controlling a Stepper Motor with a Microcontroller.

It all started when one of my devices needed to rotate a motor shaft a certain number of turns; high precision wasn't required, plus or minus a quarter turn, so I decided to do it with a DC motor. An Atmega16 microcontroller was chosen as the control element, but since the output voltage of the Atmega16 pins is only 5 volts and the maximum current must not exceed 20mA, while controlling the motor requires a current of about ten amps and a voltage of several tens of volts, you can't control the motor directly. You need some intermediary that accepts at its input what the microcontroller can deliver and outputs what the motor wants to get; such an intermediary is easy to find among logic-level IRL series field-effect transistors.
Controlling a Stepper Motor with a Microcontroller.

After the first run it became clear that we wanted one thing and got another: after the transistor turned off, the motor kept spinning for more than one turn. That makes sense: for the shaft to stop, the energy it accumulated while spinning has to be dissipated.

And this brings us to one of the main advantages of a stepper motor: the ability to position precisely.

There are three main types of stepper motors:
  • variable reluctance motors
  • permanent magnet motors
  • hybrid motors

In variable reluctance motors, the rotor is made of a soft magnetic material, i.e. a material that can quickly become magnetized and quickly lose its magnetic properties when the magnetic field is removed. Torque is created by the stator's magnetic fluxes, and the rotor always positions itself so that the magnetic flux created by the stator closes through it. To make the rotor turn, you need to switch off the current in one winding and switch it on in another.
Controlling a Stepper Motor with a Microcontroller.

In a permanent magnet motor, the rotor is divided into poles. When current is applied to one of the windings, the rotor takes a position in which opposite poles of the rotor and stator face each other.
Controlling a Stepper Motor with a Microcontroller.

Hybrid motors combine the advantages of the motors described above and have better characteristics: a smaller step angle, higher torque and higher speed, and as a result they cost more.

Let's move on to practice: for the experiment we'll take the MITSUMI M42SP-5 stepper motor with a 7.5° step, shown in the picture.
Controlling a Stepper Motor with a Microcontroller.

Download its datasheet and find the table.
Controlling a Stepper Motor with a Microcontroller.

The table shows that these motors come in two types:
  • 12-volt, with a winding resistance of 50 Ohm
  • 24-volt, with a winding resistance of 120 Ohm


The motor diagram is shown in the picture below.
Controlling a Stepper Motor with a Microcontroller.

5 wires come out of the motor: 4 phases and one common. The common wire can be found with a multimeter: the resistance between it and each of the other 4 wires equals the phase resistance, in our case 50 Ohm.

As written above, we need an intermediary that takes 20mA from the microcontroller and gives the motor 259mA. We could take a field-effect transistor as before, but there's a catch: there are four phases here, and for this case there's a simpler solution: use the ULN2003 chip.
Controlling a Stepper Motor with a Microcontroller.

The ULN2003 consists of 7 Darlington transistors, each of which can pass up to 500mA, with an operating voltage of up to 50V, which is quite enough for us. In fact, each ULN2003 cell consists not only of a Darlington transistor but also includes all the necessary support components; its structure can be seen in the datasheet.
Controlling a Stepper Motor with a Microcontroller.

A Darlington transistor, or Darlington pair, is two connected transistors; its distinctive feature is a high current gain, equal to the product of the gains of each transistor (βc = β1 ∙ β2).

You can rotate the motor by energizing the windings one after another, with only one winding on at any moment; in this case the motor works in full-step mode, i.e. it rotates 7.5° with each winding switch.
Controlling a Stepper Motor with a Microcontroller.

Similarly, you can energize two adjacent windings at once; this method increases the holding torque, and the motor still works in full-step mode.
Controlling a Stepper Motor with a Microcontroller.

You can also run the motor in half-step mode, i.e. the rotor rotates 3.75° with each winding switch.
Controlling a Stepper Motor with a Microcontroller.

For convenient control, you can create an array in which the element index is the step number and the element value indicates which windings should be on.

uint8_t step[8] = {0x01, 0x03 ,0x02, 0x06,0x04, 0x0c,0x08,0x09};

If you send the elements of this array to the microcontroller port one after another, the motor will work in half-step mode; if every other one, in full-step mode. To send values to the port, it's convenient to use the timer overflow interrupt; by changing the time between interrupts, we can regulate the motor speed.