Using the LightSensor Class in Your Windows Store Apps


Modern mobile devices aren’t used just for calling people. They pack the same computing power as a last-gen computer. In addition, they have a lot of sensors to help with the various features of a “smartphone.” One of the available sensors is the ambient light sensor, which can detect ambient light. This sensor can be used to determine the LUX value of light.

Light Sensor Basics

Windows Phone has platform support for sensing ambient light in the form of the LightSensor that is housed in the Windows.Devices.Sensors namespace.

  • The LightSensor class represents an ambient-light sensor that returns the light reading as a LUX value.
  • The LightSensor class exposes the ReadingChanged event that is fired every time a new sensor reading is reported.
  • The GetCurrentReading method on the LightSensor class is used to get the current LUX value.

We can learn about the minimum report internal by using the MinimumReportInterval property on the LightSensor class. We can set/get the report internal by using the ReportInterval property. A handle to the LightSensor object can be obtained by calling the LightSensor.GetDefault() method. If there is no light sensor on the device, the method will return null.

Hands On

In our hands on, we will build a simple application in which we will get the ambient light reading displayed in a textbox control.

Create a new Visual Studio 2013 Windows Phone Blank App project called WPLightSensorDemo.


Figure 1: The new blank project

Add a few controls as shown in the XAML layout depicted in Figure 2.

Figure 2: Placing the controls


      <TextBox x_Name="textBoxReading"
         HorizontalAlignment="Left" Margin="191,190,0,0"
         TextWrapping="Wrap" VerticalAlignment="Top"
      <TextBlock x_Name="textBlockReading"
         HorizontalAlignment="Left" Margin="134,202,0,0"
         TextWrapping="Wrap" Text="Reading"
      <TextBox x_Name="textBoxReportingInterval"
         HorizontalAlignment="Left" Margin="191,58,0,0"
         TextWrapping="Wrap" VerticalAlignment="Top"
      <TextBlock x_Name="textBlockReportingInterval"
         HorizontalAlignment="Left" Margin="99,72,0,0"
         TextWrapping="Wrap" Text="Reporting Interval"
      <TextBox x_Name="textBoxReadingTime"
         Margin="191,233,0,0" TextWrapping="Wrap"
         VerticalAlignment="Top" Width="152"/>
      <TextBlock HorizontalAlignment="Left"
         TextWrapping="Wrap" Text="Reading Timestamp"

Next, we make changes in the code-behind for MainPage.xaml. First, we include a couple of namespaces in the source file.

using Windows.Devices.Sensors;
using Windows.UI.Core;

Next, we create a couple of class variables: one for the LightSensor and another to track whether we detected a light sensor.

public sealed partial class MainPage : Page
      LightSensor ls;
      bool sensorFound = false;
      public MainPage()

Next, we update the constructor to instantiate the lightsensor variable and wire up its ReadingChanged event.

public MainPage()

      this.NavigationCacheMode = NavigationCacheMode.Required;
      ls = LightSensor.GetDefault();
      if (ls != null)
         sensorFound = true;
         ls.ReadingChanged += ls_ReadingChanged;
         textBoxReportingInterval.Text =
         ls.ReportInterval = ls.MinimumReportInterval;

Finally, we implement the event handler for the ReadingChanged event.

async void ls_ReadingChanged(LightSensor sender,
   LightSensorReadingChangedEventArgs args)
      await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
         () =>
         textBoxReading.Text =
         textBoxReadingTime.Text =


Our application is now ready. When we run the application the first time, we will set the recording interval to the lowest possible and update the textbox to reflect the ambient light reading as well as the timestamp of the reading. If you are having trouble following along, you can download the sample code from the bottom of this article.


In this article, we learned about using a light sensor in Windows Phone applications.

About the Author

Vipul Patel is a technology geek based in Seattle. He can be reached at [email protected] You can visit his LinkedIn profile at


More by Author

Must Read