Ahmed Mohamed Hazzah
Published © GPL3+

Digital Light Sensor

A Windows 10 IoT core application that switches on an LED based on digital light sensor reading.

BeginnerFull instructions provided1 hour889
Digital Light Sensor

Things used in this project

Hardware components

Raspberry Pi 3 Model B+
Raspberry Pi 3 Model B+
×1
Digital Light Sensor
×1
Breadboard (generic)
Breadboard (generic)
×1
3 mm LED: Red
3 mm LED: Red
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Male/Male Jumper Wires
×1

Software apps and online services

Windows 10 IoT Core
Microsoft Windows 10 IoT Core
Microsoft Visual Studio 2017 - Community

Story

Read more

Schematics

Digital Light Sensor wiring diagram

Code

Digital Light Sensor - MainPage.xaml.cs

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

namespace DigitalLightSensorApp
{
    public sealed partial class MainPage : Page
    {
        private GpioPin _sensorPin;
        private GpioPin _ledPin;

        private int _sensorGpioPinNumber = 5;
        private int _ledGpioPinNumber = 12;
        
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Initialize()
        {
            // Get the default Gpio Controller
            var gpioController = GpioController.GetDefault();

            //Open the sensor Gpio Pin and set the mode to input
            _sensorPin = gpioController.OpenPin(_sensorGpioPinNumber);
            _sensorPin.SetDriveMode(GpioPinDriveMode.Input);
            _sensorPin.ValueChanged += HandleSensorLightDetectionChange; ;

            // Open the LED Gpio Pin and set the mode to output
            _ledPin = gpioController.OpenPin(_ledGpioPinNumber);
            _ledPin.SetDriveMode(GpioPinDriveMode.Output);
            
            handleLightStatus();
        }

        private void HandleSensorLightDetectionChange(GpioPin sender, GpioPinValueChangedEventArgs args)
        {
            handleLightStatus();
        }

        private void handleLightStatus()
        {
            // if no light sensor detected light, then turn off the LED
            if (_sensorPin.Read() == GpioPinValue.High)
            {
                //Turn off the LED
                _ledPin.Write(GpioPinValue.Low);
            }
            else
            {
                //Turn on the LED
                _ledPin.Write(GpioPinValue.High);
            }
        }
    }
}

Credits

Ahmed Mohamed Hazzah

Ahmed Mohamed Hazzah

2 projects • 1 follower
Technology enthusiast specialized in Microsoft Development Technologies. Graduated from Computer Science - October 6 University - Egypt

Comments