Well, simply the sensor should read 200 on an axis when it is pointing straight up. The other axis wshould read 0. That reading of 200 is the force of gravity.
It's possible to detrimine the angle that the sensor is relative to that force.
Without going into a lot of explanation the simple way to do it is:
float unitX = ((float)accellX)/200.0;
float angle = acos(unitX);
angle = radiansToDegrees(angle);
where accelX is the reading of the x axis of the sensor. 200 is the force of gravity.
accelX can be changed to an axis you want and you should verify the the reading at level to make sure that it reads 200;
If you just want it to level something here's a simple sample program
(x should read 0 when level)
Hitechnic accelerometer sensor driver
#define accelAddress 0x02
void accelStart(tSensors port)
accelI2C.DeviceAddress = accelAddress;
accelI2C.LocationPtr = 0x42;
void readAccel(accelData &values)
nI2CBytesReady[Accelport] = 0;
sendI2CMsg(Accelport, accelI2C.MsgSize, 6);
while (nI2CStatus[Accelport] == STAT_COMM_PENDING)
if (nI2CStatus[Accelport] == NO_ERR)
readI2CReply(Accelport, rawData, 6);
values.x = ( rawData << 2) + (0xFF & rawData);
values.y = ( rawData << 2) + (0xFF & rawData);
values.z = ( rawData << 2) + (0xFF & rawData);
this program will producce a little "head bobbing" but it illustrates the point.
balancing is much more difficult. I've read that it's not possible with a accelerometer alone, you also need a gyroscope. But that may not be true. I think I've seena balancing robot that only used an accelerometer but there wasn't much in the way of details, so it might of had a gyro as well.