Stepper Motor

In this blog post, I will talking about a stepper motor and will be interfacing it with the DSPIC30F4011.

A stepper motor is a brushless DC motor that divides a full rotation into a number of steps. The stepper motor we will be using for this module is the 28BYJ-48 which we will be operating at 5V. Each “Step” is basically a magnetic coil within the motor turning on, as the coil turns on the within the stator, the magnetic field created by the stator turns the rotor, as the rotor turns the next step is made by simply turning off another coil within the stator and off the previous coil. In this circuit diagram and image below show a perfect example of how the coils are wired and how the rotor turns within the motor. The circuit diagram below shows a total of four coils, however, these four coils are only there to represent the circuit electrically, the stator its self is made of many of these “single coils”, the GIF image below shows these coils magnetic field rotating.

This allows for great control as small degree’s of movement can be made with this motor

Typically due to the fact, the dspic cannot feed a high enough level for current to move motors, the SN754410NE driver chip will be used once again to drive the stepper motor, very similar to how the DC motor works.


Operational Code

// Written by Kevin T
// Code Written on the 16/12/16
// Based on the code written from Ted Burke
// or

#include <xc.h>
#include 			<libpic30.h>
#include <time.h>

#define STEP_DELAY 65000

// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF); // Watchdog timer off
_FBORPOR(MCLR_DIS); // Disable reset pin

//functions must FUNCTION
void full_step_forward();
void full_step_backward();
void half_step_forward();
void half_step_backward();
void steps();

int main()
int n = 0;

//configure digital I/O
TRISD = 0b0000; // one digital output for each winding
LATD = 0b0001; // all windings off to start with

n = n + 1;
if      (n <= 1 ) full_step_forward(); else if (n >= 1 ) full_step_backward();
else if (n <= 2 ) half_step_forward(); else if (n >= 2 ) half_step_backward();
else n = 0;

return 0;


void half_step_forward()
if      (LATD == 0b0001) LATD = 0b1001;
else if (LATD == 0b1001) LATD = 0b1000;
else if (LATD == 0b1000) LATD = 0b1100;
else if (LATD == 0b1100) LATD = 0b0100;
else if (LATD == 0b0100) LATD = 0b0110;
else if (LATD == 0b0110) LATD = 0b0010;
else if (LATD == 0b0010) LATD = 0b0011;
else                     LATD = 0b0001;


void full_step_forward()
if      (LATD == 0b0001) LATD = 0b1000;
else if (LATD == 0b1000) LATD = 0b0100;
else if (LATD == 0b0100) LATD = 0b0010;
else                     LATD = 0b0001;


void half_step_backward()
if      (LATD == 0b0001) LATD = 0b0011;
else if (LATD == 0b0011) LATD = 0b0010;
else if (LATD == 0b0010) LATD = 0b0110;
else if (LATD == 0b0110) LATD = 0b0100;
else if (LATD == 0b0100) LATD = 0b1100;
else if (LATD == 0b1100) LATD = 0b1000;
else if (LATD == 0b1000) LATD = 0b1001;
else                     LATD = 0b0001;


void full_step_backward()
if      (LATD == 0b0001) LATD = 0b0010;
else if (LATD == 0b0010) LATD = 0b0100;
else if (LATD == 0b0100) LATD = 0b1000;
else                     LATD = 0b0001;


Unfortunately, my code does not transition between direction very quickly this video has been brought up to twice the original speed of the video recorded, so please do skip to 0:35, 1:10, 1:45, and 2:20 to see the direction change. And yes I did record that video for 5 minutes I would of speed up the video a bit more, however, my phone only allows me to speed it up to twice the speed max, hopefully, the background music. enjoy!