The iPhone's accelerometer has opened up a whole new world for mobile games and applications. Fortunately, Apple has made reading the accelerometer's values an almost trivial task. The hard part is just up to you - how to actually use it. This tutorial will introduce you to the accelerometer, how to read it, and how to create a simple user interface to display its X, Y, and Z values.
For the purposes of this tutorial, I'm going to assume you know how to create a basic user interface. If you don't, I would recommend checking out our previous getting started tutorial. Below you can see an image and a quick video of the application we'll be building today.

The first thing you should notice is that we've got some UILabels and some UIProgressView objects that are used to display the accelerometer's values. I created a new UIViewController subclass called MainViewController and added these objects as properties. Here's my new header file:
//
// MainViewController.h
// AccelerometerTutorial
//
// Created by Brandon Cannaday on 8/5/09.
// Copyright 2009 Paranoid Ferret Productions. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController <UIAccelerometerDelegate> {
IBOutlet UILabel *labelX;
IBOutlet UILabel *labelY;
IBOutlet UILabel *labelZ;
IBOutlet UIProgressView *progressX;
IBOutlet UIProgressView *progressY;
IBOutlet UIProgressView *progressZ;
UIAccelerometer *accelerometer;
}
@property (nonatomic, retain) IBOutlet UILabel *labelX;
@property (nonatomic, retain) IBOutlet UILabel *labelY;
@property (nonatomic, retain) IBOutlet UILabel *labelZ;
@property (nonatomic, retain) IBOutlet UIProgressView *progressX;
@property (nonatomic, retain) IBOutlet UIProgressView *progressY;
@property (nonatomic, retain) IBOutlet UIProgressView *progressZ;
@property (nonatomic, retain) UIAccelerometer *accelerometer;
@end
A couple of things might jump out at you - first, I have another property for a UIAccelerometer object. This object is actually what's used to receive values from the accelerometer hardware. The UIAccelerometer sends updates through the use of the UIAccelerometerDelegate protocol, which is why I've marked my class as implementing this protocol.
All right, we've got the declarations out of the way. Let's now set up the UIAccelerometer object. I added code to my UIViewController's viewDidLoad method for this.
- (void)viewDidLoad {
[super viewDidLoad];
self.accelerometer = [UIAccelerometer sharedAccelerometer];
self.accelerometer.updateInterval = .1;
self.accelerometer.delegate = self;
}
Instances of UIAccelerometer should never be created, they should only be acquired by getting the system's shared accelerometer object. The updateInterval property is used to instruct the UIAccelerometer object how often it should send updates to its delegate. In this case I set it to .1 seconds - so I'll get updates 10 times a second. Lastly, I simply set the delegate to the UIViewController.
We're actually pretty much done. All that's left to do is implement the delegate's method to receive accelerometer updates.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
labelX.text = [NSString stringWithFormat:@"%@%f", @"X: ", acceleration.x];
labelY.text = [NSString stringWithFormat:@"%@%f", @"Y: ", acceleration.y];
labelZ.text = [NSString stringWithFormat:@"%@%f", @"Z: ", acceleration.z];
self.progressX.progress = ABS(acceleration.x);
self.progressY.progress = ABS(acceleration.y);
self.progressZ.progress = ABS(acceleration.z);
}
The raw acceleration values are stored in the UIAcceleration object passed into this message. The values are simply what the hardware is reading - where +1 is approximately equal to 1G of acceleration. I used a simple string format to update my labels, then take the absolute value of each reading for the progress bar - since progress bars can only be values between 0 and 1. Below is an image from Apple's documentation that explains the values returned for each axis.

And that's it! We now have a working application that uses the accelerometer and displays its values on the screen. The values returned are not filtered in any way, so Apple recommends doing some averaging on the data before using it in a real application. Now that you know how to use the accelerometer, it's completely up to you now to figure out inventive ways to use the data. The example application we created today can be downloaded below.
Source Files:
Just wanted to say thanks for a quick and simple tutorial that shows off something really cool!
My Delegate and my View Controllers are instances of UIView so I keep getting the warning message: Does Not Implement UIAccelerometerDelegate
The examples I have looked at all are instances of NSObject... Which is not the case in my application. I am using a TabBar with 3 views. I only need the accelerometer in the one view.
Any Help?
Cheers man . . . , me too got the same warning n i also fixed that by referring this example
Hahahaha
After analyzing your example I fixed this by adding to my @interface
[objc] @interface FirstViewController : UIViewController { [/objc]
Thanks for your help!
Peace.
Thank``s a lot, really usefull and simple!
GO ON WORKING LIKE THISSS!
How can i test in simulator (i dont have iphone )
Jooo, I don't think you can test accelerometer functions without a phone.
Thanks for the great code. I'm new to obj-c, do you have any hints on how I would store say 15 seconds of accelerometer data? Would I create an array and stick it in there?
I'd create an NSMutableArray to hold the UIAcceleration objects. Whenever it's read, simply call addObject on the array and supply the object.
Thanks The Reddest. I will try that out and for an encore I will try to email it as a message or attachment or ftp it out.
Hey :)
Cool & simple tutorial.
One question though - I want to use my iphone to measure the direction which i am heading to. How can i isolate the right coordinates from the accelerometer?
Thanks!!!
You can't get direction from the accelerometer, you'll have to use the CoreLocation framework.
does this concept work on samsung omnia accelerometer ? C# vs2008
Thanks Buddy, Cool
Thanks, it helped me a lot!
Thanks for giving such a helpful project
Anybody built a "black box" app for crash data logging?
Thanks for your helpful codes. I just was wondering if any one knows how I would be able to record my accelerometer readings in a text file?
how would you use this code to make a it count the steps you take?
great man... inspiring.. thanks alot
hi, thanks a lot, just to point out that to avoid warnings need to @synthesize labelX, labelY, labelZ, progressX, progressY, progressZ, accelerometer;
(for noobs like me! haha) thank you again
Nice tutorial! Just add @synthesize labelX, labelY, labelZ, progressX, progressY, progressZ, accelerometer; under @implementation weatherZombieGameViewController inside of your view controller's .m file to get rid of those warnings.
Thanks a lot, this was very helpful.
How to get these values when the application is in sleeping mode also
Thx for this tutorial, But i've an error at this line :
self.accelerometer = [UIAccelerometer sharedAccelerometer];
-[CoreLocationDemoViewController setAccelerometer:]: unrecognized selector sent to instance 0x160350 2011-07-11 10:41:54.738 CoreLocationDemo[3956:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CoreLocationDemoViewController setAccelerometer:]: unrecognized selector sent to instance 0x160350'
Do you have an idea for me ? Thx again
Hi all, ee are a non-profit organisation that aims to provide tools to the public to help save lives easier. Our project involves using an everyday smartphone, eg iPhone, to take measurements of chest compressions during CPR training, and then locking the parameters into the phone, so that when the same person does refresher training or to use it on an actual life, the person will get valuable feedback to ensure that he does the chest compressions accurately. What I am asking for is if you able to analyse the signal strength for me to see if the signal is 1. strong enough from smartphones ( a couple of models for example ), and 2. if there is "noise" to be cut out while using the smartphone to perform chest compressions. I presume the signals will come from the accelerometer or gyroscope in the smartphone. If you are able to help us, we will publicise you as an important partner in our fight against sudden cardiac arrest death, just like our other partners. Our movement is current being partnered by people and across the world. You can read about our work at www.firstaidcorps.org. We will appreciate your expertise. Rgds Dana Elliott Founder First Aid Corps
My apologies, My contact email is at cocreate@firstaidcorps.org
Thanks for this great tutorial.
This is great, thanks so much!
Sample code not extracted for me. So Please can you check or update sample code. Thx
I just tested it and the zip extracted without issues.
cool tutorial! thank you...!
it is very useful to me thank you
Awesome, this is just what I was looking for. Thanks!
Is the link to the sample code no longer valid?
Seems to work for me.
After hours of trying to get something to work with isimulator, this idi the trick in 5 minutes. thank you
Hi I tried to store the accelerometer values into an NSMutableArray however the array prints and displays the realtime values, but I only want it to display the exact values say 15 seconds ago how would you go about that exactly?
how can we detect device is stable or shake ???
Hi together,
what does this ABS (...) stands for? Whats the function behind it?
Never seen it before ...
It's the absolute value function. Since my progress bars only show positive values, and the accelerometer reports both negative and positive values, I convert the negative ones to positive.
ABS(-9) = 9 ABS(9) = 9
Basically it drops the negative sign, if there is one.