Control a Stepper Motor with the PICSimLab Step Motor Spare Part
Stepper motors move in discrete steps driven by a repeating coil pattern, which makes them a clean simulation subject: if the sequence and timing are right, the motor turns; if not, it jitters. PICSimLab’s Step Motor spare part lets a PIC16F877A drive a virtual stepper so you can learn full-step sequencing without a motor, a driver board or a power supply.
What you need
- PICSimLab 0.9.3 — see the download page.
- MPLAB X IDE and the XC8 compiler.
- Board: McLab2 or PICGenios, microcontroller PIC16F877A.
- One Step Motor spare part (Outputs category).
Wiring used in this tutorial (example)
- Step Motor control inputs 1–4 → RD0–RD3
Connect the four control lines in the part’s Properties window (right-click the part → Properties). A note for real hardware: a microcontroller pin cannot drive motor coils directly — a real build needs a driver stage (a transistor array or H-bridge module) between the PIC and the motor. The spare part models the motor side in simulation, which is precisely why it is handy for learning the sequence logic first.
Steps
1. Generate the project and add the part
Create the MPLAB X project for the PIC16F877A with the Code Project Wizard. Open Modules → Spare parts, add Outputs → Step Motor and wire its four inputs to RD0–RD3 in Properties.
2. Learn the full-step sequence
Two-phase-on full stepping energises two coils at a time in this repeating pattern on RD3–RD0: 1001, 1100, 0110, 0011 (0x09, 0x0C, 0x06, 0x03). Stepping forward through the table turns the motor one way; stepping backward reverses it. The delay between steps sets the speed — too short and a real motor would skip steps, which is a habit worth building even in simulation.
3. Build, load and run
Build in MPLAB X, load the .hex and run. The example rotates forward for 48 steps, pauses, then rotates back — watch the motor part respond, and probe RD0–RD3 with Modules → Oscilloscope if you want to see the four phase signals shift along.
Example code (XC8)
// Example: full-step stepper drive on RD0-RD3 - PIC16F877A, XC8
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = ON
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
#include <xc.h>
#define _XTAL_FREQ 20000000 // Example: match the board clock
// Two-phase-on full-step sequence
static const unsigned char step_table[4] = {0x09, 0x0C, 0x06, 0x03};
static unsigned char step_index = 0;
void step_forward(void) {
step_index = (step_index + 1) & 0x03;
PORTD = step_table[step_index];
}
void step_reverse(void) {
step_index = (step_index - 1) & 0x03;
PORTD = step_table[step_index];
}
void main(void) {
TRISD = 0x00; // RD0-RD3 outputs to the Step Motor part
PORTD = 0x00;
while (1) {
for (unsigned char i = 0; i < 48; i++) { // forward 48 steps
step_forward();
__delay_ms(20); // step rate = speed
}
__delay_ms(500);
for (unsigned char i = 0; i < 48; i++) { // and back again
step_reverse();
__delay_ms(20);
}
__delay_ms(500);
}
}
Try next
- Timers and interrupts — generate the step pulses from a timer for jitter-free speed.
- Oscilloscope basics — verify the spacing of the four phase outputs.
- ADC potentiometer — use a knob to set the motor speed.
Troubleshooting
The motor vibrates but does not turn. The phase order is wrong: one swapped control line in the part’s Properties (or in the step table) produces exactly this symptom. Check the RD0–RD3 mapping first.
It turns the wrong way. Not a bug — reverse the table order (or swap the forward/reverse functions). Direction is purely a matter of sequence order in a stepper.
