Filed under: iPhone
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.
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.


![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 1)
neodymium said 5:37PM on 9-10-2007
too bad there's no compass; if there were you could make a digital brunton for calculating strike/dip of sedimentary strata. that would be cool.
Reply
neodymium said 5:40PM on 9-10-2007
... although you could still make a cool program to calculate the height of tress and buildings if you type in how far you are from the base and then sight along the phone to the top of the object...
Reply
Graham said 6:27PM on 9-10-2007
Did I fall asleep when this became The Unofficial Objective C Weblog? I'm getting fed up with these "programming For iPhone" posts that make sense to very few people. Aren't there plenty of other objective c sites that this would be more appropriate for?
Reply
Phil Harvey said 4:08PM on 9-11-2007
I think these posts are great. One thing I think would help is a post on how to set up the iPhone toolchain and how to get a simple hello world up and running.
Thanks
Reply
Donald Burr said 6:41PM on 9-10-2007
#3,
Bugger off.
Reply
Donald Burr said 6:41PM on 9-10-2007
BTW I would definitely like a "getting up and running with the toolchain" post. My feeble attempt at getting it going ended in pain and misery.
Reply
Sahil said 7:12PM on 9-10-2007
I'm with Donald on this, I also tried numerous guides all over the internet to get Toolchain going, and although it does compile things, I run into all sorts of errors regarding missing files with compiling various of the open source apps (eg. apollo, mobilechat etc).
Please I will write an awesome app if I can just get off my ass (the proverbial "onto my feet")
Reply
Alex Polson said 7:22PM on 9-10-2007
I definitely agree with the need for a "How to setup the tool chain" post. That would help out a lot.
Reply
Rich said 9:44PM on 9-10-2007
Agreed.... would love to see a "How to setup the tool chain" article!!!
Reply
Ross Byers said 9:32PM on 9-10-2007
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).
Reply
Dmitry Kirillov said 12:28AM on 9-11-2007
Hey, isn't it a right time to write "iPhone Saber" application, similar to MacSaber and WiiSaber? :)
Reply
Tom said 4:32AM on 9-11-2007
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").
Reply
jgjay said 6:17AM on 9-11-2007
+1 for a setting up the toolchain and compiling a simple Hello World app for iPhone!
Erica... make it so. :)
Reply
Rob Terrell said 9:33AM on 9-11-2007
#11: I took Erica's code and did that: http://robterrell.com/iSaber.app.zip
Reply
C S said 5:42PM on 9-16-2007
Theramin: Can someone pleeeeaaaaase make an app for the iphone to make Theramin sounds!?!
Reply
dalasv said 6:02PM on 9-16-2007
Can someone make an app that lets me hit my iPhone to snooze the alarm?
Reply
Alex said 10:02AM on 9-17-2007
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)
Reply
Alex said 10:02AM on 9-17-2007
ross byers, umm.. The Wii remote has yaw, and it doesn't have a gyro in it.
Reply
Charlie said 7:13AM on 9-19-2007
I'm writing an iPhone compass.
Although it is of much more limited use. :)
http://mockdock.com/compass/
Reply