Click here to Skip to main content
11,929,481 members (50,405 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked

Accelerometer Sensor in Windows Phone 8

, 20 Feb 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Implementation of Accelerometer in WP8 applications.


From this article, you will get a basic idea of developing the sensor based applications using accelerometer in Windows Phone 8.


Windows Phone 8 supports multiple sensors to determine the motion and orientations of the device like Accelerometer, Compass Sensor, Gyroscope and combined motions. With the use of these sensors in your applications, the physical device itself acts as user input.

Accelerometer allows applications to capture the acceleration along the X, Y, and Z axes in real time. So an accelerometer can report the direction and magnitude of the force being applied to it. The acceleration value is expressed as a 3-dimensional vector representing the acceleration components in the X - Horizontal, Y - Vertical, and Z - Perpendicular to the base. Every dimension value can range from '-2' to '2' in gravitational units.

Using the code

Let's see the implementation of this 

  1. Open VS2012 and select Windows Phone Category then create a new Windows Phone project .
  2. To make use of sensors, add Microsoft.Devices.Sensors and Microsoft.Xna.Framework references to your project. Also make sure that ID_CAP_SENSORS capability is checked in capabilities of App manifest file which is under Properties folder of your project. 
  3. Now this is my User Interface, where I have two buttons to start and stop the capturing magnitude and i have a Pivot control. So on tilting the Phone towards Right side with certain magnitude, the index of pivot element should be changed and should navigate to the next item. Below is the XAML:  
  4. //XAML   
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock Text="Simple Sensor Application" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
    <StackPanel Orientation="Horizontal">
    <Button Content="Start Sensor" Click="Button_Click_1" 
      Height="80" Width="200" HorizontalAlignment="Left"/>
    <Button Content="Stop Sensor" Click="Button_Click_2" 
      Height="80" Width="200" HorizontalAlignment="Left"/>
    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
    <phone:Pivot Name="pivot">
    <phone:PivotItem Header="Item1"> <TextBlock>AAAA</TextBlock> </phone:PivotItem>
    <phone:PivotItem Header="Item2"> <TextBlock>BBBB</TextBlock> </phone:PivotItem>
    <phone:PivotItem Header="Item3"> <TextBlock>CCCC</TextBlock> </phone:PivotItem>
    <phone:PivotItem Header="Item4"> <TextBlock>DDDD</TextBlock> </phone:PivotItem>
    <phone:PivotItem Header="Item5"> <TextBlock>EEEE</TextBlock> </phone:PivotItem>
    <phone:PivotItem Header="Item6"> <TextBlock>FFFF</TextBlock> </phone:PivotItem>
  5. So good to go with UI, now we want to start capturing the magnitude of force being applied on click of Start button.  Accelerometer is the API which gives the value of x, y, z axis as SensorReading in CurrentValueChanged callback. In this example accelerometer gives us the update for every  5 seconds which is assigned to TimeBetweenUpdates property.
  6. On Start Button Click

    // Instantiate the Accelerometer.
    accelerometer = new Accelerometer();
    accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(500);
    accelerometer.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged);

    CurrentValueChanged Callback Handler

    void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        Vector3 acceleration = e.SensorReading.Acceleration;
        // If user tilts the phone with 0.35 in X axis, Handle/Change the pivot index
        if (acceleration.X <= -0.35)
            // Application Logic
           // Change the pivot control index by -1
        if (acceleration.X >= 0.35)
            // Application Logic
            // change the pivot control index by +1
  7. Click on Stop button to stop the updates from accelerometer. Call the Stop method of the Accelerometer.

You can download the attached source code.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Michael Haephrati מיכאל האפרתי20-Feb-13 7:38
mvpMichael Haephrati מיכאל האפרתי20-Feb-13 7:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 20 Feb 2013
Article Copyright 2013 by Pradeep Reddy S
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid