Ahmed Mohamed Hazzah
Published

LED Light Selector

A Windows 10 IoT core application that lights up LED colors based on user selection through UI.

BeginnerFull instructions provided1 hour907
LED Light Selector

Things used in this project

Hardware components

Raspberry Pi 3 Model B+
Raspberry Pi 3 Model B+
×1
Raspberry Pi GPIO Breakout Board
Optional
×1
Breadboard (generic)
Breadboard (generic)
×1
3 mm LED: Yellow
3 mm LED: Yellow
×1
3 mm LED: Red
3 mm LED: Red
×1
3 mm LED: Green
3 mm LED: Green
×1
3 mm LED : Orange
×1
Resistor 330 ohm
Resistor 330 ohm
×4

Software apps and online services

Windows 10 IoT Core
Microsoft Windows 10 IoT Core
Microsoft Visual Studio 2017
Community Edition Powerful IDE, free for students, open-source contributors, and individuals

Story

Read more

Schematics

LED light selector wiring diagram

Code

LED light selector source code - Constants Class

C#
namespace LightSelectorApp.Code
{
    public class Constants
    {
       /// <summary>
       /// Color code constants which is used in UI buttons
       /// </summary>
        public struct ColorConstants
        {
            public const string Red = "RED";
            public const string Orange = "ORANGE";
            public const string Green = "GREEN";
            public const string Yellow = "YELLOW";
            // Switch on all LEDs
            public const string AllOn = "ALLON";
            //Switch off all LEDs
            public const string AllOff = "ALLOFF";
        }

        /// <summary>
        /// GPIO pin number of each color
        /// change them based on your needs
        /// </summary>
        public struct ColorPinNumberConstants
        {
            public const int Red = 12;
            public const int Orange = 6;
            public const int Green = 22;
            public const int Yellow = 25;
        }

        /// <summary>
        /// GPIO pin value, High for on and Low of Off
        /// </summary>
        public struct LedLightStatusConstants
        {
            public const Windows.Devices.Gpio.GpioPinValue On =
                Windows.Devices.Gpio.GpioPinValue.High;
            public const Windows.Devices.Gpio.GpioPinValue Off =
                Windows.Devices.Gpio.GpioPinValue.Low;
        }
    }
}

LED light selector source code - GPIOController Class

C#
using System;
using Windows.Devices.Gpio;
using Windows.UI.Xaml;
namespace LightSelectorApp.Code
{
    public class GPIOController
    {
        // The general-purpose I/O (GPIO) pin for each color
        private GpioPin _redGpioPin, _greenGpioPin, _orangeGpioPin, _yellowGpioPin;
        // The default general-purpose I/O (GPIO) controller for the system.
        private GpioController _gpioController;
        // Provides a timer that is integrated into the Dispatcher queue
        // Used to blink the LED's if requested.
        private DispatcherTimer _blinkingTimer;
        // Requested Color from UI
        private string _requestedColor;
        // Led Current light Status
        private bool _ledStatus;

        public GPIOController()
        {
            // Initialize GPIO controller
            InitGPIO();
            // Initialize dispatcher timer 
            _blinkingTimer = new DispatcherTimer();
            _blinkingTimer.Interval = TimeSpan.FromMilliseconds(200);
            _blinkingTimer.Tick += _blinkingTimer_Tick;
        }

        private void InitGPIO()
        {
            // Get the default GPIO controller
            _gpioController = GpioController.GetDefault();
            if (_gpioController == null)
            {
                throw new Exception("No Gpio Controller Found.");
            }
            else
            {
                #region Assign each color pin and set the drive mode to output
                _redGpioPin = _gpioController.OpenPin(Constants.ColorPinNumberConstants.Red);
                _redGpioPin.SetDriveMode(GpioPinDriveMode.Output);
                _greenGpioPin = _gpioController.OpenPin(Constants.ColorPinNumberConstants.Green);
                _greenGpioPin.SetDriveMode(GpioPinDriveMode.Output);
                _orangeGpioPin = _gpioController.OpenPin(Constants.ColorPinNumberConstants.Orange);
                _orangeGpioPin.SetDriveMode(GpioPinDriveMode.Output);
                _yellowGpioPin = _gpioController.OpenPin(Constants.ColorPinNumberConstants.Yellow);
                _yellowGpioPin.SetDriveMode(GpioPinDriveMode.Output);
                #endregion
            }
        }

        /// <summary>
        /// Invoked by UI to switch on / of requested color LED
        /// </summary>
        /// <param name="requestedColor">Requested color to be switched on</param>
        /// <param name="isBlinking">LED blinks if true </param>
        public void HandleLightRequest(string requestedColor, bool isBlinking)
        {
            // Switch of all LEDs 
            SwitchOffAllLights();
            // Start or Stop dispatch timer based on isBlinking value
            if (isBlinking)
                _blinkingTimer.Start();
            else
                _blinkingTimer.Stop();

            _requestedColor = requestedColor;

            // Switch on requested color
            SwitchOnLight();
        }

        // Switch on the requested color
        private void SwitchOnLight()
        {
            switch (_requestedColor)
            {
                case Code.Constants.ColorConstants.Orange:
                    _orangeGpioPin.Write(Constants.LedLightStatusConstants.On);
                    break;
                case Code.Constants.ColorConstants.Yellow:
                    _yellowGpioPin.Write(Constants.LedLightStatusConstants.On);
                    break;
                case Code.Constants.ColorConstants.Red:
                    _redGpioPin.Write(Constants.LedLightStatusConstants.On);
                    break;
                case Code.Constants.ColorConstants.Green:
                    _greenGpioPin.Write(Constants.LedLightStatusConstants.On);
                    break;
                case Code.Constants.ColorConstants.AllOn:
                    SwitchOnAllLights();
                    break;
            }
        }

        // Switch off all color LED by setting the GpioPinValue to low
        private void SwitchOffAllLights()
        {
            #region Switch off All Colors
            _redGpioPin.Write(Constants.LedLightStatusConstants.Off);
            _yellowGpioPin.Write(Constants.LedLightStatusConstants.Off);
            _orangeGpioPin.Write(Constants.LedLightStatusConstants.Off);
            _greenGpioPin.Write(Constants.LedLightStatusConstants.Off);
            #endregion
        }

        // Switch on all color LED by setting the GpioPinValue to high
        private void SwitchOnAllLights()
        {
            #region Switch on All Colors
            _redGpioPin.Write(Constants.LedLightStatusConstants.On);
            _yellowGpioPin.Write(Constants.LedLightStatusConstants.On);
            _orangeGpioPin.Write(Constants.LedLightStatusConstants.On);
            _greenGpioPin.Write(Constants.LedLightStatusConstants.On);
            #endregion
        }

        // Handle blinking dispatcher timer tick event
        private void _blinkingTimer_Tick(object sender, object e)
        {
            // if _ledStatus is true then all lights will switch on, else they switch off
            if (_ledStatus)
                SwitchOnLight();
            else
                SwitchOffAllLights();

            // invert _ledStatus
            _ledStatus = !_ledStatus;
        }
    }
}

LED light selector source code - MainPage.xaml

C#
<Page
    x:Class="LightSelectorApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LightSelectorApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <StackPanel Orientation="Vertical"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center">
            <StackPanel Orientation="Horizontal">
                <CheckBox x:Name="chkBlink" 
                          Content="Blink" 
                          Checked="handleBlinkChange"
                          Unchecked="handleBlinkChange"
                          HorizontalAlignment="Left" 
                          VerticalAlignment="Center" 
                          Margin="0,0,0,0" 
                          Width="68"/>
             </StackPanel>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="10"/>
                    <RowDefinition/>
                    <RowDefinition Height="10"/>
                    <RowDefinition/>
                    <RowDefinition Height="10"/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <StackPanel Orientation="Horizontal">
                    <Button Content="Red" CommandParameter="RED" Width="100" Click="handleColorRequest"/>
                    <Button Content="Yellow" CommandParameter="YELLOW" Width="100" Margin="10,0,0,0" Click="handleColorRequest"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal"
                            Grid.Row="2">
                    <Button Content="Green" CommandParameter="GREEN" Width="100" Margin="0,0,0,0" Click="handleColorRequest"/>
                    <Button Content="Orange" CommandParameter="ORANGE" Width="100" Margin="10,0,0,0" Click="handleColorRequest"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal"
                            Grid.Row="4">
                    <Button Content="All On" CommandParameter="ALLON" Width="100" Margin="0,0,0,0" Click="handleColorRequest"/>
                    <Button Content="All Off" CommandParameter="ALLOFF" Width="100" Margin="10,0,0,0" Click="handleColorRequest"/>
                </StackPanel>
                <TextBlock Grid.Row="6" 
                           HorizontalAlignment="Center" 
                           VerticalAlignment="Center"
                           x:Name="lblColor"/>
            </Grid>
        </StackPanel >

    </Grid>
</Page>

LED light selector source code - MainPage.xaml.cs

C#
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace LightSelectorApp
{
    public sealed partial class MainPage : Page
    {
        // GPIOController class refrence
        Code.GPIOController _gpioController;
        private string _requestedColor;

        public MainPage()
        {
            this.InitializeComponent();

            // Instantiate the GPIOController object
            _gpioController = new Code.GPIOController();

            //Start with all LEDs switched off
            _requestedColor = Code.Constants.ColorConstants.AllOff;

            InvokeLEDLight();
        }

        private void handleColorRequest(object sender, RoutedEventArgs e)
        {
            // Get requested color code
            _requestedColor =
                (sender as Button).CommandParameter.ToString();

            InvokeLEDLight();
        }

        private void handleBlinkChange(object sender, RoutedEventArgs e)
        {
            InvokeLEDLight();
        }

        private void InvokeLEDLight()
        {
            if (_requestedColor == Code.Constants.ColorConstants.AllOn)
                lblColor.Text = "You switched on all colors.";
            else if (_requestedColor == Code.Constants.ColorConstants.AllOff)
            {
                chkBlink.IsChecked = false;
                lblColor.Text = "You switched off all colors.";
            }
            else
                lblColor.Text =
                    string.Format("You swtiched on {0} color.",
                    _requestedColor.ToString().ToLower());

            _gpioController.HandleLightRequest(_requestedColor,
                                               chkBlink.IsChecked.Value);
        }
    }
}

Credits

Ahmed Mohamed Hazzah
2 projects • 2 followers
Technology enthusiast specialized in Microsoft Development Technologies. Graduated from Computer Science - October 6 University - Egypt

Comments