How to Use the Accelerometer Sensor

sensors
accelerometer
aia

#1

Overview

This article is a collection of the following pieces of information:

  1. How an Accelerometer works
  2. How to use the blocks
  3. Building a sample app
  4. References
  5. Downloads

1. How an Accelerometer Works

Most modern smartphones have an accelerometer that can measure forces in three directions, or along three axes, which are usually labelled x, y and z. The accelerometer in your phone measures the g-force it experiences in each of these three axes. Even when your phone seems to be at rest the accelerometer will still detect acceleration due to gravity (9.81 m/s/s ) and we can view this with an Android app.

Using Thunkable we can detect if the phone is moved up or down, forwards or backwards, or, left or right.

2. How to Use the Blocks

The accelerometer has two events and four properties that you can work with.

3. Building a Sample App

In this app we just want to read data from the sensor and detect when the phone has been shaken. To build one yourself you need the following components.


The first thing to do is chech whether or not the phone has an accelerometer in the first place. If not you can notify your user about this:

Alternatively, if your user must have an accelerometer to use your app properly you can notify the user and then close the app by doing the following:

It is also possible to turn on and off the sensor. Here, this is achieved with a Checkbox:


Next we can use the Accelertion Changed event to display the accelerations in each direction:


Finally, we show a notification and make the phone vibrate when the phone is shaken:

4. References

Scott Ferguson’s video is excellent

Thunkable Documentation:
http://thunkable.com/reference/components/sensor_components.html

The App Inventor book has information on the Accelerometer starting on page 356
http://www.appinventor.org/bookChapters/chapter23.pdf

Downloads

You can download the sample app and source here:

accelerometer_test.aia (49.7 KB)
accelerometer_test.apk (2.2 MB)


Accelerometer for 1-Player Pong
Gyroscope and google maps
Gyro + GPS + Foto Button = crash in Android 7 every time
Pedometer component
Beginner looking for help/advice in building "Shake Me" app
Frequently Asked Questions (FAQ)
#2

@Domhnall Even without the Video, your Theory is so perfectly Clear and understandable. Kindly make a Lesson on using Thunkable and API (Thunkable with API) if you have an idea on that. i love your skills of explanations. Thanks


#3

Thanks @Brainwork, that’s a great suggestion - I’ll definitely do one on APIs!


#4

WOW, you explained it perfectly in it’s very easy to understand. IMPRESSIONANT :+1: :clap:

Why not add to the FAQ? I will add a staff color to the post


#5

That would be great, thanks @barreeeiroo. I want to do some similar for all the sensors over the next few weeks so it would be nice to have them in the FAQ.


#6

@Domhnall the FAQ is a Wiki so everyone has access to edit it. Feel free to add your post to it. :wink:


#7

Great, thanks for that @Conor. I’ll add it when I get home.


#8

Accelerometer Sensor not working in screen off mode


#9

Best! Best tutorial. thank You