Skip to content

November 25, 2010

9

How to Use the Gyroscope of Your iPhone in a Mobile Web App

A Gyroscope

This week's release of iOS 4.2 for iPad and iPhone comes with some nice little features most people will not immediately become aware of as they're neither directly visible in the iOS UI nor are they mentioned in Apple's official release notes. You have to dig a little deeper to find them. One of them is a JavaScript API for the iPhone's gyroscope. Read on to see it in action and learn how to use it.Your iPhone has a number of sensors, some of which are rather essential for the phone's operation (such as the microphone). While the accelerometer and the gyroscope might not be the most essential sensors for a phone, they're certainly the most exciting ones. While accelerometer measures the acceleration you induce on the phone, the gyroscope gives a rather precise feedback on the orientation of the phone.

Until now, web developers didn't have access to the accelerometer sensor and the gyroscope sensor. With this week's release of iOS 4.2, this has changed and we can now use DeviceMotionEvent and DeviceOrientationEvent to determine the acceleration and orientation data of the phone.

Let's first determine whether the current browser supports device orientation sensing:

if (window.DeviceMotionEvent==undefined) {
}

We can then read the sensor data by registering the respective callbacks. Here's how you read the accelerometer's data:

window.ondevicemotion = function(event) {
  ax = event.accelerationIncludingGravity.x
  ay = event.accelerationIncludingGravity.y
  az = event.accelerationIncludingGravity.z
  rotation = event.rotationRate;
  if (rotation != null) {
    arAlpha = Math.round(rotation.alpha);
    arBeta = Math.round(rotation.beta);
    arGamma = Math.round(rotation.gamma);
  }
}

The gyroscope's data can be read like this:

window.ondeviceorientation = function(event) {
  alpha = Math.round(event.alpha);
  beta = Math.round(event.beta);
  gamma = Math.round(event.gamma);
}

I've put together a little demo that uses the sensor data to color some boxes on the phone's screen. Here's a short video showing it in action:

If you want to take it for a spin, open this URL in mobile safari on your phone: http://demos.peterfriese.de/gyro/gyro.html.

(image of Gyroscope by stop that pigeon! taken from http://www.flickr.com/photos/25312309@N05/2651042796/)

Thanks for reading this post. Follow me on twitter here to be notified about updates and other posts I write. Or, subscribe to my RSS feed here

Fork me on GitHub
  • http://bigyellowstar.com Damien

    Does anyone understand the difference between alpha/beta/gamma values in devicemotionovent (within “rotationrate”), on the one hand, and in deviceorientationevent, on the other? They both appear to be from the gyro. Maybe they just use a different standard of reference?

    Documentation links:
    https://developer.apple.com/library/safari/#documentation/SafariDOMAdditions/Reference/DeviceMotionEventClassRef/DeviceMotionEvent/DeviceMotionEvent.html

    https://developer.apple.com/library/safari/#documentation/SafariDOMAdditions/Reference/DeviceOrientationEventClassRef/DeviceOrientationEvent/DeviceOrientationEvent.html

  • Pingback: Prototype in HTML5? « SNOnetwork

  • Christopher

    Great article @peterfriese do you know if there is a way to access the compass via a similar method? Or do you think this will be supported in the future?

  • Nikkostikis

    I am having trouble matching the rotation rate data (alpha, beta, gamma) I get using ondevicemotion to the ones through an app like SensorData. I can’t seem to be able to increase the sampling rate through javascript and I end up with a 20Hz signal, which is way off compared to a native app captured 20Hz rotation rate signal. Would you know anything about it?

  • Da_131

    The code has a logic error. When the phone doesn’t move or it moves with constant velocity, the accelaration should be zero. This isnt the case here.

  • http://www.register-domainname.in Register domain India

    Really super invention because now a day, iphone is very much famous among people. Even me too mad on iphone. Good article. Keep on doing.

  • http://er247plus.com/ Steve Waugh

    Very interesting
    innovation buddy! I really don’t know about the technique, though I’m using an
    iPhone since long time. I must say you are great software architect. I’ll come
    again to know more interesting segments. Thanks

  • Idanbachar26

    hey,

    how can you make it sample the gyro faster?

  • http://www.adodis.com/Hosted-Ecommerce-Software.php Hosted Ecommerce Software

    Awesome tutorial dude !! Even Iphone was amazing.