3

I am working on a project which requires operating bipolar stepper motor with Raspberry Pi 3 and Windows 10 IoT core. I am writing the program in C#.

Currently, I am getting the pulses on the motor and it is producing little movement, but I am not getting a full revolution from the motor. I am using L298n motor shield as a motor driver. I have pasted the source code below.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Gpio;
using System.Threading.Tasks;



namespace Stepper
{

    public sealed partial class MainPage : Page
    {
        private const int orange = 5;
        private const int blue = 13;
        private const int red = 19;
        private const int yellow = 26;

        private GpioPin orangePin;
        private GpioPin bluePin;
        private GpioPin redPin;
        private GpioPin yellowPin;

        public MainPage()
        {
            this.InitializeComponent();
            InitGPIO();
            step1();
           step2();
            step3();
            step4();
            loop();
        }
        private void InitGPIO()
        {
            var gpio = GpioController.GetDefault();

            if(gpio == null)
            {
                //redPin = null;
               // orangePin = null;
               // greenPin = null;
               // yellowPin = null;

                return;
            }

            redPin = gpio.OpenPin(red);
            orangePin = gpio.OpenPin(orange);
            bluePin = gpio.OpenPin(blue);
            yellowPin = gpio.OpenPin(yellow);

            yellowPin.Write(GpioPinValue.Low);

            redPin.Write(GpioPinValue.Low);
            bluePin.Write(GpioPinValue.Low);
            orangePin.Write(GpioPinValue.Low);



            //greenPin.Write(GpioPinValue.Low);
            //yellowPin.Write(GpioPinValue.Low);

            redPin.SetDriveMode(GpioPinDriveMode.Output);
           orangePin.SetDriveMode(GpioPinDriveMode.Output);
            bluePin.SetDriveMode(GpioPinDriveMode.Output);
            yellowPin.SetDriveMode(GpioPinDriveMode.Output);
        }
        private void MainPage_Unloaded(object sender, object args)
        {
            redPin.Dispose();
            bluePin.Dispose();
            orangePin.Dispose();
            yellowPin.Dispose();
        }
        private async void step1()
        {
            yellowPin.Write(GpioPinValue.Low);
            redPin.Write(GpioPinValue.High);
            bluePin.Write(GpioPinValue.Low);
            orangePin.Write(GpioPinValue.High);
            await Task.Delay(1000);
            //yellowPin.Write(GpioPinValue.Low);
            //redPin.Write(GpioPinValue.High);
            //bluePin.Write(GpioPinValue.High);
            //orangePin.Write(GpioPinValue.Low);
            //await Task.Delay(1000);
            //yellowPin.Write(GpioPinValue.High);
            //redPin.Write(GpioPinValue.Low);
            //bluePin.Write(GpioPinValue.High);
            //orangePin.Write(GpioPinValue.Low);
            //await Task.Delay(1000);
            //yellowPin.Write(GpioPinValue.High);
            //redPin.Write(GpioPinValue.Low);
            //bluePin.Write(GpioPinValue.Low);
            //orangePin.Write(GpioPinValue.High);
            //await Task.Delay(1000);
        }
        private async void step2()
        {
            yellowPin.Write(GpioPinValue.Low);
            redPin.Write(GpioPinValue.High);
            bluePin.Write(GpioPinValue.High);
            orangePin.Write(GpioPinValue.Low);
            await Task.Delay(1000);
        }
        private async void step3()
        {
            yellowPin.Write(GpioPinValue.High);
            redPin.Write(GpioPinValue.Low);
            bluePin.Write(GpioPinValue.High);
            orangePin.Write(GpioPinValue.Low);
            await Task.Delay(1000);
        }
        private async void step4()
        {
            yellowPin.Write(GpioPinValue.High);
            redPin.Write(GpioPinValue.Low);
            bluePin.Write(GpioPinValue.Low);
            orangePin.Write(GpioPinValue.High);
            await Task.Delay(1000);
        }
        private void stopMotor()
        {
            yellowPin.Write(GpioPinValue.Low);

            redPin.Write(GpioPinValue.Low);
            bluePin.Write(GpioPinValue.Low);
            orangePin.Write(GpioPinValue.Low);
        }

        private async void loop()
        {
            for (int i = 0; i <= 11; i++)
            {
                step1();
                step2();
                step3();
                step4();
            }
            stopMotor();
            await Task.Delay(1000);
            for (int i = 0; i <= 11; i++)
            {
                step3();
                step2();
                step1();
                step4();
            }

            stopMotor();
            await Task.Delay(1000);
            for (int i = 0; i <= 11; i++)
            {
                step4();
                step3();
                step2();
                step1();
            }

            stopMotor();
            await Task.Delay(1000);

        }
    }

}

I want to know if there is any issue with my code or is there any extra thing I need to do to get a complete revolution from the stepper motor.

Your help will be apprecciated.

LotPings
  • 366
  • 1
  • 13

0 Answers0