Posted on by & filed under Arduino, Schools & Curriculum, Software and Apps.

Lessons Menu:

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.

Arduino 5 Minute Tutorials

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.

Arduino 5 Minute Tutorials

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 minute (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.

Additional Considerations

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.

Want to learn more? Start with the material put out for free by Analog Devices, makes or many MEMS acceleromters, gyroscopes and other sensors.

Tags:

12 Responses to “Arduino 5 Minute Tutorials: Lesson 7 – Accelerometers, Gyros, IMUs”

  1. cbenson

    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.

  2. Sigurt

    You really should put a link to this article on the arduino playground.
    It is much more explicit than the one allready there :)

  3. Dom

    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 :)

  4. Bhavik

    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.

  5. cbenson

    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

  6. Joan

    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!

  7. Hana

    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?????????????

  8. ben

    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.

  9. Arinze Chukwunonso

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

Leave a Reply

  • (will not be published)