microframework.dk

.NET softwired

Controlling a Stepper Motor with .NET MicroFramework

This post illustrates how you can control a unipolar stepper motor from .NET MicroFramework using very simple hardware.

The motor which I have scavenged from an old 5 1/4" floppy disk drive (do you remember those?) is controlled by a dedicated Stepper Motor control class written in C#.



You can start out with a look at the short video on YouTube that I made to show the motor running:

 


Update 2015.

The code running in the video, is as follows.

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;

namespace PFJ.NETMF.Hardware.Motor
{
    public class StepperMotor2003
    {
        private byte[] pattern1 = { 1, 8, 4, 2};

        OutputPort port1;
        OutputPort port2;
        OutputPort port3;
        OutputPort port4;

        private int delay;
        public int Delay
        {
            get { return delay; }
            set { delay = value; }
        }

        private int index;

        public StepperMotor2003(Cpu.Pin Pin1, Cpu.Pin Pin2, Cpu.Pin Pin3, Cpu.Pin Pin4)
        {
            port1 = new OutputPort(Pin1, true);
            port2 = new OutputPort(Pin2, false);
            port3 = new OutputPort(Pin3, false);
            port4 = new OutputPort(Pin4, false);

            index = 0;
            SetPattern(pattern1[index]);

            this.delay = 2;
        }

        private void SetPattern(byte p)
        {
            switch (p)
            {
                case 1:
                    port1.Write(true);
                    port2.Write(false);
                    port3.Write(false);
                    port4.Write(false);
                    break;
                case 2:
                    port1.Write(false);
                    port2.Write(true);
                    port3.Write(false);
                    port4.Write(false);
                    break;
                case 4:
                    port1.Write(false);
                    port2.Write(false);
                    port3.Write(true);
                    port4.Write(false);
                    break;
                case 8:
                    port1.Write(false);
                    port2.Write(false);
                    port3.Write(false);
                    port4.Write(true);
                    break;
            }
        }

        public void StepRight()
        {
            index = index == 3 ? 0 : index += 1;

            SetPattern(pattern1[index]);
            Thread.Sleep(this.delay);
        }

        public void StepRight(int Steps)
        {
            int i = 0;
            for (int r = 0; r < Steps; r++)
            {
                SetPattern(pattern1[System.Math.Abs(i)]);
                i += 1;
                i = i % 4;
                Thread.Sleep(this.delay);
            }
        }

        public void StepLeft()
        {
            index = index == 0 ? 3 : index -= 1;

            SetPattern(pattern1[index]);
            Thread.Sleep(this.delay);
        }

        public void StepLeft(int Steps)
        {
            int i = 3;
            for (int r = 0; r < Steps; r++)
            {
                SetPattern(pattern1[System.Math.Abs(i)]);
                i = i == 0 ? 3 : i -= 1;
                Thread.Sleep(this.delay);
            }
        }

        public void Wait(int Delay)
        {
            Thread.Sleep(Delay);
        }
    }
}

Peter