Geeks With Blogs
.Net Loftings Easy .NET for pre-Beginners


Its been a great ride for all of us who Love Technology and play with what it can do for us with the devices that we have. In search to explore new stuff that can be done with the wide range of sensors that are available for us these days I have come across this wonderful post done by Sacha Barber about Light sensor on Windows 8. “Fun With Windows 8 Light Sensor” is where you can look how the light sensor can be used to control the Google Earth.

I either can follow him and implement the same but I decided to start learn the basics first and then jump onto more complex implementations, and I decided to write blog posts along my journey so you have a smooth understanding of what we are going to do with sensors.

This post is about Light Sensor (100 Level) – Here we are just going to initialize the light sensor and read the Luminance value (which is measured in LUX). You cal always wiki the LUX and learn more about it .

Let me start now and show what I did to get the following final result.


The number  “122” is the Luminance value that the light sensor on my Surface Pro read from the light ambience in my room. Here you go, very simple to follow.

1. Open Visual Studio 2012 (I am on Surface Pro, any computer with light sensor that can run VS is a minimum requirement)

2. File >> New >> Project (Select/Enter the stuff that are marked in Red)


Since this is a very small application I selected the Blank App Template. Now Visual Studio Creates a Blank Windows 8 Application.

When you run it you will see a black blank windows, since we don't have anything on the app yet.

3. Lets put some stuff on the page to show up on the screen so that we don't see blank page. Since I am like many of you guys who are not great designers or Lazy to write the UI code, Microsoft have Blend for us. I open Blend and opened my project and below is who it looked.


The Red box to the left is the tree structure of the application. The Blank application template only gives a blank grid and I added a stackpanel under it to stack my text. Below is how it  looked after i had my UI design done.


Its very clear that i have a stackpanel with 4 textblocks (labels), Title, Author area, the Box to show the Luminance value, and a help text to show how to activate it.

4. Now go back to VS 2012 and you shall see something like this


here click “Yes to All” to save the changes that are made on Blend, now you will see the UI changes on VS 2012 as well. Now if you run the App it shall look like below rather than a blank black window like it was earlier.


5. Now lets get back to the code and add few lines of code and shall see the light sensor do its magic. So far when you run the app what we see on the page is contenst from the MainPage.xaml

Now lets click on the little arrow next to it to add code behind (open MainPage.xaml.cs)

You will see a constructor which Initializes the component and right above that Initialize a private variable

private LightSensor _sensor; (you might need to add a reference to Windows.Devices.Sensors)

and add the following code right below InitializeComponent() call in the constructor

private LightSensor _sensor;

_sensor = LightSensor.GetDefault();
if (_sensor != null)
    LuxValue.Text = "";
    LuxValue.Text = "No Light Sensor Found";

what basically we are doing it getting a new local instance of LightSensor before the constructor is called and checking the default value of the sensor after the MainPage object has been initialized, if that default value is null then it means that there is no sensor and we show the “No Light Sensor” text if not we just don't do anything.

6. in the same MainPage.xaml.cs file you will also get an override method called OnNavigatedTo , this is the method that is called when the app is completely loaded (to put in simple terms). and this is where we have to call the function that reads the Luminance value from the light sensor and displays in the box that is shown in step 3 second image.

and this is how it looks

protected override void OnNavigatedTo(NavigationEventArgs e)
    _sensor.ReadingChanged += new TypedEventHandler<LightSensor, LightSensorReadingChangedEventArgs>(ReadingChanged);

I am calling an async method on the OnNavigatedTo so that the Luminance ReadingChanged event is called continuously and the number is shown every time the value changes. below is that the ReadingChanged method looks like

async private void ReadingChanged(object sender, LightSensorReadingChangedEventArgs e)
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        LightSensorReading reading = e.Reading;
        LuxValue.Text = String.Format("{0}", reading.IlluminanceInLux);

and thats it, everything is in place and lets run the app and we shall see the same windows as step4 second image, but if you read the help text at the bottom asking you to wave before the sensor so that the ReadingChanged event is activated and the Luminance number is shown.

Sample out put window


The “123” is the Luminance value from the light sensor on my surface pro in my room. I used my windows phone flash app pointed directly to the light sensor and the number increased to around 12000 (simulating sun)

And there we go, out first very simple app that reads the data from the sensor and the complete code can be downloaded from my public skydrive at LightSensor1 CodeDownload

Further updates are going to come soon, please stay tuned and let me know your comments.

Thank you

Vijaya Malla.

Posted on Wednesday, February 27, 2013 8:38 PM How To , Surface | Back to top

Comments on this post: Playing with Windows 8 Sensors – Light Sensor

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Vijaya Malla | Powered by: