Go to Map Courses Lab Glossary How it works Troubleshooting Start →
ITEN
← Course 04 · Sensing motion (Lesson 04.01 · 15 minutes · Motion sensors · For everyone)

The phone senses motion.

Inside your phone, a mass smaller than a grain of sand feels every push. Here you'll find out how the accelerometer and the gyroscope turn motion into six traces, and you'll watch your own as you move the phone.

What you learn
  1. What the accelerometer and the gyroscope measure, and why gravity always shows up
  2. What a window is, and what the features of a window are
  3. Why motion sensors are perfect for small models
You need to know

If you don't remember one of these, the link takes you to the chapter that explains it.

How do you want to follow it
Preparing the scene…
  1. 01 The accelerometer

    The phone feels pushes in three directions.

    Inside the phone there's a tiny mass hanging from silicon springs. When the phone speeds up, the mass stays behind for a moment and the springs bend. The chip measures how much, in three directions, x, y and z. That's the accelerometer. Even lying still on the table it reads something, gravity, about 9.8 meters per second squared, pointing down.

    In practice. Put the phone down on the table and watch the traces in the exercise below. The axis that reads about 9.8, plus or minus, is the one at right angles to the table.

01 / 07
(Go deeper)

A few more numbers.


(01)

How an accelerometer is made

The accelerometer in your phone is a MEMS, a micro electro mechanical system. On a piece of silicon smaller than a millimeter there's a moving mass with lots of tiny teeth, facing other teeth that don't move. When the mass shifts, the gap between the teeth changes, and the chip measures the difference as a change in electric charge.

It usually measures up to 8 or 16 times gravity, enough for a fall or a jump. It costs a few cents and uses a fraction of a milliwatt. That's why you find one in every phone, watch, earbud and game controller.

  • 9.81meters per second squared, which is 1 g
  • 16 gthe most that many accelerometers can measure
(02)

The most common features

In 2012 a team at the University of Genoa published UCI HAR, one of the most widely used datasets for activities. Thirty people wore a phone on their belt and did six activities, among them walking, climbing stairs and sitting. The phone took 50 readings per second, cut into windows of 2.56 seconds. For each window the team worked out 561 features.

In practice you need far fewer. The table shows the ones people use almost every time. The typical mistake of models on this dataset is mixing up sitting and standing, because a phone on the belt stays still in both cases.

FeatureWhat it tells youGood for
Average of each axisHow the phone is turnedTelling lying, sitting and standing apart
SwingHow much it movesStill or moving
Minimum and maximumThe hardest hitsFalls, shakes, bumps
Number of peaksHow many times the movement repeatsSteps, repetitions
Main rhythmHow fast the rhythm isWalking or running
(03)

The sensors in the browser, in code

In the browser the sensors arrive through the devicemotion event. On iPhone you first have to ask for permission, and the request has to come from a tap. On Android they arrive right away on https pages. Each event carries the acceleration with and without gravity and the rotation rate, plus the time between one reading and the next.

The exercise in this lesson does exactly that. It listens for the event, keeps the last two seconds of readings, and works out a few features five times per second.

if (DeviceMotionEvent.requestPermission) await DeviceMotionEvent.requestPermission();
addEventListener("devicemotion", e => {
  const a = e.accelerationIncludingGravity;   // x, y, z in m/s²
  const g = e.rotationRate;                    // alpha, beta, gamma in degrees per second
  samples.push([a.x, a.y, a.z, g.alpha, g.beta, g.gamma]);
});
(Try it on your phone)

Live traces


Pick up your phone and press the button. You'll see the six traces scroll by, the accelerometer on top and the gyroscope below. Underneath you'll find a few features worked out on the last two seconds. Try shaking the phone, turning it, walking around.

The tool needs JavaScript turned on.

On iPhone the phone asks for permission to use the motion sensors, so tap Allow. A computer has no motion sensors, so this only works on the phone. The readings never leave the page.

(Quiz)

4 questions to close the lesson.

When you have answered them all, the lesson counts as done and its stone on the map turns yellow. Answering is what counts, not the score.


  1. (01)

    Your phone is lying still on the table. What does the accelerometer read?

  2. (02)

    With 50 readings per second and six axes, how many numbers are in a two-second window?

  3. (03)

    Why do you work out features instead of using all the readings?

  4. (04)

    Your model only recognizes a gesture when the phone is upright. What should you do?

To close the lesson, answer the quiz

← From voice to text, and back Back to the map
(Next lesson · 04.02) Recognizing a gesture or an activity