Stepper Motor Control with AndroiDAQ

Stepper motors are common motion control actuators that are used in robotics, radio controlled devices, 3D printers, and other motion driven systems. Using stepper motors with AndroiDAQ is easy. This article contains a brief history of stepper motors, some basic operational background for stepper motors, and how to connect and use stepper motors with the AndroiDAQ data acquisition and control module.

 

Stepper motors, which are part of the DC motor family, became popular in the 1960s, as they were originally conceived of as a low-cost alternative to servo motors. As we now know, they were quickly adopted by the then upcoming computer industry for peripheral applications. A stepper motor is typically a brushless DC motor that divides the full rotation of the motor into an equal number of steps. The motor's position can be commanded by a stepper motor controller to move and then hold to any one of these steps without using feedback. Stepper motors are considered an open-loop system due to this lack of feedback; however, this lack of feedback limits their positional performance. A stepper motor can only drive a load that is well within its load capacity. If this load capacity is exceeded, the motor can stall, causing missed commanded steps. This leads to positioning errors and the degradation of positional performance. The main advantage of stepper motors is that they can provide open-loop position control for a fraction of the cost of a servo system requiring feedback.

 

A stepper motor is controlled by two circuits: a controller circuit, which generates step pulses and a directional signal, and a driver circuit, which receives the step pulses and directional signal from the controller circuit and then generates the necessary current and direction to drive the stepper motor. The controller circuit is typically a digital logic device which requires relatively little power, such as a microcontroller or the AndroiDAQ module. The driver circuit typically contains power transistors, which are on board or off board of the driver integrated circuit, to provide isolated power to drive the connected stepper motor. Isolated power is used to protect the digital control circuitry from current and noise spikes that occur during stepper motor operation. For this article, the AndroiDAQ module will be used for the stepper motor controller circuit and a MC3479 Stepper Motor Driver chip by ON Semiconductor will be used for the stepper motor driver circuit. You can find more information about the MC3479 IC here:

http://www.onsemi.com/pub_link/Collateral/MC3479-D.PDF.

There are other considerations for designing a motion control system using stepper motors, such as stepper motor type and stepper motor drive modes. These topics are thoroughly covered by the many articles on the Internet, so I will leave it up to you to review these topics for your application.

 

Below is a diagram that shows how to connect the various signals of the MC3479 stepper motor driver chip to a stepper motor and the AndroiDAQ module. For this article we will use AndroiDAQ’s I/O pin 3 to control the direction of the stepper motor’s rotation and I/O pin 4 to provide the step pulses. You can use whichever AndroiDAQ I/O pins you want to for your project; you need only to remember to change the pin number in the AndroiDAQ firmware to match your application. For this article I used an old surplus stepper motor that I dig out of an old dot matrix printer I had lying around. This stepper has six wires which I was able to ohm out with a digital VOM meter to find which wire group was for which winding of the stepper motor. Here is a good article for sorting out your surplus stepper motor’s wiring to windings:

http://digital.ni.com/public.nsf/allkb/0AEE7B9AD4B3E04186256ACE005D833B

 

AndroiDAQ Stepper Connection Diagram 1

Figure 1 MC3479 Stepper Motor Driver Connections for AndroiDAQ

After following the diagram above, you will have a fully connected stepper motor with its associated stepper motor driver and stepper motor controller. Now we can explore how to move the stepper motor with the AndroiDAQ module. We know that the controller circuit creates step pulses and a directional signal which the driver circuit receives to generate the necessary current to drive the stepper motor in the direction that we want it to go. To do this, we could use the AndroiDAQ DEMO application for Android connected via USB, Bluetooth, or Wi-Fi. Then in the AndroiDAQ DEMO application, set AndroiDAQ’s I/O pin numbers 3 and 4 to outputs, then swipe to the Outputs page and set pin 3 as a digital output which is set to high, and then set pin 4 to a pulse output and set it to an output value of say 10-hertz. If all is connected correctly, when you press the “Send Output Values to AndroiDAQ” button, the stepper motor should turn at 10 steps per second, counter-clockwise. Now if you set pin 3 to low and send the output values again to the AndroiDAQ module, the stepper motor will turn at 10 steps per second, clockwise. 

 

This very simple example of driving a stepper motor with the AndroiDAQ DEMO application is great, if you only need to move the stepper motor forward or backward for driving let’s say a small robot across the floor, but how can we get the open-loop positional control that we discussed earlier? We know that stepper motors divide the full rotation of the motor into an equal number of steps. It is this equal number of steps per rotation that provides the positional control for stepper motors. The surplus motor that I am using for this article is divided into 192 steps per revolution, so each step represents 1.875° (360°/192-steps). Your stepper motor most likely will have different angular divisions per full rotation, as some steppers have a wider degree of angle per step and some have a narrower degree of angle per step. Your stepper motor’s data sheet should state its divisions per revolution, which is usually stated in degrees, or you can manually find out your motor’s step angle like I had to with the surplus motor, due to its age, by manually counting the full revolution of steps for the motor.

 

So we now know how many steps per revolution the stepper motor has. With this data we can now tell the stepper motor to move 100 steps and know how far it has moved, in my case 100 steps represents 187.5° of movement. By giving the stepper motor a command of how many steps to move and in what direction, motion control can occur, but to really control motion accurately, we need a zero start point. To explain what a zero start point is and why it is important: imagine a motion control system where the system was stopped midway in its motion process. Without feedback, like a servo motor has, we now have no idea where the shaft of the stepper motor is in its rotation. If a command was sent to the stepper motor to move the motor beyond the limits of the motion control system, damage could occur as the motor overdrives the system’s limits.

 

It is the motion control system’s limits that are important when using a stepper motor for motion control system applications. These motion controlled system limits are usually guarded by limit switches and/or stops when using a stepper motor. A properly designed motion control system will have as least one of these stops to provide to the motion control system its zero start point, and perhaps another for its full scale stop limit point. To further explain this, remember when you first observed an old dot matrix printer and what happened when you sent a document file to it to print. First the printer loaded the paper and then it adjusted the paper’s position to ensure that the print started at the topmost margin setting. This initialization and movement of the paper before printing gave this motion controlled printing system its zero start point and the page was printed out in the format that was expected.  

 

So how do we incorporate a zero start point when using stepper motors with AndroiDAQ? As stated above, limit switches are typically used to give this data. Luckily, the old dot matrix printer that I scavenged had a few limit switches that I could use for this article. A limit switch typically has a long paddle to actuate the switch and also two wires that allows one to measure if the switch is open or closed. Connecting one of these wires to 3.3-volts and the other wire to one of AndroiDAQ’s I/O pins, I used I/O pin 14 for this article, we can now have the AndroiDAQ module monitor and tell us the condition of the limit switch: open (no voltage or low)  shut ( 3.3-volts or high).

 

Now if we mechanically connect the limit switch to the stepper motor such that the limit switch’s paddle hits some build-on mechanical limit, like a protruding screw head on the stepper motor’s shaft, the limit switch can now be used to sense when the limit switch hits that position. We will use this exact point, when the limit switch closes, as our zero start point for controlling the stepper motor’s position. For this article, I used a screw head that was sticking out from the stepper motor shaft coupling. You may need to be creative to implement a limit switch for your zero start point for your project.

 

We now have a simple stepper motor motion control system that consists of: the AndroiDAQ module as the stepper motor controller, a MC3479 IC chip as the stepper motor driver, and a stepper motor with a build-on limit switch to tell us our zero start point for the system (see diagram below).

AndroiDAQ Stepper Motor Connections Diagram 2

 

Figure 2 Connection Diagram Stepper Motor to AndroiDAQ

We can now derive a software subroutine and modify the AndroiDAQ firmware to move the stepper motor x amount of steps in the y direction. This subroutine will also monitor if the limit switch closes so that the subroutine will stop the stepper motor from going any further. To use this subroutine to move your stepper

 

PUB TestLoop  | PinA, PinB, steps

''Test loop for users to test their methods Serial Menu number 03

  steps := 10

  dira[14] := 0                                           'Set Pin 14 to input -is our limit switch

  PinA := 4                                                'Pin 4 sents steps pulses

  PinB := 3                                                'Pin 3 sets CW or CCW

  dira[PinB] := 1                                       'Set Pin 3 as Output

  outa[PinB] := 0                                       'Set Pin 3 high CW rotation

  dira[PinA] := 1                                       'Set Pin 4 as Output

                     

  repeat steps                                             'Repeat loop for # of 'steps'

     if ina[14] <> 1                                      'Repeat unless Pin 14 is high

        outa[PinA] := 1                                 'Set Pin 4 to high

        waitcnt((clkfreq/10)  + cnt)               'Small delay between high and low

        outa[PinA] := 0                                 'Set Pin 4 to low

   

  MenuLoop

 

motor, copy it into the TestLoop subroutine of the AndroiDAQ firmware and then enter how many steps you desire in the ‘steps’ variable and set whether PinB (pin 3 for this example) is outputting a high (1) or a low (0) to control the stepper motor rotational direction. Also remember to set the I/O pin number where you connected the limit switch, I used I/O pin number 14 for this article. After programming the firmware (by pressing F10 to copy to RAM or F11 to copy to the EEPROM, in the Propeller Tool) simply type a 03 in Parallax’s Serial Terminal, or another serial terminal, and the subroutine will trigger and move your stepper motor the ‘steps’ variable amount of steps in the outa[Pin 3] direction. If the subroutine senses that the limit switch is closed, via the ina[14] statement, your stepper motor movement will stop.

 

The above example is not very dynamic as it uses static values for the steps and direction variables. We have included in the newest version of the AndroiDAQ firmware a stepper motor driver that we found and implemented from Parallax’s OBEX, where community members share "open source" objects and snippets that are written for Parallax Semiconductor's Propeller microcontroller.  This OBEX exchange library is at: http://obex.parallax.com/content/welcome-new-obex. The stepper motor driver that AndroiDAQ uses is called NCOburst.spin written by Tracy Allen. This stepper driver generates a set number of pulses at a set frequency, meaning, you send your desired number of steps and desired direction, and the NCOBurst subroutine takes care of the rest. A neat feature about the NCOBurst subroutine is that you can match the frequency of the burst pulses to the number of steps that your stepper motor has. This allows for a very fast response time from the stepper motor as one rotation can equal one second in time, or faster, if your application deems this necessary.

 

Below is the new subroutine that is now included in AndroiDAQ’s firmware. The subroutine for stepper motors is called MoveStepper and it is included in AndroiDAQ’s command menu system (menuloop) where a 015 command can be sent via the UART connection to start the subroutine. After the subroutine starts, it waits for you to enter the number of steps that you want the stepper motor to move. For example, if you want to move your stepper motor 120 steps, you would send a 0120. Then the subroutine waits for you to send a code that controls the rotational direction for the stepper motor. Sending a 00 will rotate your stepper motor clockwise or sending a 01 will rotate your stepper motor counter-clockwise. If you are using AndroiDAQ in a larger application, you can send a complete serial command sequence to the AndroiDAQ module to move your stepper motor. For example, sending: 015\r0120\r01 to AndroiDAQ causes the MoveStepper subroutine to move the stepper motor 120 steps in the counter-clockwise direction.

 

PUB MoveStepper | PinA, PinB, pulses, direction

''Move Stepper motor X steps via pulses, can control direction as well

''via PinB. Menuloop number 15

  menu := 0

  PinA := 4

  PinB := 3

  'myFreq := 192

  direction := 0 

  repeat

    menu := debug.rxcheck                                   'This section allows menu driven actions via USB

    if menu > 0

        pulses := debug.GetDec

        direction := debug.GetDec

        if direction == 1

          dira[PinB] := 1

        else

          dira[PinB] := 0

        if pulses > 0

          burst.make(PinA, myFreq, pulses)

        quit   

  MenuLoop

 

To demonstrate further how AndroiDAQ utilizes the zero start point that we installed on our stepper motor motion control system, the AndroiDAQ firmware has another new subroutine called CenterPanTilt. You can see how this subroutine works by sending AndroiDAQ the command of 013. This will start the CenterPanTilt subroutine and move the stepper motor clockwise until the limit switch closes and then the subroutine will move the stepper motor counter-clockwise 96 steps, or halfway for my stepper motor set up. This subroutine is used for a pan and tilt operation that uses both a stepper motor and a servo motor, but that is for another article; though I included the CenterPanTilt code below to wet your whistle.

 

PUB CenterPanTilt | PinA, PinB, pulses, thisFreq                          'Test loop for users to test their methods

''Center Pan/Tilt assembly for motion tracking Menu number 13

  dira[14] := 0

  PinA := 4

  PinB := 3

  thisFreq := 96

  pulses := 1

  dira[PinB] := 1

    

  repeat while ina[14] == 0

    debug.str(String("Centering: "))

    debug.tx(CR)

    debug.tx(LF)

    burst.make(PinA, thisFreq, pulses)

    'waitcnt(clkfreq/1000 + cnt)

  dira[PinB] := 0 

  burst.make(PinA, thisFreq, 102)

  SERVO.leftPulseLength(1500)  

  debug.str(String("Done: "))

  debug.tx(CR)

  debug.tx(LF)

  MenuLoop

  

This article describes stepper motors and how simple it is to use stepper motors with the AndroiDAQ module. Please look for our other articles that will provide instructions on how to use servo motors and more with the AndroiDAQ module. Servo motors are also quite easy to use with the included Servo motor driver in the AndroiDAQ firmware. In future articles, we will combine the servo and stepper motor operations with AndroiDAQ and then create a simple webcam pan and tilt mechanism that can be used for following or targeting a subject in the cameras field of view using OpenCV.

We invite you to read more about the AndroiDAQ data acquisition module for Android, LabVIEW, JAVA, and Python: About the AndroiDAQ module.

 

AndroiDAQ with xBee WiFi module