Skip to Content

iPhone Coding: Using the Accelerometer

You don't need to use a lot of fancy routines to subscribe to data generated by the iPhone's built in accelerometer. A single UIApplication delegate lets you know whenever the iPhone has updated its tilt data.

When you add a method for (void)acceleratedInX:(float)xx Y:(float)yy Z:(float)zz to any UIApplication, the iPhone sends regular messages to your application giving you values for X, Y, and Z. These values are not exactly what I first expected:

X = Roll X corresponds to roll, or rotation around the axis that runs from your home button to your earpiece. Values vary from 0.5 (rolled all the way to the left) to -0.5 (rolled all the way to the right).

Y = Pitch. Place your iPhone on the table and mentally draw a horizontal line about half-way down the screen. That's the axis around which the Y value rotates. Values go from 0.5 (the headphone jack straight down) to -0.5 (the headphone jack straight up).

Z = Face up/face down. I expected the Z value to correspond to yaw. And it does not. It refers to whether your iPhone is face up (-0.5) or face down (0.5). When placed on it side, either the side with the volume controls and ringer switch, or the side directly opposite, the Z value equates to 0.0.


I went to expert Nate True for some advice about these values. He explained the reason that Z is not yaw. It's because yaw is a compass value, something the iPhone can't measure. X, Y, and Z are linear acceleration values. They point to whichever way gravity is operating. Together, X, Y and Z form a 3-D acceleration vector that indicates the direction of gravity. "Say you're tossing the phone up into the air, then all the values will fall to 0 you could make an app that makes the phone scream if you drop it."

You can use the X and Y values to determine the highest point of the iPhone by using an arctan function, i.e. float angle = atan2(yy, xx);. I've put together a sample application that demonstrates this. As the screen shot here shows, it provides the raw X, Y and Z values and keeps the arrow pointing to whatever point is "up" for your iPhone.

Makefile

mainapp.m

SampleApp.h

SampleApp.m

If you'd like to play some more with the accelerometer, I've also thrown together a simple game called Butterfly that allows you to slide a net around your screen to catch a virtual monarch butterfly.



Categories

iPhone

You don't need to use a lot of fancy routines to subscribe to data generated by the iPhone's built in accelerometer. A single UIApplication...
 

Add a Comment

*0 / 3000 Character Maximum

19 Comments

Filter by:
Charlie

I'm writing an iPhone compass.
Although it is of much more limited use. :)
http://mockdock.com/compass/

September 19 2007 at 7:12 AM Report abuse rate up rate down Reply
Alex

ross byers, umm.. The Wii remote has yaw, and it doesn't have a gyro in it.

September 17 2007 at 10:01 AM Report abuse rate up rate down Reply
Alex

so then I was wrong it seems :(

Once I figured out how to code for the iPhone I was gonna make a version of Nate True's thing that was a 3D arrow instead of 2D, but I guess without a Z it's not possible :(

or is it just me? (it is pretty early in the morning and I am not thinking this through on purpose)

September 17 2007 at 9:59 AM Report abuse rate up rate down Reply
dalasv

Can someone make an app that lets me hit my iPhone to snooze the alarm?

September 16 2007 at 6:02 PM Report abuse rate up rate down Reply
C S

Theramin: Can someone pleeeeaaaaase make an app for the iphone to make Theramin sounds!?!

September 16 2007 at 5:39 PM Report abuse rate up rate down Reply
Rob Terrell

#11: I took Erica's code and did that: http://robterrell.com/iSaber.app.zip

September 11 2007 at 8:04 AM Report abuse rate up rate down Reply
jgjay

+1 for a setting up the toolchain and compiling a simple Hello World app for iPhone!

Erica... make it so. :)

September 11 2007 at 6:16 AM Report abuse rate up rate down Reply
Tom Robinson

I've been working on a few neat things with the iPhone's accelerometer, but it's incredibly frustrating having an accelerometer but not a gyro (or Wiimote's "sensor bar").

September 11 2007 at 4:01 AM Report abuse rate up rate down Reply
Dmitry Kirillov

Hey, isn't it a right time to write "iPhone Saber" application, similar to MacSaber and WiiSaber? :)

September 11 2007 at 12:27 AM Report abuse rate up rate down Reply
Ross Byers

Warning: Science content

There is a reason that you cannot get yaw from the acceleromter: It's an acceleromter, not a gyro. Assuming the iPhone is at rest, the only thing the accelerometer can measure is gravity. So imagine a line with a weight on it (a plumb-bob) tied to your iPhone. If you pitch or roll it, the angle of the string changes. This is measurable. Now rotate it in place, yawing it. The angle of the string does not change. Thus, the accelerometer cannot measure it.
This is why the Wiimote, which uses the same technology, requires the 'sensor bar' in order to get correct yaw.
PS: The numbers being reported at not angles in an arc, they are forces on an axis, where X=the narrow axis(left-right), Y=the long axis(forward-back), and Z=the vertical axis(up, down).

September 10 2007 at 9:31 PM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.