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.
- Digital Light Sensor → GPIO #5
- Red LED → GPIO #12
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
- Open Visual Studio 2017 or 2015.
- Choose Blank App (Universal Windows).
- Set the project name to DigitalLightSensorApp.
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
- Heartbeat Sensor: How It Works & Practical Applications
- Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
- Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
- BMP180 I2C Barometric Pressure Sensor – Raspberry Pi Integration Guide
- Reading a Linear Hall Sensor with Raspberry Pi: Java Guide & ADC0832 Insights
- Build a Multi‑Sensor Temperature & Light Monitoring System with Raspberry Pi & DS18B20
- Testing the DS18B20 Temperature Sensor on Raspberry Pi
- Raspberry Pi Light Sensor Tutorial – Wire an LDR and Read Light Levels with Python
- Capacitive Touch LED Switch with Arduino UNO – Easy DIY Project
- Measure Heart Rate with the KY-039 Arduino Sensor