Berndt Hamboeck
Published © GPL3+

Windows 10 IoT Fan Control - no need to sweat!

Raspberry Pi controls two fans using the L293D as the motor driver

BeginnerFull instructions provided7,889
Windows 10 IoT Fan Control - no need to sweat!

Things used in this project

Hardware components

L293D
×1
Mini ventilator
×2
Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Windows 10 IoT Core
Microsoft Windows 10 IoT Core

Story

Read more

Schematics

Fritzing File

Wire Instructions

Code

MainPage.xaml.cs

C#
Init GPIOs, cleanup GPIOs, turn them on and off on button click
using Windows.Devices.Gpio;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


namespace FanControl
{
    public sealed partial class MainPage : Page
    {
        private const int RedE_PIN = 12; //fan on or off
        private const int Red1_PIN = 6; //positive
        private const int Red2_PIN = 13; //negative
        private const int YellowE_PIN = 18;
        private const int Yellow1_PIN = 27;
        private const int Yellow2_PIN = 22;

        private GpioPin _pinRedE;
        private GpioPin _pinRed1;
        private GpioPin _pinRed2;
        private GpioPin _pinYellowE;
        private GpioPin _pinYellow1;
        private GpioPin _pinYellow2;


        public MainPage()
        {
            this.InitializeComponent();

            InitGPIO();
        }

        private void InitGPIO()
        {
            var gpio = GpioController.GetDefault();

            _pinRedE = gpio.OpenPin(RedE_PIN);
            _pinRedE.Write(GpioPinValue.Low);
            _pinRed1 = gpio.OpenPin(Red1_PIN);
            _pinRed1.Write(GpioPinValue.Low);
            _pinRed2 = gpio.OpenPin(Red2_PIN);
            _pinRed2.Write(GpioPinValue.Low);

            _pinYellowE = gpio.OpenPin(YellowE_PIN);
            _pinYellowE.Write(GpioPinValue.Low);
            _pinYellow1 = gpio.OpenPin(Yellow1_PIN);
            _pinYellow1.Write(GpioPinValue.Low);
            _pinYellow2 = gpio.OpenPin(Yellow2_PIN);
            _pinYellow2.Write(GpioPinValue.Low);

            _pinRedE.SetDriveMode(GpioPinDriveMode.Output);
            _pinRed1.SetDriveMode(GpioPinDriveMode.Output);
            _pinRed2.SetDriveMode(GpioPinDriveMode.Output);

            _pinYellowE.SetDriveMode(GpioPinDriveMode.Output);
            _pinYellow1.SetDriveMode(GpioPinDriveMode.Output);
            _pinYellow2.SetDriveMode(GpioPinDriveMode.Output);

        }

        private void MainPage_Unloaded(object sender, object args)
        {
            _pinRedE.Write(GpioPinValue.Low);
            _pinYellowE.Write(GpioPinValue.Low);

            // Cleanup
            _pinRedE.Dispose();
            _pinRed1.Dispose();
            _pinRed2.Dispose();

            _pinYellowE.Dispose();
            _pinYellow1.Dispose();
            _pinYellow2.Dispose();
        }

        private void RedLeftButton_Click(object sender, RoutedEventArgs e)
        {
            //Set first motor's GPIO for forward
            _pinRed1.Write(GpioPinValue.Low);
            _pinRed2.Write(GpioPinValue.High);
            _pinRedE.Write(GpioPinValue.High);

        }

        private void RedRightButton_Click(object sender, RoutedEventArgs e)
        {
            //Set first motor's GPIO for backward
            _pinRed1.Write(GpioPinValue.High);
            _pinRed2.Write(GpioPinValue.Low);
            _pinRedE.Write(GpioPinValue.High);
        }

        private void YellowButtonLeft_Click(object sender, RoutedEventArgs e)
        {
            //#et second motor's GPIO for forward
            _pinYellow1.Write(GpioPinValue.Low);
            _pinYellow2.Write(GpioPinValue.High);
            _pinYellowE.Write(GpioPinValue.High);
        }

        private void YellowButtonRight_Click(object sender, RoutedEventArgs e)
        {
            //Set Yellow motor's GPIO
            _pinYellow1.Write(GpioPinValue.High);
            _pinYellow2.Write(GpioPinValue.Low);
            _pinYellowE.Write(GpioPinValue.High);
        }

        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            //Set both motors enable pin to low
            _pinRedE.Write(GpioPinValue.Low);
            _pinYellowE.Write(GpioPinValue.Low);

        }
    }
}

MainPage.xaml

XML
GUI
<Page
    x:Class="FanControl.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:FanControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Width="800" Height="480">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Image Height="200" Source="Images\FanRed.png" Grid.Row="0" Grid.Column="0" Stretch="Uniform"  />
        <Image Height="200" Source="Images\FanYellow.png" Grid.Row="1" Grid.Column="0" Stretch="Uniform" />

            <Button Name="RedLeftButton" Grid.Row="0" Grid.Column="1"
                        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                        Click="RedLeftButton_Click"
                        Background="Red">On</Button>
        <StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Stretch">
            <!--<Button Name="RedRightButton" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="RedRightButton_Click"
                    Background="Red">Right</Button>-->
        </StackPanel>

            <Button Name="TopButton" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                        Margin="0 5"
                        Click="YellowButtonLeft_Click" 
                        Background="Yellow">On</Button>
        <!--<StackPanel Grid.Row="1" Grid.Column="1" VerticalAlignment="Center">
            <Button Name="BottomButton" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  
                        Margin="0 5"
                        Click="YellowButtonRight_Click"
                        Background="Yellow">Down</Button>
        </StackPanel>-->

        <Button Name="StopButton" Grid.Row="2" Grid.Column="1" Height="80" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="StopButton_Click"
                    Background="Green">Stop</Button>
        
        <TextBlock Grid.Row="2" VerticalAlignment="Center" 
                   HorizontalAlignment="Center"
                   FontStyle="Italic">Windows 10 IoT powered fan control</TextBlock>

    </Grid>

</Page>

Credits

Berndt Hamboeck

Berndt Hamboeck

5 projects • 16 followers
MCT, C# Developer, Nerd

Comments