Posted on by & filed under Arduino Tutorials, 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:

48 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

  10. P S B

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

  11. rahul ramesh

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

    • Coleman Benson

      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.

  12. Son

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

    • Coleman Benson

      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.

  13. Son

    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.

    • Coleman Benson

      For the ADXL345 chip, it seems to accept SPI and I2C communication. We suggest becoming familiar with I2C communication as part of the Arduino, which will also teach you what to connect where and the protocol.

  14. Sneeze

    This is a very useful tutorial! Thank you!
    I would like to implement dead-reckoning on a mobile ground robot using a 3 axis accelerometer or a 3 axis gyroscope, with Arduino. What would you suggest? I am also looking for tutorials about coding the data and the algorithm.

    • Coleman Benson

      A gyro is really only useful if your robot will rotate about its axes; an accelerometer is a better choice for land-based mobile robots. You might add some non-contact IR distance sensors. In terms of the code, it really is specific to your setup, although others may have posted code which you can adapt to your needs.

  15. Vinay

    Can I directly connect 9DOF IMU Razor to the arduino without the FTDI breakout to get gyro and acel values??
    If yes, how??
    thanks in advance!!

    • Coleman Benson

      An FTDI breakout simply converts USB to serial. Most Arduino boards already have this, so you only need to connect the serial pins to the Arduino’s serial pins. If you want to leave these free, consider softserial (has its own advantages / disadvantages). Choosing a sensor breakout board which does not have an onboard processor like the Razor will save you a bit of money and make connections easier.

  16. rachelle

    do you have a code for detecting rot/decay in wood or trees using vibration sensor and arduino? i just need this things to create my project

  17. ahito

    Hello, If i have tilt angles for X,Y,Z axis in ADXL335. How can i get back the raw adc values? Is it even possible? How exactly these tilt angles are useful?

  18. mohammed

    Please
    I want to use the gyroscope to guide mobile robot for the same angle of the robot arm connected with the servo motor How do I do that?

  19. sameer

    i am going to build a humonoid robot like NAO(mostly same).what could be the IMU to be chosen??required ranges of the accelero and gyro sensors

    • Coleman Benson

      Since a humanoid robot will likely not be traveling / moving fast, you only need a low ‘g’ accelerometer and a lot low rate gyro. The communication method and exact model are really up to you.

  20. sameer

    what is the function and connection of INT pin(interrupt pin in GY-521 IMU)?

  21. parshav

    i want to take input from camera which will track faces and pass its status to accelerometer, whch will then move 2 servos accordingly..can u help me?
    servo – mg995
    accelerometer – adxl335

  22. Abhi

    how to interface gyro and bldc motors with arduino for Quadcopter & from where to get program for it

  23. dhanesh

    I wnt to make my robo straight line.while using compas I got some time it Is showing different value.so I would like to use MPU 6050 gyro…pls tell is it right option to make my robo straight????if it Is provide me arduino code for finding the rotation

    • Coleman Benson

      @dhanesh For more technical questions, it’s best to create a new topic on the RobotShop Forum. We’ll be happy to help.

    • Coleman Benson

      @youssouf There is some simple sample code in the article, but it depends on which sensors you have, as well as which microcontroller (assume Arduino?). If you create a new topic on the RobotShop Forum we would be happy to help.

  24. Boualem

    I’m working on a new project and actually i’m kinda new on this field, so please i really need some help, here it is I need to know how can i connect an accelerometer Altimu-10 v4 to an Arduino MEGA 2560 card and get the 3 values I need (x,y,z) on my screen, Thank you.

    • Coleman Benson

      @Boualem For troubleshooting and technical support, please create a new topic on the RobotShop Forum and we would be happy to help.

  25. ali

    @ Coleman …. i want to integrate multiple IMUs with arduino ..so how should i proceed ?. i want to reduce the drift using multiple sensors

    • Coleman Benson

      @Ali The best way to reduce drift is to choose a really fast microcontroller with high resolution A/D, and minimal extra code so it can read values (almost) as fast as the sensors can provide them. If you need to make calculations etc., use a separate microcontroller. If you can tell us more about your project, perhaps we can offer more insight – create a new topic on the RobotShop Forum.

  26. fardin

    How can I display mma7361 accelerometer on the PC for graphic display. Please help me

    • Coleman Benson

      @fardin the output of that sensor seems to be analog (three pins), so if you are using an Arduino microcontroller as an interface, your code needs to read each of those values and send a serial out command. Your could then use the Arduino IDE’s serial terminal window to see the values.

Leave a Reply

  • (will not be published)