Sensors: Using the Accelerometer to detect motion

This tutorial introduces the accelerometer – at a high level – as a tool to detect the phone being shaken. You can use this feature as another kind of user input to your app – for example, make a game where shaking the phone resets the game play or starts a new game.

An accelerometer is a hardware device that detects and measures motion, typically in three axes: X, Y and Z. For example, if the phone is moved left or right, the acceleration changes in the X axis and the accelerometer returns a value indicating the X axis movement.

Smart phones – and many modern devices – have special hardware accelerometer components built in. The orientation sensor, described previously, is actually a software sensor that uses the hardware accelerometer but converts acceleration into orientation values.

The purpose of this demonstration app is to show one example of using the accelerometer.

The user interface is simple – it displays a Start button and a Stop button, and the status of the accelerometer.

AccelUI

 

At the bottom of the Designer Viewer, you can see that three non-visible components have been added to this app. The AccelerometerSensor1 is the component that detects phone movement. Find the Accelerometer in the Sensors section of the Palette.

The Player1 component is a media player – for our app, when the phone is shaken, it plays an mp3 audio file that is specified as the Source in the properties for the player. Find the Player component in the Media section of the Palette.

AccelPlayerProperties

To specify the mp3 file, click on source and select an mp3 file from your disk drive. I used a sound effects file from Soundbible.com.

Regardless of the length of the mp3 audio clip, in our app we will limit the mp3 file to playing for just two seconds (because we feel like it!)

How do we limit the playing of the file to two seconds? By using the App Inventor clock timer. Find the Clock in the Sensors section of the Palette and drag a Clock timer to the Viewer – this becomes a non-visible component. The clock acts as a timer – creating a time event at specified intervals. We write a clock event handler to process the timer event.  We set the timer to a two second time and then when the timer event occurs, the event handler turns off the media player.

How This Works – The Blocks

The Start button activates the accelerometer by setting its Enabled property to true. The sensitivity of the accelerometer – that is, how sensitive it is to motion – is set by adjusting the value of the Sensitivity property. Setting the value to 3 makes the accelerometer more sensitive, while a value of 1 makes the accelerometer less sensitive.  For our demonstration app, the Sensitivity ought to be set to 1 (less sensitive) but because the app is intended to show some features of the accelerometer, we will detect the amount of shaking in a different way.

AccellStart

The remaining features, while simple, are explained in sort of reverse order – meaning we show how to stop the player before we see how to start it!  We start with the Stop button. When the Stop button is pressed on screen, we disable the accelerometer, disable the timer, and disable the media player.

The accelerometer is disabled by setting its Enabled property to false. The Clock’s timer is disabled be setting the TimerEnabled property to false, and the media player is stopped by calling its Stop method.

AccellStop

The media player is started by calling it’s Start method, as seen below. Because this set of blocks is very wide, the blocks are displayed very narrow – click on the image to display in a larger size. The rest of the code blocks, here, will be explained in a moment.

AccellShaking1

When the Clock’s time interval has elapsed, the Clock throws a Timer event. When this event occurs, we stop the player and stop the clock timer:

AccellTimer

Now, let’s go back and look at the Accelerometer event handler:

AccellShaking1

The Accelerometer has an event called Shaking. Rather than detect each individual movement of the phone, we can let Android run some code behind the scenes that determines if the phone is being shaken – versus merely moved.  We do not need to know how this works – it just happens by magic and simplifies our programming.

Remember that the Accelerometer indicates the device’s motion in X, Y and Z axes. Values corresponding to these directions of movement are found in the XAccel, YAccel and ZAccel properties and represent meters per second squared.  Values may be either negative or positive.

For our app, we want to restrict the shake detection to fairly aggressive shaking. To do this, an if-then block determines if any of the X, Y or Z values are fairly large – and if any of these comparisons is true, then the player is turned on and the two second timer is activated.

Note that we use the “absolute” value of the X, Y and Z acceleration.  The absolute value function converts negative values to positive values (the absolute value of -5 is 5 and the absolute value of 5 is just 5, unchanged). The absolute value of each acceleration component is then compared to the value we have stored in a global variable named SHAKELIMIT. This is set with a global initialize variable and set to a value of 18 – but you can experiment with this to see if other values work better for you. With the value set at 18, you need to give the phone a very good shake to activate the media player.

The accelerometer may be used in other ways too – for example, it can be used to detect any device motion. Then the values of the X, Y and Z axis motion can be converted, using math, into angles, magnitude of the movement, and other values.

Downloads

  • Source code App Inventor “.aia” source file (App Inventor source code files have the filename extension .aia)
  • Download the source code to your computer. Then, in App Inventor, go to the Projects menu and select “Import project (.aia) from my computer…”

4 thoughts on “Sensors: Using the Accelerometer to detect motion”

  1. Thank you for such valuable information. Your site is great!
    I want to make an app that completes an action when you stop moving (driving). Is the Accelerometer the best tool for this. I have tried the code tutorial and cant get it to sense slow acceleration forward. I might be on the wrong track completely as my initial idea was to use GPS?

    1. The accelerometer is best for detecting small movements and the GPS for larger movements.

      If I were to write this – and am thinking this sounds like a good tutorial idea! – I would probably start with GPS and detect when the GPS has not detected movement for a period of time.

      When driving, do we want to detect merely when we stop at a stop sign? Or when the car is parked? To detect if the car is parked, we will want to check to see if the GPS has shown no movement for, say, 5 or 10 minutes, to avoid detecting being stuck at a signal light or in traffic.

      Another item to keep in mind is that GPS is not 100% accurate. GPS works by measuring the time delay of signals coming from satellites. These signals can be delayed by very small amounts depending on atmospheric and other situations. Therefore, to detect a stop, you need to round off the GPS location to within, say, 30 feet (10 meters or even a greater circle size). While at a stop, the GPS location might return different values within that circle. That’s why you need to round off or assume an “error” of GPS measurement.

      Sounds like a fun project concept.

      Ed

Comments are closed.