Introduction: Angle Measurement Using Gyro, Accelerometer and Arduino

Picture of Angle Measurement Using Gyro, Accelerometer and Arduino

The device is a rough prototype of what will eventually become a self balance robot, this is the second part of the hole thing (read accelerometer and control a motor to self balance). The first part with only the gyro can be found here.
On this instructable we will measure the angle using both gyro and accelerometer, and using a technique to merge both sensor to get a smooth signal. The technique is called "complementary filter".

previous

Step 1: What We Gonna Need:

Picture of What We Gonna Need:

Some part can be replace, and some modification at the code have to be made to fit your hardware.
On this project we will use:
- Breadboard
- Microcontroller, I used the Arduinoboard
- Wire
- Jumper Wires
- Gyroscope XV-8100
- Nunchuck Wii (for the accelerometer)
- Nunchuck Wii adapter for Arduino

Step 2: Building...

Picture of Building...

The circuit consists of a gyroscope connected direct to port 0 at your arduino and a nunchuck wii connect at the I2C port.

Assembling the gyro:

1. - plug the gyro at the breadboard
2. - wire ever thing:
- Vo pin from gyro connected to analog port0 at arduino (Light orange wire)
- G pin from gyro connected to ground (White wire)
- V+ pin from gyro connected to Vdd(3.3V) (Orange wire)

Assembling the accelerometer:

1. - plug the adapter at the nunchuck
2. - plug the nunchuck at the arduino using the adapter
3. - place the accel sensor like the photo above

Step 3: Some Extra Information Before We Proceed

Picture of Some Extra Information Before We Proceed

From both sensor we can measure the angle but using two distinct technique.

To measure the angle using the gyro we have to integrate the signal.
But why we have to do this?
Because the gyro give us the angular rate, so a simple way to get the angle is have the angular rate multiplied by the time [angle = angle + w * dt]

To measure the angle using an accelerometer we have to sense the gravity in each axis of the accelerometer, what it means, the projection of the gravity acceleration on each direction of the sensor give us an idea about the angle. [angle_accel = arctg( Ay / sqrt( Ax^2 + Az^2 ) )]

So why we gonna use two sensor instead one?
To take the advantage of both sensor properties. If you look at the graphic the gyro data continuous increase, this is called drift and the accelerometer data change a lot at a small time.

And how to merge both signal?
We will use a technique called complementary filter. I don't know the exact theory behind this, but it works fine. There are some information at the internet, just google it if you need more information. This link has a lot of information and can be useful.

filtered_angle = HPF*( filtered_angle + w* dt) + LPF*(angle_accel); where HPF + LPF = 1

The values of HPF and LPF can be found at this link, on the filter.pdf file. Thanks you guys from "The DIY Segway".
Just for test purpose we gonna set this values like this, HPF = 0.98 and LPF = 0.02.

Step 4: Code:

The code is a adaptation from a code that I used on another project. Probably there are some variables unused.
I used a library to read the nunchuck data from http://todbot.com/blog/. Thank you Tod E. Kurt.
The comments on code are in portuguese, as soon as I have a free time, I will translate it.

The code output trough the serial line some numbers with this pattern:

dt : w : teta : pitch : filter_teta $
time:angular_velocity:gyro_angle:accel_angle:filtered_angle$

So you can save this values at a serial terminal and plot a graphic or use the angle for other things.

If you need any explanation about the code fell free to ask.

The code is ziped. Just unzipe, open and upload it to your arduino.

Step 5: Testing and Conclusion

Picture of Testing and Conclusion

To test the system I saved the data using a program called Termite, then import this data at excel and plot the graphic to see how good my filter is. The results are amazing. Of course you can use the signal to drive a motor, or other things.

Any comments, any doubt, any information missing, just tell me and I correct it.

Please if you like this, rate.

Thank you all.

Comments

rouis.jihene.20 (author)2017-05-15

Please help me in getting the angle from the gyroscope, i applied the equation metined but i got incorrect results

i write: angleGyro+=(w*dt)*RAD_TO_DEG;

with: w=w=(int)gyro.g.y;

where is the problem??????? please help, it's very urgent

MamunEdt (author)2017-02-21

this is nesessery encoder motor needed for self balancing robot

HichS1 (author)2016-03-22

ok

kwickramasinghe (author)2015-03-08

Hello, First of all great work and thank you very much for sharing the knowledge.

I need to know a couple of things.

1. Are the angle measurements accurate. Does a 45 degree change show 45 degrees approxiamtely.

2. Up to what value of angle the device measure ? Is it capable of handling more than 90 degree angles

mba7 (author)2014-09-21

I made an equivalent tool in python that print real time data from ADXL345 accelerometer.

https://github.com/mba7/SerialPort-RealTime-Data-P...

may be it will be helpful for someone

saurabh.more.92167 (author)2014-08-02

is this possible to use only accelerometer for angle measurement.

It is possible to use only the accelerometer but the result may not to be so good.

It is recommended use both sensors.

ihelmi (author)2014-04-13

Nunchuck controller are use for what?? How can I use this, to combine with my pyranometer tracker?

otaviousp (author)ihelmi2014-04-14

Nunchuck controller has an internal integrated accelerometer.

You can use it for a variety of things, depend what are you doing or your need.

ontreus (author)2012-02-11

Desculpe-me, é que estou construindo um bicoptero e estou com um problema no equlibrio de todo o sistema, estudando seu codigo: angle_for_instructable.pde
Não vi nenhuma linha para a execução dos motores que vc usou no seu robô.
estes comandos estão em um outro arquivo a parte?
Obrigado pela atenção.

jgeidl (author)2011-03-04

The code link does not link to a .rar but to a .tmp file. Is the code link available somewhere else?

Thanks,

Jim

vivekchopra (author)2011-01-27

i need to build a human body sensor using an accelerometer and a gyroscope........and i am gonna use labVIEW as software for analysis.....can you please help me with the microcontroller part........should i go with lpc2138???

thanks

skeleton1102 (author)2011-01-05

Is there any information about how that formula is derived, because I hate using things i dont fully understand

otaviousp (author)skeleton11022011-01-15

Read this instructable first, there is a plenty of information.

https://www.instructables.com/id/Accelerometer-Gyro-Tutorial/

Any doubt feel free to ask.

skeleton1102 (author)otaviousp2011-01-15

I mean the one to calculate the angle using accelerometers.

paolo.mosna (author)2010-11-12

Sorry If i bother you but where can I buy the gyroscope device you used in your realization?

otaviousp (author)paolo.mosna2010-11-12

I bought this gyro at eBay. But is no longer available.

Accentrix (author)2010-05-17

Thanks for the explanation. I was waiting for someone to do this.

WyoJustin (author)2009-11-02

Thanks, I'll need this for my next project.

Justin

otaviousp (author)WyoJustin2009-11-03

Nice, xD

gellis3000 (author)2009-09-14

When I try to download the .rar, all I get is a .tmp file. I have tried 3 different browsers, how does this work?

otaviousp (author)gellis30002009-09-14

Try just renaming from .tmp to .rar

About This Instructable

107,214views

64favorites

License:

More by otaviousp:CNC G-Code Interpreter using ProcessingHow to connect Arduino and RFIDAngle measurement using gyro, accelerometer and Arduino
Add instructable to: