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

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

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

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

1. #### Noah Zerkin

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

2. #### cbenson

Typo. Corrected and appreciated 🙂

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

4. #### Sigurt

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

5. #### 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 🙂

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

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

8. #### 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!

9. #### 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?????????????

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

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

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

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

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

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

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

17. #### Vinay

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

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

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

• #### Coleman Benson

How would you detect rot using vibration?

19. #### 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?

20. #### mohammed

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?

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

22. #### sameer

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

• #### Coleman Benson

@sameer For technical inquiries, it’s best to contact us via the RobotShop Forum.

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

• #### Coleman Benson

@parshav It is best to contact us via the RobotShop Forum for design related questions.

24. #### Abhi

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

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

26. #### youssouf

how to write a code for accelerometer

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

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

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

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

30. #### Ray Massena

I want to measure the number of jump shots taken in basketball using the accelerometer and the gyro. Is it possible?

• #### Coleman Benson

@Ray Massena You would need to mount a sensor on each player which might make a jump shot, and it can probably be done, but the hard part will be processing the data afterwards.

31. #### Monika Malik

Hi,

I want to connect two ADXL 337Z to the arduino uno. I don’t know how to connect two acclerometer. I had done for one acclerometer.

Thanks

• #### Coleman Benson

@Monika Malik The sensor seems to have three analog outputs for each of the three axes, so you would just connect each axis to its own analog pin on the Arduino, connect VS to 5V and GND to GND.

32. #### Monika Malik

@Coleman Benson For your reply. But I have to connect 3axis ADXL 337Z ,two acclerometer to the Arduino and then compare the data between them. If could help for me.

Thank

• #### Coleman Benson

@Monika Malik For more in-depth technical support, it’s best to create a new topic on the RobotShop Forum here: http://www.robotshop.com/forum/
The more details you can provide about what you have and what you want to do, the better we can help you.

33. #### neha singh

I have bought RKI -1208 servo motor having 33 kg-cm stall torque at 6v, stall current-3.8 A.
I have to use it to rotate some amount of weight.
while the motor is stopped at certain angle, it is very difficult to move the horn- shaft even after applying a lot of force.
But while it is moving (sweep code in arduino library), its very easy to stop the motor with a little force.
I understand speed is indirectly proportional to torque.
when the speed is tooo slow its giving a good amount of torque.
I need a speed of at least 4 rpm to 10 rpm with maximum torque for my application
Also, can i do this with microseconds??
the torque loss might be happening because of the delay.
So, is there any other way to maintain speed without using delay??

• #### Coleman Benson

@neha singh It does seem odd that the servo can be easily stopped when in motion, and we would need to know a lot more about your setup (controller, power supply, mechanics). Can you create a new topic on the RobotShop Forum? If you use the “delay” function then the servo is likely not receiving a signal and as such does not try to maintain a position.

34. #### neha singh

Thank you so much for the reply Mr. coleman…
By servo being stopped, i meant it is stopped by some amount of resistance (in my application) while moving. and it is not able to move against a force. Running torque is almost one-third of the stall torque. Also, my lever arm is of 6 cm. so, all the torque is getting divided by 6. of-course, by decreasing the speed I am able to get full torque. But, I want a certain speed (6 seconds- 120 degree) with more running torque. I am using arduino uno and 6v, 4.5 AH power supply. the system doesn’t have friction because of bearing.

35. #### neha singh

Also, how to create a new topic on the RobotShop Forum?

36. #### neha singh

Thank you so much for the reply Mr. coleman…
By servo being stopped, i meant it is stopped by some amount of resistance (in my application) while moving. and it is not able to move against a force. Running torque is almost one-third of the stall torque. Also, my lever arm is of 6 cm. so, all the torque is getting divided by 6. of-course, by decreasing the speed I am able to get full torque. But, I want a certain speed (6 seconds- 120 degree) with more running torque. I am using arduino uno and 6v, 4.5 AH power supply. the system doesn’t have friction because of bearing.

• #### Coleman Benson

@neha singh We have found the same thing – the “running torque” is less than that of the static torque. The exact fraction is often servo to servo, and the value is never provided by the manufacturer; they only list the maximum stall / static torque. We can only propose that you select a servo which can provide significantly higher static torque than you need and hopefully it can provide enough “dynamic” torque for your application.

37. #### ahasan

Can you please suggest me the way of controling a biped walking using arduino and gyroscope. So far i get to know is that i’ll have continously measure the center of gravity. I’ve never worked with gyro. And i dnt know how to calculate it

• #### Coleman Benson

@ahasan That’s a bit beyond the scope of this tutorial; feel free to contact us via the RobotShop Forum and describe what you want to do and what parts you have or are considering.

38. #### vm

I’m wondering if there is any Ardino board available , which has both the accelerometer and the microcontroller on the same board?

• #### Coleman Benson

@vm Yes, there are several such boards. If you want an “all in one”, you might consider the MWC MultiWii Flight Controller or Quadrino Nano Flight Controller, both available from RobotShop.com

39. #### asf

hi nice tutorial. I have one question tho, if i wish to find the acceleration of a moving car, what kind of accelerometer should i be using? thanks in advance.

• #### Coleman Benson

@asf The choice of accelerometer largely depends on the maximum acceleration you think the car will be subjected to, and also the communication method you prefer (serial, I2C, analog etc). Accelerometers also come in one, two or three axes.

40. #### Akash Mahla

Hello sir, i am trying to control a bldc motor( with hall sensor and 3 phase) with help of Arduino uno and gyroscope(MPU6050) means i want to make the motor run in frwrd direction when gyroscope is tilted in one direction, and make the motor run in opposite direction when gyroscope is tilted in backward direction, can it be done?? if so how?? please help, motor is 24volt brushless dc motor.
Thank you

• #### Coleman Benson

@Akash Mahla Are you looking to make a two wheel balancing robot?

41. #### Akash Mahla

First of all, thank you so much for replying to my comments 😉
Yes Sir, what we are making is kind of similar to two wheel balancing robot, only difference is that, in two wheel balancing robot we use to control two dc motors with single arduino and single gyroscope,BUT in this project we wish to control only a single motor, with single gyroscope and arduino, but the problem is that our motor is not a normal DC motor, it is 3 phase brushless dc motor. What we can’t figure out is that how to control this motor, means we can control it in similar way as we control motors of a quadcopter, but motors of a quadcopter run only in one direction, we don’t know how to change the direction of the 3 phase brushless motor, when gyrsoscope is tilted in opposite direction.
thank you sir

• #### Coleman Benson

You need an ESC which can rotate the motor in two (rather than just one) direction. Gyro -> Microcontroller -> ESC -> BLDC motor

42. #### Akash Mahla

Hmm….thank you so much sir, we would try our best.

43. #### Sherri P.

I can not get the accelerometer (arduino lily pad) to change numbers when I run the program

• #### Sébastien Parent-Charette

@Sherri P.: Please make a new forum post here for technical help. Make sure to include as many details as possible (setup, code attachments, pictures, etc.) to help determine the issue.

44. #### Ketan

Sir , we have been making a body wear device which should monitor the change in position of body movement . so which sensors should we use and how to monitor 3D maping of body positions.

• #### Coleman Benson

@Ketan Since this will likely require quite a bit of back and forth communication, we suggest creating a new topic on the RobotShop Forum here: http://www.robotshop.com/forum/
Try to provide as many details as possible (indoor vs outdoor, what you want to track and why etc).

45. #### Arjav

Hey. I am working on self balancing robot. I am having trouble working with PID on MPU6050 and arduino uno. Can you please help me out?

• #### Coleman Benson

@Arjav Unfortunately we cannot provide sample code, but a quick search online should give you some 2WD balancing, Arduino-based projects where the creator put the sample code online.

46. #### Arjav

@Coleman. I tried searching that online. bt was unssuccessful

• #### Coleman Benson

@Arjav Look up “Arduino balancing robot code” in Google.

47. #### Nicolas Gonzalez

Hi, I’m looking to get the position (inclination angle) of the three axes through the gyroscope. It’s posible? or should I use an IMU?

• #### Coleman Benson

@Nicolas Gonzalez A gyro won’t be able to give you absolute angles, just an estimate on relative angles. You would need an accelerometer.

48. #### asdf

Hi! Is it possible to connect an acceleration sensor (MMA8452Q) and an atmospheric pressure sensor (BMP180 Barometric Pressure Sensor) to an Arduino Uno via a breadboard simultaneously? If so, how could I go about that?

• #### Coleman Benson

@asdf Check which pins are required by each sensor and see if there is overlap. Bo the accelerometer and the barometric sensor seem to be I2C; I2C devices are meant to be connected in series, so you would use the same pins for each: https://en.wikipedia.org/wiki/I%C2%B2C

49. #### asdf

Thank you! Yes, they are both I2C device. However, I have found that a breadboard is unsuitable for the experiment I have in mind as the connections might be too loose. Is there a way to directly connect both sensors to the same ports on my microcontroller securely?

• #### Coleman Benson

@asdf If you want a permanent fix, you can solder the connections. If your microcontroller only has pin headers, you might find one which has solder pads instead.

50. #### Yue

Hi, Mr Benson
Could you please let me know how you determine the number (67.584) when you do the gyro value conversion ? From data sheet ?
Thanks!

• #### Coleman Benson

@Yue Where do you see the value of 67.584?

51. #### Yue

Hi, Mr Benson
Thank you for your reply. In the code: Gx=Xread/67.584. I guess that is the gyro scale number. Could you please let me know how you determine the number ?
Thanks !

• #### Coleman Benson

@Yue That seems like the specs for a particular sensor / product. Yo would really need to understand the sensor in order to know what that value represents. It’s a bit beyond the scope of this article.

52. #### Hassaan

hey! i’m working with accelerometer mma 7361 as it’s an analog sensor, i want to know is there anyway i could display my accelerometer output values graphically like GUI apart from IDE serial monitor, and secondly i want to know what is the advantage of I2C and digital accelerometer compared to analog ones?

• #### Coleman Benson

@Hassaan You will need to connect it to a microcontroller which is then connected to a computer (or LCD). You would need to find or create an application to get the data. You might consider the FlowBotics App for Phidgets 8/8/8 or 2/2/2: http://www.robotshop.com/en/flowbotics-apps.html In regard to I2C vs digital vs analog, it really depends on your setup and what the device is connected to (microcontroller specs). In any situation, you likely want a setup which can handle a very high refresh rate to minimize data loss.

53. #### Hend

Hi Coleman Benson, I have a question. I have an IMU 9DOF and i want to use it in telling if the object is stationary or moving? how is that possible?

• #### Coleman Benson

@Hend When you say “want to tell”, can you explain more? Do you want a human to know? A computer? If you can provide a bit more information about your project / application it would help us help you.

54. #### Hend

Basically my project is about IMU sensor connected with a Arduino, a person must carry the sensor and walk. Then a data will be sent to the computer every 20 ms, the data contains values of accelerometer and gyroscope. I have a matlab code that takes the data from this sensor and plots the motion of the person, the question is now how does the code be able to tell if the person is moving or in stationary status from the sent data?
Thank you very much.

• #### Coleman Benson

@Hend That will come down to data analysis; perhaps set a minimum threshold value below which you can assume the subject stopped moving.

Hi please how I can using gyroscope to control on three servo motor I tried to do that.

• #### Coleman Benson

@murtadaabd87 You need a microcontroller to read the values of the sensor, then either send the appropriate pulses directly to the servo motors, or via a servo motor controller. It all comes down to the code on the microcontroller.

56. #### Dim

Hello.
I need to control a 9DOF sensor with a gyroscope, an accelerometer and a magnetometer, in order to eliminate the deviation issues of my autonomous vehicle.
I was wondering if there is any useful advice you have to suggest
Thank you.

57. #### Aaron

Just brought a adxl326. Some odd reason I can not get any horizontal data on X any y. But only when it tilts. Example. Want to measure the g force and acceleration of a drag car.

• #### Coleman Benson

@Aaron Each axis has its own pin, and your code needs to read each axis.

58. #### amir

I have used accelerometer Adxl345.I have collected the outputs in x,y,and z.but my requirement is to measure the position of that car .what I have to do.and what is the programming code that I must use to get my requirement.

• #### Coleman Benson

@amir You need to integrate the values to get velocity, then position. Note that there is drift (the sensor cannot provide every subtle change fast enough, and the microcontroller will miss some values), so the position will become less accurate over time. If the application is outdoors, a GPS module is best.

59. #### yogesh

light blue bean app when connected to bean, the accelerator values are continuously changing even i am not changing the position of bean. how to make the values constant??

• #### Coleman Benson

@yogesh This requires some troubleshooting which we cannot do in the comments section. Please create a new topic on the RobotShop Forum under “Robot Parts -> Sensors” and provide as much information as possible.

60. #### saikumar

can i get a programe for arduino mega of robot controlled by using bluetooth modulue..???

• #### Coleman Benson

@saikumar Unfortunately creating custom code is beyond the scope of this article. We suggest creating a new topic on the Arduino Forum.

61. #### mark bertollo

Im going to be making sort of a self balancing robot with arduino uno.. What gyro would i use.. and do i also need and accelerometer… where would I find code to program… Thanks

• #### Coleman Benson

@mark bertollo An accelerometer will give you a good idea of if your robot is vertical, whereas a gyro will tell you if it’s rotating / falling. You likely only need low acceleration values for each (2g for the accelerometer for example and 300 deg/s2 for the gyro). We do not have any specific sample code to suggest since it will depend entirely on the parts you choose.

62. #### christy

I want to read data from an IMU and display it,could anyone help me how to do it?

• #### Coleman Benson

@christy There are many ways to do this – have you already selected the parts? You need to know how the IMU communicates (analog, I2C etc), as well as the LCD (digital, serial, I2C etc).

63. #### scott

Hi, im trying to create an electronic bike alarm which is opened by my mobile phone via bluetooth. if the lock is cut this will trigger a loudspeaker and activate a gsm which will send a sms to the owners phone. aswell as just activating if the cable is cut, i want it to activate using an accelerometer if the lock is shaken in a attempted bike theft. is this all possible via one arduino board and one massive code? ive got the lock working with my mobile so far via bluetooth. any help would be much appreciated.

• #### Coleman Benson

@scott Certainly! We hope you post your project on Let’s Make Robots.

64. #### sumeet

how to calculate speed using gyro?

• #### Coleman Benson

@sumeet A gyro can provide angular acceleration, which can be integrated into angular velocity, which can then be integrated into an angle. If you want linear velocity, you need an accelerometer.

65. #### Dedrick

I’m trying to use one of the ADXLXXX (16g and 200g ) accelerometers with an Arduino. Is it necessary to place LPFs on the X,Y, and Z outputs?

• #### slatour

@Dedrick: It is not mandatory but greatly helps get a more representative reading and also remove electrical noise.

66. #### Dave

Hi, is it possible to use the arduino, with a gyro, to make a wearable device that measures the different angles of rotation at the wrist of a human arm?

• #### Coleman Benson

@Dave Certainly. However, you will need to factor in drift and suggest an IMU rather than just a gyro.

67. #### sharat

@Coleman, can put up a program to get raw values from IMU MPU6050

• #### Coleman Benson

@sharat Unfortunately creating custom code is beyond the scope of this article.

68. #### Zila

I have to make a stair climbing robot.The problem is after 3 step of stair the robot need to go straight until it found the next step of stair.I need the robot to have straight path from the first step of stair it climb until the end.Can I used gyrosensor with arduino uno to make sure the robot will always have straight path no matter what disturbance it face.thanks in advance =)

• #### Coleman Benson

@Zila There are not many commercial stair climbing robots available to base your design off of. It’s really up to you to come up with a design which works.

69. #### BHANU

Any suggestions to transmit and receive wirelessly the readings of the sensor(accelerometer and gyroscope) ?

• #### Coleman Benson

@BHANU You need the sensor, connected to a microcontroller, and the microcontroller connected to a wireless device.

70. #### sana

which among the three will b the best choice for hand gesture controlled robot and why?

• #### Coleman Benson

@sana an IMU is a good choice, but it all depends on what you want to do and why.

71. #### Agustin Ramon Ruckstuhl

Excelent website, I was using an IMU to start with after reaching the state in which the drone responds to every command I send from far away.
From what you say Mr. Benson, is that if I want to control orientation and correct for pitch, roll and yaw movements on my quad, I should start woth a 3AXIS gyro and a regular compass.? If such, can you suggest any?

72. #### SHANTANU BARAI

My robot is moving in an arena. I need the location of my robot basically x and y co-ordinates..(x,y). Can i get that using IMU?

• #### Coleman Benson

@SHANTANU BARAI Only for a certain time, because you will have drift. You may want to use distance sensors.

73. #### Connor

I am using a 3-axis analog accelerometer to measure wave displacement at sea, how would the code be double integrated so it outputs displacement instead of acceleration ?

• #### Coleman Benson

@Connor Given a known initial location, you would use integration (continuously). As you can see, unless you are able to record and integrate all values at all times, your net displacement will drift over time. You can use it to get a good idea of relative displacement where acceleration is zero. You would likely also need a gyro, and if you are talking open ocean, an accurate GPS is often best.

74. #### Abhishek

Very informative tutorial.
But I have doubt, which interfacing are we using to connect Arduino UNO R3 with ADXL335.
Is it SPI or I2c or Serial(UART)?

• #### Simon Latour

@Abhishek The ADCL335 chip has 3 analog outputs, so it is not spi, i2c or UART. You need to use 3 of the analog ports of the Uno R3.

75. #### Maruf

Typo: 1 rotation per minute does not equal to 360deg/s as implied within the parenthesis.

• #### Sébastien Parent-Charette

@Maruf: Thank you for the correction. We have updated the article accordingly! 🙂

76. #### Hamid

Hello, I’m trying to use a gyro/ accelerometer sensor and my software so far is on vba, so my question is. Would it be possible to feed the data into excel or run the sensor on vba?

• #### Coleman Benson

@Hamid Yes, but that’s entirely on the software side; your interface board / adapter which acts as intermediate between the sensors and your computer is outputting the sensor data in a specific format. You then need to create a custom code (in whichever programming language you are familiar with) to read these values, and output them in a format which can be imported into EXCEL (for example comma separated data). If you’re using Arduino, you can use he serial monitor and then copy/paste (or special import) the data.

77. #### Marko

Hey Cole. I am working on a project that involves three points on the same plane, and how they change as the plane is moving/rotating. I am assuming that I would connect a 6dof IMU on each point and feed it back to the arduino in real time. That way I can measure how much each point has moved since its initial position, and where the plane is now. Does this sound correct to you? Secondly, do you have an IMU or Arduino that you would suggest for my use? Thanks!

• #### Coleman Benson

@Marko Are the points on the plane fixed? If so, then you really only need one IMU. If they can each move, then yes, you need one for each point. Note that IMUs are not good for absolute positioning. To get a better idea of what you want to do, can you create a new topic on the RobotShop forum and provide a few drawings of what you want to do? It’s not easy with text only here as comments.

78. #### matin

I am using LSM9DS1 connecting with unity. I need to calculate velocity and distance from accelerometer from arduino. My problem is that I don’t know what should I put for delta Time in the formula such as velocity = ac. * delta_T

• #### Coleman Benson

@matin Ideally you would use the Arduino’s timer to get the time. The faster the better.

79. #### Agustín Ramón Ruckstuhl

Hi Mr. Benson. Thanks for your previous answer. If I have a 2 axis gyro, is it safe to assume that, if programmed correctly the quad will stabilize along x and y axis when the pitch/Roll joystick is released? Thank you very much for your time. Best Regards

• #### Coleman Benson

@Agustín Ramón Ruckstuhl No; you need an accelerometer to stabilize with respect to ground. A gyro only tells you rate of rotation, but that is only relative. An accelerometer gives you absolute angle with respect to ground, though even that cannot account for wind.

80. #### mei

How can we obtain linear acceleration (due to motion alone) from accelerometer? Because accelerometer gives acceleration of motion + gravity, right?

• #### Coleman Benson

@mei The sensor will always provide the force of gravity, but you can (fairly) easily remove this using equations and simply obtain the raw three axis linear acceleration values.

81. #### Rameel Khan

I am using ADXL335.
My query is, how to make a ROBOT turn using the values obtained by accelerometer’s ouputget the values. Is there any matematical equation available?
I also dont know what exactly the output values of accelerometer indicates.

• #### Coleman Benson

@Rameel Khan The output is analog, so you need a microcontroller which accepts 0-5V analog via ADC pins. The microcontroller would then be programmed to convert the analog values to acceleration values. It would then be up to you what to do with these values and how you want your robot to “react”.

82. #### Popi_chan

im trying to make a glove that can identify finger movements of deef mute people and convert them into words .the convertion can be done by myself i want to know how to get accelometer reading correctly.can you help?

• #### Coleman Benson

@Popi_chan We recently saw a project about this. You need to interface the sensors with a microcontroller and create the appropriate code.

83. #### Jerwin Prabu

I am using GY-521 MPU 6050. I have created a c++program for my Robot using qt, Now I have added a condition for Gyro, but I would like to add more condition for Accelerometer, magneto meter. If possible to add Accelero, magneto condition with gyro or else need to create separate header and source file for that ?

• #### Coleman Benson

@Jerwin Prabu You can use only one “header” depending on the complexity of the code, if you use libraries etc.

84. #### Jerwin Prabu

@ Benson. I have decided to use only gyro for my robot motion, but If possible to make my Robot straight movement using gyro ? I have used encoder condition for straight motion that is working, but I want to make my robot exact straight what can I do for that ?

• #### Coleman Benson

@Jerwin Prabu No; a gyro only measures the rate of angular acceleration. You need a magnetic compass.

85. #### fahin

hello!!
i am using mg996r servo and mpu6050 gy521 sensor but servo not moving…….!!
for servo i have used external power but still not working!! what should i do? is this because of high torque servo or programming???

• #### Coleman Benson

@fahin Unfortunately the comments section of a blog is not the best place to troubleshoot issues. We suggest you create a new post on the RobotShop or Let’s Make Robots forum. Be sure to provide many details about the setup and code.

86. #### miranda

I am using aurdino 101 with zepher operating system.Both accelerometer and gyroscope are inbuild sensor,but when i am trying to run the sample sensor code of zepher,its not working.Can anyone help me to find the issue and make the code run.

• #### Coleman Benson

@miranda Zephyr is unfortunately beyond the scope of this tutorial.

87. #### Sid

Hello
I am using arduino uno R3 model and an accelerator … And later I want that output from the accelerator to a GSM 900 module I have both of the source code but can u give an other code such that both of the two interfaces are within a code and such that I would get the output from accelerator to the gsm module?

• #### Coleman Benson

@Sid You mean an accelerometer? Unfortunately it’s not the objective of this article to help provide custom code, but one line would be reading the accelerometer values, another to convert them to serial, and a third to send that serial string via the GSM module. There should be some sample code for both the accelerometer and the GMS module – it’s a matter of learning how to program in Arduino and merging the two. There is no shortcut.

88. #### Meseret

Hi I wanted to connect a mma8451 acceleroneter with a arduino micro how do I do that Thank you

• #### Coleman Benson

@Meseret The mma8451 communicates via I2C, so you need to connect GND to GND, Vin to 5V, and the SCL and SDA pins (The latest version of the Arduino Uno has all four pins broken out next to D13, the previous versions have these as specific analog pins).

89. #### Joel

Hi,i have a project and i would want to know if can i use a IMU for wearable device that detect if the waist of a person is moving in a range of angles of rotation ?

90. #### Durga Turaga

Hi, I am helping my son with a science project. Using sensors on his arms and bow we want to measure the angles and rotations of his various shots to compare. Is it possible to connect 5 mpu 6050 sensors to an arduino mega and record the various positions and angles? Thanks

91. #### Ekrem

Hello. I want to draw a path with using gyroscope+accelerometers. My mobile robot will move, i take the values from microcontroller and draw a path which robot went. Anyone can help? Thank you.

• #### Coleman Benson

@Ekrem You would ideally need encoders. A gyro would not be very useful, and neither would an accelerometer, unless you wanted to compare values or use a filter. A digital compass would help.

92. #### ARULPRASATH K

Coleman Benson- First of all your are doing great job congrats for that, i want to calculate the position of a object which in-belted with acclerometer and gyroscope and magnetometer with more and more accuracy for example if have 10mx10m area, my robot are inside this site, now i want know the accurate position of my object (robot)
Note: if suppose the error may occurs, it should be less than 0.5cm. i’m looking for more accurate sensors acclerometer, gyroscope and magnetometer which available in resent days (2016)

• #### Coleman Benson

@ARULPRASATH K The “most accurate” would not be hobby grade and be quite expensive. You essentially want the least “drift” possible. The best solution would be to place fixed sensors around the room and track the location of the object. Ex Indoor Navigation Systems: http://www.robotshop.com/en/gps-modules-gps.html

93. #### kweli

Hello. Im failing to find gyroscope libraries on proteus. How can I represent and code a gyro on proteus for simulation

• #### Coleman Benson

@kweli Unfortunately that is beyond the scope of this article.

94. #### TULASI

sir,i am going to develop a wearable device,which should measure body moments and body positions.which sensor is best either accelerometer or gyroscope or both?which gives most accurate values??kindly mention that sensor specifications which suites for Arduino.

• #### Coleman Benson

@TULASI You would need both; a gyroscope only provides relative angular acceleration. Choose an IMU with around the maximum ‘G’ rating you need; it should likely be 2g or lower, and fairly low angular acceleration (<300 degrees /s^2)

95. #### Himanshu Sharma

Hello Coleman ,
I would like to use IMU MTi G 710, integrated with GPS unit. I want to place this in a van with antenna on the top. Is there any particular technique to be followed for the placement (where to place) of IMU inside the van for correct measurement.

• #### Coleman Benson

@Himanshu Sharma You indicate The GPS unit is integrated, so you need to place it in an unobstructed location – this would likely mean on top of the roof of the vehicle. Note that even cell phones often have issues obtaining GPS lock within a car. Towards the bottom of the window (i.e. the least metal around the unit) may also be an option, though not as good.

96. #### vanessa

hello sir. can you help us to find the angular displacement using triaxis sensor? thankyouu

• #### Coleman Benson

@vanessa If you have a gyroscope you would need to do a double integration. Note that if you also have an accelerometer (three axis), then you can get absolute angles.

97. #### evanz

hello sir, do u have an email account.. i seriously need your advice about our project. thank you.

• #### Coleman Benson

@evanz Unfortunately personal e-mails are not possible, but you can post your question on the RobotShop Forum.

98. #### vanessa

Goodevening sir. 🙂 i’m about to get the displacement taken by the person by giving triaxis sensor the value of x, y and angle. just like the use of pedometer, but the only difference is that in any direction.. can you send me ur e-mail address sir? i badly need your advice .. it was so hard to explain it here. i tried many times to know you e-add but i can’t find anything.. thankyou.

• #### Coleman Benson

@vanessa Unfortunately we cannot provide individual e-mails, but you can post your question on the RobotShop Forum.

99. #### Ruben Zilzer

hi, great article;
I an building a device arduino and mpu6050 that tell the computer yaw pitch roll of tthe device itself. (it works nice, only some drift) but i need to sense and tell the computer “i’ve moved right or left (sway?)or front /backward) like a computer mouse

• #### Coleman Benson

@Ruben Zilzer The accelerometer component would measure linear acceleration, and you can integrate this twice to get position (but drift will build up). If not, a mouse would be a great basis for the technology needed (encoder wheel or optical).

100. #### Arisha

Hi there,

I’m currently doing my FYP, developing a wearable device to measure posture deviation. I’ve chosen to use either IMU – 6DOF LSM6DS3 or ITG3200-ADXL345. I’m new to microcontrollers, hence I would like to ask what kind of specs I should look out for when choosing an Arduino? If possible, could you recommend me the arduino that I should be looking at?

Thanks.

• #### Coleman Benson

@Arisha Both the LSM6DS3 and the ADXL345 seem to have an I2C output, so most Arduino boards have I2C. The question then becomes the speed of the main processor in order to ensure all values are obtain as close to real time as possible. For most wearable applications though, high accuracy is not needed, so a normal Arduino like the Uno would work well.

101. #### Mark

i want to read the speed of my RC car using arduino and any sensor ? what do u suggest for me to use ?

• #### Coleman Benson

@Mark If you use your RC car outdoors, you can use a GPS to get decent measurement. If you want instantaneous speed, you can use a three axis accelerometer. Another option of course would be to use encoders.

102. #### Alaa

Hi , I have BNO055 IMU sensor. how I can find the 3d position by Arduino?

• #### Coleman Benson

@Alaa Adafrtui provide a unified sensor library (for Arduino) with the BNO055 included. This should help save you quite a bit of time programming.

103. #### Andrews

Sir , can you explain how to read translational velocity from raw data accelerometer ?

• #### Coleman Benson

@Andrews You need to integrate once to get velocity from acceleration, and integrate again to get position from velocity.

104. #### Wei Hong

Hi Benson, I am currently doing a Hand Gesture Drone Project. As for now, i am able to do the roll & pitch by titling my wearable device to control the drone using the accelerometer. However, for the yaw, how do i use the magnetometer or gyroscope to implement it on my wearable device as the magnetometer reading is always based on true north and the gyroscope is based on the angular rate of change. If i yaw my wearable device to left or right facing different direction, it won’t be able to work.

105. #### Nishith Kotak

Hi,
I want to use this sensor i.e. MPU6050 to determine the basic strokes of characters like vertical, horizontal left curve, right curve, back slash and forward slash. This is been recognise for the purpose of human character recognition. Can you guide whether this concept is useful for my purpose or how shall i proceed to serve my purpose. Thanks in advance for your response.

• #### Coleman Benson

@Nishith Kotak To confirm, you want to mount the sensor on a pen / pencil to track motion to determine what the character is? This might be possible, but would take a LOT of programming to filter out unwanted positives.

106. #### Balaji V

hello, can you help me the with code for finding the displacement using accelerometer adxl335 with arduino uno board.?

• #### Coleman Benson

@Balaji V There is actually quite a bit of sample code online – search for “ADXL335 Arduino” in Google.

107. #### piyush jain

how do i compute pure acceleration form accelerometer & rate gyro can you give me the code

• #### Coleman Benson

@piyush jain It depends on the type of accel / gyro and the input. Normally the output is described in the chip’s datasheet, as well as how to convert to acceleration.

108. #### Balaji V

what is the unit of the data that displays in serial monitor, like 335 286……?
when coding is done for linear aceleration

• #### Coleman Benson

@Balaji V It depends on the accelerometer you choose; some are analog, others are I2C etc.

109. #### IG

How do you connect an arduino to an accelerometer?

• #### Coleman Benson

@IG That depends on the communication of the accelerometer you choose – it might be analog (connects to analog pins), I2C (connects to I2C pins) or serial.

110. #### bhavuk

i am getting the following results when my sensor is at rest:
AcX = 436 | AcY = -64 | AcZ = 14928 | Tmp = 27.92 | GyX = -555 | GyY = 306 | GyZ = -53
And when i am rotating my sensor, GyX, GyY or GyZ change for a few secs then they are back to normal. Is it suppose to happen? and what is the unit of the following values?
Further can I compute the displacement from these values?

• #### Coleman Benson

@bhavuk An accelerometer measures linear acceleration, including the force of gravity. The gyro component measures angular acceleration. You need to read through the sensor’s datasheet to know what the proper range of values should be.

111. #### Conner

Hello, I am trying to use an accelerometer to light up some lights. after I get to a certain speed I would like to have the lights light up and then when I go under that speed I would like the lights to shut off. Would that be possible to do with an accelerometer and an arduino?

• #### Coleman Benson

@Conner Sort of – an accelerometer measures linear acceleration, so you would need to integrate in order to get velocity. There will be drift, so you will ideally need some other sensors to help zero it.

112. #### Khoa Tran

Hello , I’m trying to use an gyroscope to detect the rotation direction (clockwise or counterclockwise or the device is stay still). Is this possible ? Thank you.

• #### Coleman Benson

@Khoa Tran Yes and no – it will tell you the direction of angular acceleration. However, accelerating on in direction might not mean it is rotating in that direction (but simply might be decelerating). A quadrature encoder should work well.

113. #### Jihene

Please, can someone help me: i have a gyroscope and i want to get the angle from it? how can i do it’s very urgent, please help me

• #### Brahim Daouas

@Jihene As explained in the tutorial, a gyroscope provides the rate of change of the angular position over time which gives you the angular velocity. However, you can obtain the angular position by integrating the angular velocity. This tutorial will provide you further explanation about this and an Arduino sample code.

114. #### fauzie

what is the unit of the value recorded by the accelerometer adxl335? i hope you to help me

• #### Sébastien Parent-Charette

@fauzie: The ADXL335 provides 3 analog outputs that are proportional to the acceleration detected (example product). According to the ADXL335 datasheet, 0 g is represented by half of the voltage source.

115. #### Dhirendra Meher

Which micro controller should I use for 40v 45 amp motor

116. #### Erick Rojas

is there a way that a MPU 6050 can activate a servo motor at a certain z height?

• #### Coleman Benson

@Erick Rojas Not directly – you need a microcontroller as well and program it accordingly. Note too that the height will be relative as opposed to absolute since the accelerometer only measures acceleration, not absolute position.

117. #### Derek

@Coleman Benson. Could you point me in the right direction to start for the following project that I am thinking about completing. I’m only a very basic amateur in electronics and programming. I’m hoping to build a cube with a different colour on each side, I’m hoping to use an accelerometer, RFduino and a transmitter and receiver to send/receiver the data which will activate a vibration motor in the receiver (or an iphone) a number of times depending on which colour is facing up. Can you help?

• #### Coleman Benson

@Derek Based on what you describe, you actually seem to know what you’re doing. You need the Arduino, transceiver, 3-axis accelerometer, battery pack, vibrating motor and LEDs. The accelerometer will tell you which one is facing “up”. It’s up to you how you want to arrange the LEDs or choose an EL panel. For more info, please create a new topic on the RobotShop Forum.

118. #### Ben Sockono

@Coleman Benson I am undertaking project about a smart boxing glove. The boxing glove is to measure the strength and speed of each punch during a fight and to keep track of number of punches thrown in real time and be display on a mini screen. Or there should be a display when there is a contact with the glove, could you please point me in the right direction as what material to use.

• #### Coleman Benson

@Ben Sockono Since the question is more design related, we invite you to create a new topic on the RobotShop Forum (best not to have a lot of comments related to a specific technical question here).

119. #### syafiq

i would like to know more about the implementation and basic concept about accelerometer inside uav

• #### Coleman Benson

@syafiq How can we help? There are many articles online about the inner workings of MEMS accelerometers / gyros. In a UAV, they help by sensing angular acceleration and linear acceleration.

120. #### kbk

Which sensor should i use for measuring vibration ? Accelerometer or gyro or imu ?

• #### Coleman Benson

@kbk If you want to know the amplitude of the vibrations, an IMU is best, and does not cost much more than an accelerometer / gyro.

121. #### avinas

i want to build a rocket deployment system using gyroscope to trigger servo at a specific angle. can you please help me

• #### Coleman Benson

@vinas Gyros measure angular acceleration rather than absolute angles. You should use an accelerometer which can measure the gravitational pull of the earth, and as such give you an absolute angle. Most applications involving rockets use both an accelerometer and a gyroscope (called an inertial measurement unit, or IMU).

122. #### brookez

I’m trying to use an IMU with my arduino uno to measure location and be able to detect a fall. We have parameters that are fall detectors of a person and are trying to input it into our imu code that we got from the arduino site. we are then trying to translate this program to activate a solenoid. is this all possible?

• #### Coleman Benson

@brookez Certainly. Note that the solenoid might consume too much current than the Arduino’s IO pins can provide – consider using a relay.

123. #### Nufail

@Coleman Benson Mr Benson, can you give me an idea on how to calculate wave velocity, the hardware needed and the connection? Right now i am doing a project to protect the shore’s of an island using a buoy. My idea is to use accelerometer to calculate the speed of the wave. Right now i have an Arduino Uno, GY-621 accelerometer, data telemetry and OSD. Thank you in advance

• #### Coleman Benson

@Nufail The issue is that, as far as I understand, an object on a wave does not necessarily travel with it at the same velocity. You might consider two “sensor rigs” spaced apart but in contact with each other (they know their distance relative to each other using GPS and can wirelessly communicate with each other) and able to measure the time it takes for waves to raise and lower them. After that, you’ll need to research the formulas needed to calculate velocity of waves based on distance and height etc.