stepper motor (bipolar motor driving) library and sample codes


このページは日本語でも記載されています.日本語版はこのページ後半をご覧ください. This page is written in Japanese as well. Please find it in 2nd half of this page.

What is this?

A class library and its sample code for the stepper (stepping) motor drive.

The mbed drives a stepper motor but a motor driver chip. Because the mbed cannot drive the motor directly which does not have enough current capacity.

The stepper motor can be driven with two signals with appropriate phase pulse sequence. This library let the mbed to generate those signals.

Next picture is a simplified model of the stepper motor operation (simplified to explain the mechanism easer). You can see the switching of coil current direction in each timings makes the rotor turn. The mbed with this library manages these signal timing and phase. The example of this picture has only 4 position (angle) in one rotation (in two phase operation) but actual motor has more position when the signals making one of those phase.

Code :

The code is available as a library : StepperMotor

And sample codes are available.

Just a simple code to operate one motor: StepperMotor_HelloWorld

Bit complex sample is also available that operates 3 stepper motors simultaneously : AnalogClock_StepperMotor_NTP

This analog clock demo is a code which has been demonstrated in Make: Tokyo Meeting 06 (@Tokyo Institute of Technology on 20 & 21 Nov 2010). It demonstrates the stepper motors and NTP access capability via internet connection. The NTP access code is from .

#include "mbed.h"
#include "StepperMotor.h"

BusOut          leds( LED4, LED3, LED2, LED1 );
StepperMotor    m( p21, p22, p23, p24 );

int main() {
    m.set_sync_mode( StepperMotor::SYNCHRONOUS );
    m.set_power_ctrl( true );

    while ( 1 ) {
        leds    = 1;
        m.go_angle( 120 );
        wait( 0.5 );

        leds    = 2;
        m.go_angle( 240 );
        wait( 0.5 );

        leds    = 3;
        m.go_angle( 0 );
        wait( 0.5 );

        leds    = 4;
        m.go_angle( 240 );
        wait( 0.5 );

        leds    = 5;
        m.go_angle( 120 );
        wait( 0.5 );

        leds    = 6;
        m.go_angle( 0 );
        wait( 0.5 );

Hardware :


To simplify the hardware configuration, it uses only one power supply from USB bus (from mbed's VU output). The USB supply should be taken from root hub (USB ports on a PC) or self power hub. If bus power hub is used for the supply, it may have problem of the current capacity (self powered hub: >500mA/port, bus powered hub: = 100mA/port).

If the motor doesn't have enough torque, higher voltage supply can be applied on motor power supply pins on the TA7774PG (pins 9 and 16).

Stepper motor:

The motor I could buy was a Copal SPG20-332. It's not a bipolar motor but it may be OK to use as an bipolar. The unipolar motor has a middle point of each coils but just ignoring this connection, it can be used as a bipolar motor.


In figure above, the signalA and the signalB are necessary for the motor operation. Other 2 signals (Power control output and Position sensor input) are option. The power control is a signal to the TA7774PG that controls the POWER-OFF(Low) and OPERATION(High) on Vs2_b pin (pin8). The Vs2_b pin has a pull up resister because this signal is an option. The pull-up resister ensures that the pin has High state if it is not connected any control output. The power control can be enabed by set_power_ctrl( true ). It makes power control signal low (POWER-OFF) when no motor turn required.

An option input is available called position_detect (position sensor input). This can be used for the sensor (output of a photointerruptor, etc.) input to detect home position of the motor. User can set the edge of rise or fall for the detection. When the find_home_position( StepperMotor::RISING_EDGE | StepperMotor::FALLING_EDGE ) called, it starts to turn the motor (1 rotation). And then when the defined signal edge detected, the motor is stopped and the home position will be set to current position.


Multiple motor operation:

Multiple motors can be used. The analog clock applicaition shows a sample of 3 hands (hour, minute and second) clock driven stepper motors.

StepperMotor    m_s( p21, p22, p23, p30 );  //  motor for second
StepperMotor    m_m( p24, p25, p26, p30 );  //  motor for minute
StepperMotor    m_h( p27, p28, p29, p30 );  //  motor for hour

Since the position_detect (position sensor input) is an input, it can be shared by multiple sensors [THIS IS NOT TESTED, SORRY]. But if the sensor shared configuration taken, the find_home_position() should be done one after the other (or sensor sharing declaration may be good if you don't use the sensors).

Operation modes:

There are several modes and settings for operation. The functions of set_sync_mode() and set_rot_mode() are for SYNC, ROTATE mode setting. With set_pps() function, the motor speed can be controlled.

set_sync_mode( StepperMotor::SYNCHRONOUS | StepperMotor::ASYNCHRONOUS ) sets the motor operation synchronised to the program execution or not. Three function makes the motor to turn to specified position (target position): go_angle(), go_position() and move_steps(). If the SYNCHRONOUS is set, the program execution will be kept waiting inside of those functions until motor rotation complete.If ASYNCHRONOUS is set (default), the function will return immediately and program can work for something else.

set_rot_mode( StepperMotor::SHORTEST | StepperMotor::NO_WRAPAROUND | StepperMotor::CLOCKWISE_ONLY | StepperMotor:: COUNTER_CLOCKWISE_ONLY ) defines rotate direction when the motor works. If it is in SHORTEST mode (default), motor will turn the way shortest to target position. NO_WAPAROUND is to avoid the motor don't turn across the home position. CLOCKWISE_ONLY and COUNTER_CLOCKWISE_ONLY are to make one directional turn.






















アナログ時計のコードは20101120日,21日に東京工業大学で開催されたMake: Tokyo Meeting 06でデモされたものです.






(セルフパワー・ハブ >500mA/port,バスパワー・ハブ = 100mA/port)









Power制御はset_power_ctrl( true )によってイネーブルされます.これがイネーブルされている間,モータが動作しない状態であるときにTA7774PGPOWER-OFF状態になるようにします.


検出をどちらのエッジ(立ち上がりまたは立ち下がり)で検出するのかを選択可能です.find_home_position( edge )


Please log in to post a comment.