Make your own sugar activities

Making Activities That Use The Accelerometer

by Aneesh Dogra

What is an Accelerometer?

As the name suggests an accelerometer is a device which measures acceleration forces in all directions. The XO 1.75+ provides us with a 3-axes accelerometer. A 3-axes accelerometer has 3 axes: X, Y, and Z. X & Y axes are parallel to the plane of the ground and the Z axis is perpendicular to the plane of the ground.

How to get the data?

Getting accelerometer data is as simple as reading from a file. The accelerometer writes its position data in the following file:


This file contains a tupple of (x, y, z) values. Where x, y and z are the acceleration forces in X, Y and Z axes receptively. The x, y, z values range from -1152 to +1152.

The X axis can be used to measure Right and Left inclinations. If you dip your XO to the right you'll get positive X-axis values; if you dip your XO to the left you'll get negative X-axis values.

The Y and Z axes can be used to measure up and down inclinations. Y & Z axes values are positive when you dip your XO towards up; they are negative when you dip your XO towards the bottom.

When the XO is leveled i.e its kept parallel to the plane of the ground X and Y axis values are approximately 0 and Z axis value is -1152.

Here's how the values are get in the Level Tool [1] :

def read_accelerometer(canvas):
        fh = open(ACCELEROMETER_DEVICE)
        string =
        xyz = string[1:-2].split(',')
        x = float(xyz[0]) / (64 * 18)
        y = float(xyz[1]) / (64 * 18)
        canvas.motion_cb(x, y)
        return True

Polling for Accelerometer Values

If you are using accelerometer values in your code, most probably you will need to poll them constantly checking for changes. This can be easily done using GObject's add_timeout method.

GObject.timeout_add(interval, callback, ...)

interval is the timeout between calls to the function in milliseconds.

callback is the function you wish to call.

The GObject.timeout_add() function sets a function (specified by callback) to be called at regular intervals. Additional arguments to pass to callback can be specified after callback. The function is called repeatedly until it returns FALSE, at which point the timeout is automatically destroyed and the function will not be called again. The first call to the function will be at the end of the first interval. Note that timeout functions may be delayed, due to the processing of other event sources.

To Poll the function we read about in the previous section, just add the following line in the main or the init function:

GObject.timeout_add(100, read_accelerometer, canvas)

This would call the function - "read_accelerometer" - every 0.1 seconds.

Stop Polling

Some times you might need to stop polling a particular function. Here's how it is done.

You can use the GObject's source_remove method to stop polling a particular function.


 where tag is the integer returned by the timeout_add function.


Your activity can be tested without an XO too. You can use the Accelerometer Emulator [2].


It has a simple interface with 5 buttons, Left, Right, Up, Down and Reset. These buttons can be used to simulate acceleration forces is that particular direction.

Using the accelerometer emulator

The emulator writes to a particular file pointed by the PATH variable and updates it in real-time, as in the case of an accelerometer. To integrate this emulator in your activity you need to change the ACCELEROMETER_DEVICE_PATH in your activity to PATH. Read more [2]

Activities using the Accelerometer

Activities using the accelerometer are pretty scarce but we do have some activities to take inspiration from :

  1. Level Tool [1]
  2. Turtle Art [3]

[1] :

[2] :

[3] :