Carey Payette
Published © CC BY

Windows IoT : Bi-Directional DC Motor Control

Bi-Directional DC Motor Control using Windows IoT Core, a DC motor and the L293D IC to automate physical movement in an IoT system.

BeginnerProtip2 hours3,643
Windows IoT : Bi-Directional DC Motor Control

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Arduino L293D
×1
Adafruit DC Motor
×1
Adafruit Pi Cobbler
×1
Adafruit 4xAA Battery Pack
×1

Software apps and online services

Windows 10 IoT Core
Microsoft Windows 10 IoT Core

Story

Read more

Schematics

Wiring Setup

Wiring Setup

Code

UI Definition

Plain text
<Page
    x:Class="PiDCMotorControl.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PiDCMotorControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="50" />
            <RowDefinition Height="50" />
            <RowDefinition Height="50" />
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="200" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock FontSize="30" Grid.Row="1" Grid.ColumnSpan="3">Motor Control</TextBlock>
        <Button Name="btnIgnitionOn" Margin="5" Background="Green" Foreground="White"  Grid.Row="2" 
                Click="btnIgnitionOn_Click">Ignition ON</Button>
        <Button Name="btnIgnitionOff" Margin="5" Background="Red" Foreground="White" IsEnabled="False" 
                Grid.Row="2" Grid.Column="1" Click="btnIgnitionOff_Click">Ignition Off</Button>
        <Button Name="btnForward" Margin="5" Background="LightBlue" Foreground="DarkBlue" IsEnabled="False" 
                Grid.Row="3" Click="btnForward_Click">Forward</Button>
        <Button Name="btnReverse" Margin="5" Background="Orange" Foreground="OrangeRed" IsEnabled="False" 
                Grid.Row="3" Grid.Column="1" Click="btnReverse_Click">Reverse</Button>
        <Button Name="btnStop" Click="btnStop_Click" Background="Salmon" Foreground="Sienna" IsEnabled="True"
                Grid.Row="4" Margin="5">Stop</Button>
     </Grid>
</Page>

DC Motor Control Implementation

Plain text
using Windows.Devices.Gpio;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
 
namespace PiDCMotorControl
{
    /// <summary>
    /// Basic Bi-Directional Control of a single DC Motor
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private int _pinEn1_2 = 21; 
        private int _pin1A = 20; 
        private int _pin2A = 16; 
 
        private GpioController _controller;
        private GpioPin _motorEnable;
        private GpioPin _motorControl1A;
        private GpioPin _motorControl2A;
      
        public MainPage()
        {
            this.InitializeComponent();
 
            _controller = GpioController.GetDefault();
            _motorEnable = _controller.OpenPin(_pinEn1_2);
            _motorControl1A = _controller.OpenPin(_pin1A);
            _motorControl2A = _controller.OpenPin(_pin2A);
            _motorEnable.SetDriveMode(GpioPinDriveMode.Output);
            _motorControl1A.SetDriveMode(GpioPinDriveMode.Output);
            _motorControl2A.SetDriveMode(GpioPinDriveMode.Output);     
        }
 
        private void _turnOnIgnition()
        {
            _motorEnable.Write(GpioPinValue.High);
        }
 
        private void _forwardMotor()
        {
            _motorControl1A.Write(GpioPinValue.High);
            _motorControl2A.Write(GpioPinValue.Low);
        }
 
        private void _reverseMotor()
        {
            _motorControl1A.Write(GpioPinValue.Low);
            _motorControl2A.Write(GpioPinValue.High);
        }
 
        private void _stopMotor()
        {
            _motorControl1A.Write(GpioPinValue.Low);
            _motorControl2A.Write(GpioPinValue.Low);
        }
 
        private void _turnOffIgnition()
        {
            _motorEnable.Write(GpioPinValue.Low);
            _motorControl1A.Write(GpioPinValue.Low);
            _motorControl2A.Write(GpioPinValue.Low);
        }
 
        private void btnIgnitionOn_Click(object sender, RoutedEventArgs e)
        {
            btnIgnitionOn.IsEnabled = false;
            btnIgnitionOff.IsEnabled = true;
            btnForward.IsEnabled = true;
            btnReverse.IsEnabled = true;
            _turnOnIgnition();
        }
 
        private void btnIgnitionOff_Click(object sender, RoutedEventArgs e)
        {
            btnIgnitionOn.IsEnabled = true;
            btnIgnitionOff.IsEnabled = false;
            btnForward.IsEnabled = false;
            btnReverse.IsEnabled = false;
            _turnOffIgnition();
        }
 
        private void btnForward_Click(object sender, RoutedEventArgs e)
        {
            btnForward.IsEnabled = false;
            btnReverse.IsEnabled = true;
            _forwardMotor();
        }
 
        private void btnReverse_Click(object sender, RoutedEventArgs e)
        {
            btnReverse.IsEnabled = false;
            btnForward.IsEnabled = true;
            _reverseMotor();
        }
 
        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            _stopMotor();
        }
    }
}

Credits

Carey Payette

Carey Payette

13 projects • 134 followers
Sr. Software Engineer at Independent

Comments