Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

Digital Light Sensor – Power an LED with Ambient Light Using Windows 10 IoT Core

This Windows 10 IoT Core project demonstrates how to drive an LED in real‑time based on the reading from a digital light sensor. By simply monitoring the GPIO pin attached to the sensor, the application toggles the LED whenever light is detected or absent.

Project Overview

The goal is straightforward: when ambient light is present, the LED turns on; when the light goes out, the LED turns off. The entire logic is handled in a single C# file with no UI, making the code lightweight and ideal for quick prototyping.

Demo of the project result (image placeholder)

Setting Up the Board

Below is the wiring diagram. Feel free to adjust the pin numbers as long as you update the code accordingly.

The Part I Like: Coding!

This application contains only one page that houses the entire logic, keeping the project lean.

Step 1: Create the UWP Project

Step 2: Implement the Logic

Open MainPage.xaml.cs and replace the existing using directives with the following:

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

Declare the sensor and LED GPIO pin members:

private GpioPin _sensorPin;
private GpioPin _ledPin;

Define the GPIO pin numbers:

private int _sensorGpioPinNumber = 5;
private int _ledGpioPinNumber = 12;

Create an initialization routine that opens the pins and sets their modes:

private void Initialize()
{
    var gpioController = GpioController.GetDefault();
    // Sensor – input
    _sensorPin = gpioController.OpenPin(_sensorGpioPinNumber);
    _sensorPin.SetDriveMode(GpioPinDriveMode.Input);
    _sensorPin.ValueChanged += HandleSensorLightDetectionChange;
    // LED – output
    _ledPin = gpioController.OpenPin(_ledGpioPinNumber);
    _ledPin.SetDriveMode(GpioPinDriveMode.Output);
    // Initial state
    HandleLightStatus();
}

Respond to sensor changes:

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

Toggle the LED based on the sensor reading. A high value indicates darkness, while low means light is present:

private void HandleLightStatus()
{
    if (_sensorPin.Read() == GpioPinValue.High)
    {
        _ledPin.Write(GpioPinValue.Low); // Turn LED off
    }
    else
    {
        _ledPin.Write(GpioPinValue.High); // Turn LED on
    }
}

Place the Initialize call in the page constructor after InitializeComponent():

public MainPage()
{
    this.InitializeComponent();
    Initialize();
}

Full Source Code

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();
            Initialize();
        }

        private void Initialize()
        {
            var gpioController = GpioController.GetDefault();
            _sensorPin = gpioController.OpenPin(_sensorGpioPinNumber);
            _sensorPin.SetDriveMode(GpioPinDriveMode.Input);
            _sensorPin.ValueChanged += HandleSensorLightDetectionChange;

            _ledPin = gpioController.OpenPin(_ledGpioPinNumber);
            _ledPin.SetDriveMode(GpioPinDriveMode.Output);
            HandleLightStatus();
        }

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

        private void HandleLightStatus()
        {
            if (_sensorPin.Read() == GpioPinValue.High)
            {
                _ledPin.Write(GpioPinValue.Low);
            }
            else
            {
                _ledPin.Write(GpioPinValue.High);
            }
        }
    }
}

The code above runs continuously, reacting instantly to changes in ambient light.

For additional details, refer to the Digital Light Sensor documentation.

Manufacturing process

  1. Heartbeat Sensor: How It Works & Practical Applications
  2. Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
  3. Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
  4. BMP180 I2C Barometric Pressure Sensor – Raspberry Pi Integration Guide
  5. Reading a Linear Hall Sensor with Raspberry Pi: Java Guide & ADC0832 Insights
  6. Build a Multi‑Sensor Temperature & Light Monitoring System with Raspberry Pi & DS18B20
  7. Testing the DS18B20 Temperature Sensor on Raspberry Pi
  8. Raspberry Pi Light Sensor Tutorial – Wire an LDR and Read Light Levels with Python
  9. Capacitive Touch LED Switch with Arduino UNO – Easy DIY Project
  10. Measure Heart Rate with the KY-039 Arduino Sensor