Code
This code use four step four pat mode to drive the stepping motor forward and reverse direction.
C Code 16.1.1 SteppingMotor
First observe the project result, and then analyze the code.
1. Use cd command to enter 16.1.1_SteppingMotor directory of C code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/C_Code/16.1.1_SteppingMotor
2. Use following command to compile "SteppingMotor.c" and generate executable file "SteppingMotor".
gcc SteppingMotor.c -o SteppingMotor -lwiringPi
3. Run the generated file "SteppingMotor".
sudo ./SteppingMotor
After the program is executed, the stepping motor will rotate 360° clockwise and then 360° anticlockwise,
circularly.
The following is the program code:
#include <stdio.h>
#include <wiringPi.h>
const int motorPins[]={1,4,5,6}; //define pins connected to four phase ABCD of
stepping motor
const int CCWStep[]={0x01,0x02,0x04,0x08}; //define power supply order for coil for
rotating anticlockwise
const int CWStep[]={0x08,0x04,0x02,0x01}; //define power supply order for coil for
rotating clockwise
//as for four phase stepping motor, four steps is a cycle. the function is used to drive
the stepping motor clockwise or anticlockwise to take four steps
void moveOnePeriod(int dir,int ms){
int i=0,j=0;
for (j=0;j<4;j++){ //cycle according to power supply order
for (i=0;i<4;i++){ //assign to each pin, a total of 4 pins
if(dir == 1) //power supply order clockwise
digitalWrite(motorPins[i],(CCWStep[j] == (1<<i)) ? HIGH : LOW);
else //power supply order anticlockwise
digitalWrite(motorPins[i],(CWStep[j] == (1<<i)) ? HIGH : LOW);
printf("motorPin %d, %d \n",motorPins[i],digitalRead(motorPins[i]));
}
printf("Step cycle!\n");
if(ms<3) //the delay can not be less than 3ms, otherwise it will exceed
speed limit of the motor
ms=3;
delay(ms);
}
}