Projects   /   Motors & Robotics   /   PIC18F4550

Stepper Motor Driver using PIC18F4550 Microcontroller

Drive a 5-wire unipolar stepper from a PIC18F4550 in single, full and half-step modes, with a ULN2003 driver and a warning to keep 9V off the 5V PIC.

Published
Updated

At a glance

CategoryMotors & Robotics
PIC modelPIC18F4550
Reading time~3 min
Stepper Motor Tutorial
In this tutorial we are going to drive a Single unipolar stepper motor using PIC18F4550 Microcontroller in various different stepping modes. The source code and Project files are free to download at the end of this page. -
Stepper Motor Driver using PIC18F4550 MicrocontrollerStepper motor due its excellent features is very famous in between hobbyist for various robotic applications. Running DC, Stepper motor and servo is the most basic requirement in any robotic application which moves. Stepper motor’s ability to run in various modes with various speed and torque gives it a more degree of advantage over the simple DC motor for various projects, especially in robotics based projects. - See more at: http://www.rakeshmondal.info/Stepper-Motor-Driver-PIC18F4550-Microcontroller#Stepper-Motor-Driver

So in this basic stepper motor tutorial we will learn to drive a simple 5 wire unipolar stepper motor using a 40 pin PIC18F4550 microcontroller.

For a little more sophisticated Stepper motor Driver you can also follow my USB Stepper Motor Driver project which is quiet similar to current project , however this driver tutorial does not involve any interaction to any computer system directly except for writing the codes.

- See more at: http://www.rakeshmondal.info/Stepper-Motor-Driver-PIC18F4550-Microcontroller#Stepper-Motor-Driver

Stepper Motor Schematic

The stepper Motor can be also connected directly without ULN2003, but during the testing the results were not satisfactory hence a ULN2003 was added.

Caution!: 9V power supply is given to Stepper Motor which is not same as the voltage for the PIC18F4550 power supply (5V). Do not connect both the power supply together as the PIC18F4550 cannot bear more than 5V, both the supply must not be shot together or the microcontroller will be damaged.

If you have a single source of power supply for both the Stepper motor supply and Microcontroller, then add a MCP102 or an IC 7805 Voltage regulator (5v) before the VSS terminal of the Microcontroller, which would keep the input voltage to microcontroller at 5V.

Stepper Motor Wire Color Code
It is important to understand the color code of the stepper motor for identifying the Coils and Common wire before it can be connected across the Microcontroller pins. The diagram below shows which color cables are connected across which pin of the microcontroller.
Compiler IDE and Source code

The source code is compiled with MPLAB X IDE and X C8 Compiler which can be loaded into the microcontroller using pickit2 or JDM Programmer. It can be also compiled with Mplab and C18 compiler without much effort. For convenience I have added both versions of the source code at the end of this post with the compiled HEX file. For our Stepper Motor Driver we are not going to use any external Oscillator with the microcontroller.

There are two source code posted here, the source code 1 demonstrate the basic and simple Single Stepping mode of a stepper motor. The second source code is little more complex and has three other stepping modes (Single, FULL and HALF) cycling with delay of 5 seconds each.

Source Code 1

The first code demonstrates Single stepping mode for driving our 5 wire unipolar stepper Motor. Where the Stepper Motor coils are connected to PIC18F4550 pins, RD4, RD5, RD6 and RD7.

You would also require a header file which contains the various compiler directives, which is common for both the source codes 1 and source code 2.The delay has been set to 50000 microsecond or 50 milliseconds.

Stepper Motor Driver using PIC18F4550 MicrocontrollerIf in case if you want to use __delay_us( ); instead of __delay_ms( ); for adding delays then you can use some time converters or my favorite online scientific calculator ( eeweb calculator ) for ms to us conversion.

        /*      ****************** Steppermotor.c ******************      */

#include <p18f4550.h>
#include "headerzz.h";
#define _XTAL_FREQ 2000000

void main(void)
{
TRISDbits.TRISD0=0;          // Configuring Port D to output
TRISDbits.TRISD1=0;          // From pins RD4 to RD7
TRISDbits.TRISD2=0;
TRISDbits.TRISD3=0;

while(1)
{
LATDbits.LATD0=1;        // ON
LATDbits.LATD1=0;
LATDbits.LATD2=0;
LATDbits.LATD3=0;
__delay_ms(50);

LATDbits.LATD0=0;
LATDbits.LATD1=1;        // ON
LATDbits.LATD2=0;
LATDbits.LATD3=0;

__delay_ms(50);
LATDbits.LATD0=0;
LATDbits.LATD1=0;
LATDbits.LATD2=1;       //On
LATDbits.LATD3=0;

__delay_ms(50);
LATDbits.LATD0=0;
LATDbits.LATD1=0;
LATDbits.LATD2=0;
LATDbits.LATD3=1;       // On
__delay_ms(50);                     }      }

/* THE END */

Beginner Tutorials · PIC18F4550

PICDEM FS USB CDC Evaluation Board using PIC18F4550 with Proteus Simulation

Introduction The PICDEM FS USB CDC Evaluation Board is a practical microcontroller project built around the PIC18F4550, designed to demonstrate USB communication...

Peripheral Tutorials · PIC18F4550

PICDEM FS USB Mass Storage Device using PIC18F4550 with Proteus Simulation

Introduction This microcontroller project demonstrates how to simulate a USB Mass Storage Device (MSD) using the PIC18F4550 in a Proteus simulation environment....

Development Boards & Kits · PIC18F2550

Pinguino Project: A PIC Microcontroller Based Arduino

This is a complete tutorial to make a PIC Microcontroller based Arduino. This is a complete tutorial to make PIC microcontroller based...

Motors & Robotics

Real-Time CAN-Based Vehicle Monitoring System with PIC MCU

INTRODUCTION The Controller Area Network (CAN) is a robust vehicle communication bus standard that was primarily developed for automobile applications but has...

Motors & Robotics

Easy Way to Learn Robotics Using Microcontrollers: A Beginner’s Guide

The title, "Easy Way to Learn Robotics Using Microcontrollers," promises an accessible entry point for those curious about the exciting world of...

Motors & Robotics · PIC16F877A

Efficient Sun-Following Robot: A Solar Tracker with Microcontroller Control

Abstract The impending global challenge of dwindling non-renewable fuel resources underscores the urgency of transitioning towards renewable energy alternatives like solar power....

Ibrar Ayyub

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough...