Simple Stepper Motor 4-Phase 5-Wire with ULN2003 Driver

Dependencies:   mbed

Committer:
lasmahei
Date:
Wed Apr 02 10:09:19 2014 +0000
Revision:
0:481ac50cc86b
Added Brief Description

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lasmahei 0:481ac50cc86b 1 // 28BYJ-48 stepper motor example
lasmahei 0:481ac50cc86b 2 // showing how to control a unipolar stepper motor by mbed digital output ports.
lasmahei 0:481ac50cc86b 3 // Tested on the Nucleo F103RB Board
lasmahei 0:481ac50cc86b 4 // Based on http://www.geeetech.com/wiki/index.php/Stepper_Motor_5V_4-Phase_5-Wire_%26_ULN2003_Driver_Board_for_Arduino
lasmahei 0:481ac50cc86b 5 // Using the ULN2003A Driver.
lasmahei 0:481ac50cc86b 6
lasmahei 0:481ac50cc86b 7 #include "mbed.h"
lasmahei 0:481ac50cc86b 8
lasmahei 0:481ac50cc86b 9 BusOut motor_out(PC_0, PC_1, PC_2, PC_3); // blue - pink - yellow - orange
lasmahei 0:481ac50cc86b 10
lasmahei 0:481ac50cc86b 11 int step = 0;
lasmahei 0:481ac50cc86b 12 int dir = 1; // direction
lasmahei 0:481ac50cc86b 13
lasmahei 0:481ac50cc86b 14 int main()
lasmahei 0:481ac50cc86b 15 {
lasmahei 0:481ac50cc86b 16 while(1)
lasmahei 0:481ac50cc86b 17 {
lasmahei 0:481ac50cc86b 18 switch(step)
lasmahei 0:481ac50cc86b 19 {
lasmahei 0:481ac50cc86b 20 case 0: motor_out = 0x1; break; // 0001
lasmahei 0:481ac50cc86b 21 case 1: motor_out = 0x3; break; // 0011
lasmahei 0:481ac50cc86b 22 case 2: motor_out = 0x2; break; // 0010
lasmahei 0:481ac50cc86b 23 case 3: motor_out = 0x6; break; // 0110
lasmahei 0:481ac50cc86b 24 case 4: motor_out = 0x4; break; // 0100
lasmahei 0:481ac50cc86b 25 case 5: motor_out = 0xC; break; // 1100
lasmahei 0:481ac50cc86b 26 case 6: motor_out = 0x8; break; // 1000
lasmahei 0:481ac50cc86b 27 case 7: motor_out = 0x9; break; // 1001
lasmahei 0:481ac50cc86b 28
lasmahei 0:481ac50cc86b 29 default: motor_out = 0x0; break; // 0000
lasmahei 0:481ac50cc86b 30 }
lasmahei 0:481ac50cc86b 31
lasmahei 0:481ac50cc86b 32 if(dir) step++; else step--;
lasmahei 0:481ac50cc86b 33 if(step>7)step=0;
lasmahei 0:481ac50cc86b 34 if(step<0)step=7;
lasmahei 0:481ac50cc86b 35 wait(0.015); // speed
lasmahei 0:481ac50cc86b 36 }
lasmahei 0:481ac50cc86b 37 }
lasmahei 0:481ac50cc86b 38