# Arduino 5 Minute Tutorials: Lesson 7 - Accelerometers, Gyros, IMUs

### Lesson 7 Hardware:

1. Computer / Laptop or Netbook
2. Arduino Microcontroller
3. USB to Serial Adapter (if your microcontroller does not have a USB port)
4. Appropriate USB cable (Arduino boards draw power from the USB port – no batteries yet)
5. Analog accelerometer, gyroscope and/or IMU
6. Connectors (between the IMU and the Arduino
Accelerometers, gyroscopes and IMUs are incredibly useful little sensors which are being integrated more and more into the electronics devices around us. These sensors are used in cell phones, gaming consoles such as the Wii wireless remote control, toys, self-balancing robots, motion capture suits and more. Accelerometers are used mainly to measure acceleration and tilt, gyroscopes are used to measure angular velocity and orientation and IMUs (which combine both accelerometers and gyroscopes) are used to give a complete understanding of a device's acceleration, speed, position, orientation and more.

When choosing an accelerometer, gyroscope or IMU, it is also important to consider the type of output; depending on the type of sensor, readings can be output as:

• Serial data (Tx pin)
• I2C (SDA, SCL)
• Analog
• TTL
• others...
In this tutorial we're only going to cover analog output. The code shown below includes the output for a single axis sensor and factors in the rest value.

## Accelerometer

Accelerometers measure acceleration in one to three linear axes (x, y, z). A single axis accelerometer can measure acceleration in whichever direction it is pointed. This may be good for a rocket, an impact, a train or other scenario where the device really moves in one basic direction. Knowing the acceleration and time, you can use mathematics to find the distance traveled by the object. There are fewer and fewer single and double axis accelerometers on the market because a triple axis accelerometer can do so much more. Thanks to low manufacturing costs the three axes accelerometers are not much more expensive than single or double.

Acceleration due to gravity is a constant and is in fact measurable using an accelerometer. When placed parallel to the ground, acceleration due to gravity would only be "felt" by one axis. However, when tilted, this acceleration would appear as components of two (or three) axes. You can get an idea of how to use an accelerometer to measure tilt here and here.

Connect the accelerometer to the Arduino; each output pin goes to one of the analog pins on the Arduino, the Vin pin goes to the 5V pin on the Arduino (read the user guide to ensure the Vin pin is 5V as opposed to 3.3V), and connect the GND pin to the GND pin on the Arduino. Note that there is no need for additional electronics! Next, open the sample sketch File -> Examples -> Sensors -> ADXL3xx. Upload to the Arduino and see the values change.

In order to choose the right accelerometer, consider the maximum linear acceleration the sensor will be subjected to. If you are planning to add an accelerometer to a small mobile robot, you will likely use a 2g accelerometer (even that is likely overkill), whereas if you are attaching it to a rocket, a 16g accelerometer is likely a better choice. When connected to a 10 bit ADC, the 2g accelerometer will have an accuracy of 2 / 1024 = 0.002g, and the 16g accelerometer will have and accuracy of 16 / 1024 = 0.0156. Therefore if you only need a range of 2g, but purchase a 16g accelerometer, you will only have about 128 possible readings, instead of the full 1024. Conversely, if you choose a 2g accelerometer when you really needed a 16g, you will get a lot of "maximum (1024) "readings since the acceleration is "off the scale".

## Gyroscope

Gyroscopes measure angular velocity in α, β, γ (see image below). Gyroscopes can be used to help with stabilization and well as changes in direction and orientation. Unlike accelerometers, gyroscopes do not have a fixed reference, and only measure changes. To choose the right gyroscope for your needs, consider the maximum angular rate of change (degrees per second) your product will be subjected to. A remote control will likely rotate at less than 1 rotation per second (360 degrees per second), while a rocket tumbling out of the sky may be rotating at 1500 degrees per second. When connected to the same microcontroller (10 bit for example), the 360 degree/s gyro will have an accuracy of 360 / 1024 = 0.35 deg/s, whereas the 1500 deg/s gyro will have an accuracy of 1500 / 1024 = 1.46 deg/s. Therefore if you chose a 1500 deg/s gyro when you only needed a 360 deg/s gyro, you will only get about 245 readings as opposed to 1024.

Courtesy: Wikipedia

## IMU

An IMU (Inertial Measurement Unit) usually consists of an accelerometer and gyroscope and is used to measures an object's orientation, velocity etc. Often additional sensors (magnetic, temperature) are included to improve accuracy. The number of "degrees of freedom" indicates the number of different axes measured by the chip. For example, combining a three axis accelerometer with a two axis gyroscope would be consider a 3+2 = 5 DoF IMU.

When using accelerometers, gyroscopes or inertial measurement units (IMUs) to obtain positions in space, it is important to note that there are several additional factors that will affect the readings, the main obstacle being the sampling rate. Microcontrollers require a certain amount of time to read values being provided to them by the sensor, and because of this, the values between these readings are lost. There are several mathematical methods (a Kalman filter being a popular choice) that attempt to compensate for this. A second source of error is that readings are often affected by fluctuations in temperature. Most datasheets associated with micro-electro-mechanical systems (MEMS) attempt to describe how temperature affects the output.

This is a companion discussion topic for the original entry at https://community.robotshop.com/tutorials/show/arduino-5-minute-tutorials-lesson-7-accelerometers-gyros-imus
1 Like

im trying to implement a tilt measurement system with arduino not just orientation but tilt to an accuracy of upto 1 degree. I need suggestions on the sensor to use.

Hi, I am looking for correction of my robot car with gyro sensor. Robot car containing four wheel , when travelling forward for long distance it does not travel in straight line instead there is angle deviation from straight line. I want to use gyrosensor in this case.
How can i correct that with gyro senor using arduino board???

hi!! thanks a lot for you tutorial.
I’m trying to make an object that I can throw up on the air and know the position on the space, it’s possible?
first, I thought that I only need an accelerometer but, reading your tutorial, I’m sure that I need work with an IMU… is it?
where I can find informations about IMUs and objects positions?. is it difficult to do?
thanx!

An IMU (accelerometer and gyro) are the best way to start. If you just want orientation, you should consider a three axis accelerometer and a magnetic compass. Follow the user guide for each product, and there is some sample code (which you will need to modify) available from arduino.cc

I am looking for something that measure the orientation of the object that is hanging . I am planning to use arduino accelerometer and gyroscope for it. I have no idea about the electronic. SO there any tips or way I can finish my job.

Hello
I am trying to make a arduino robot wirelessly controlled by and arduino nano and an accelerometer. Any ideas on how I would go about doing this??

Thanks,
Dom

It is much more explicit than the one allready there

Connect each of the x and y pins to one of the analog pins (A0 and A1 for example). Connect + to the 5V pin and - to the GND pin. NC is not connected.

Hello guys

Im trying to connect my arduino mega with Gyroscope directly, how can it be done?

This is the link for the Gyroscope that i have http://uk.rs-online.com/web/p/processor-microcontroller-development-kits/7485344/u

Thank you

Typo. Corrected and appreciated

Nice tutorial.
Minor correction: Gyros measure angular velocity, not acceleration.

The link for lesson 8 is not working. When would it start functioning? and when would other lessons be uploaded. This tutorial is heavenly

I am looking for a board with Accelerometer, Gyroscope, GPS and microcontroller to detect the potholes on roads while travelling on a vehicle with this set up connected to a laptop.
I need to get the readings from the Accelerometer and filter the noise from vehicle and apply some algorithm to the data acquired so as to detect the potholes from the accelerometer readings. Gyroscope may also be used for some extra algorithms. GPS is required for locating the potholes when detected by the accelerometer. Also the speed of the vehicle should be noted. The coding is to be done in C Programming for this application. Please suggest a suitable kit which has a data acquisition module in built and can be used in the vehicle and connect to the laptop or computer to record the data obtained. I am a beginner so need guidance … Please suggest…

All of the sensors you seek can actually be found on many UAV flight controllers. Consider the Quadrino, which should be available soon:

This includes an accelerometer, gyro, GPS and more. The firmware can be flashed, though you may try using it with Arduino (close to C).

i want to review different output values of the accelerometer which is interaced with the arduino… how it can be done??

The easiest way is to choose an analog accelerometer and connect it to the Arduino’s analog pins. To display the values, you can use a serial print command and the Arduino serial terminal.
If you have additional questions, it’s best to create a new topic on the RobotShop Forum.

hi!ahm…how can we connect the accelometer with flexsensor using arduino??

The accelerometer could be connected to the necessary IO pin (many acceleromters are analog, so you should use an analog pin; if it’s I2C or other interface, connect it to the Arduino accordingly). The flex sensor needs a voltage divider circuit (if it does not already have one) which is really just a resistor. The thee leads would go to GND, V and an Analog pin.

we are using accelometer ADXL345 3 axis…how can we identify which pin is the x,y and z,the pins are labeled cs,di,do,clk…and how we connect the accelometer together with the flex sensor to the arduino.