Arduino, Gyroscope and Processing

91,585

75

47

Intro: Arduino, Gyroscope and Processing

Hi guys, this is my first attempt to post a project here.

This is an instructable on how to read a gyro sensor and plot the data using processing software at your desktop. I am using gyroscope model XV81-000 and an arduino. The device is a rough prototype of what will eventually become a self balance robot, this is the first part of the hole thing (read accelerometer and control a motor to self balance).

Step 1: What You Need?

You will need:

- Breadboard
- Microcontroller, I used the Arduino board
- Wire
- Jumper Wires
- Gyroscope XV-8100

Step 2: Building

The circuit consists of a gyroscope connected direct to port 0 from your arduino. Some capacitor added to reduce noise from analog value.

1� - plug the gyro at the breadboard
2� - plug a small ceramic capacitor to bypass the DC noise between the ground pin and the vout signal. (optional)
3� - add another capacitor to reduce even more the noise between the ground pin and the vcc pin. (optional)
4� - wire ever thing:
- Vo pin from gyro connected to analog port0 at arduino (Blue wire)
- G pin from gyroconnected to ground (White wire)
- V+ pin from gyro connected to Vdd(3.3V) (Orange wire)

Step 3: Software

To communicate the arduino with the processing I used the standard firmata code, the same found inside the arduino's library. This library automatize the communicate process between the arduino and firmata. So is not necessary any modification at the arduino code. We will gone just edit the processing code according with our desire.

The processing code:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

int ledPin = 13;
float value=0;
int first_try=1;
String stf="";

void setup()
{
size(400,400);
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[1]);
arduino.pinMode(ledPin, Arduino.OUTPUT);

frameRate(60);
delay(500);
}

float sens= 0.512;
float offset= 316 ;
float count= 0;
float valor =0;
float aux1=0,aux2=0;
int[] contador={
0,0,0};
float first_time, time;
float teta=0;

void draw() {
background(150);
fill(250,250,250);
arc(200,200,100,100,0,TWO_PI);
if(first_try==1){
//delay for arduino initialization
delay(2000);
first_try=0;

}

if (mousePressed == true) {
fill(255,0,0);
aux1=0;
aux2=0;
contador[0]=0;
contador[1]=0;
contador[2]=0;
teta=0;
arduino.digitalWrite(ledPin, Arduino.HIGH);
}
else {
fill(0,0,0);
arduino.digitalWrite(ledPin, Arduino.LOW);
}

//####################################################################//
// //
// Gyro //
// Scale Factor 2.5mV/ �/s = 0.512 counts/ �/s //
// Offset 316 counts = 1562mV = 1.562V //
// ADC 4.88 mV/count // 0.2048 count/mV //
// //
//####################################################################//

count =0;
for(int i=0;i<20;i++){

count = count + arduino.analogRead(0);

}

count = count /20;

valor = (count - offset ) / sens;

time=millis()-first_time;
first_time=millis();
teta=teta+valor*time/1000;
if(teta>-1 && teta<1) teta=0; //avoid drift error
PFont font;
font = loadFont("EngraversMT-48.vlw");
stf = str(teta);
textFont(font);
text(stf,150,270,200,200);
println("teta: "+teta+" count: "+count+" time: "+time+" valor: "+valor);
line(200,200,50*cos(radians(teta+270))+200,50*sin(radians(teta+270))+200);

}

Step 4: Turn On

Now that we have all we gonna need, lets uploaded the standard firmata code at arduino. The code is found inside the library folder of your arduino.
The processing code I posted at the previous page. The code initialize the firmata library read the analog port and plot it at your desktop .
The processing code need a little tune and I'm working on it. As soon i fix I will edit the code here. Feel free to edit and improve the code for a better performance, and let me know.

Turn on the arduino, compile the code and you gonna see a circle with a line at middle, wait a few seconds and click at the circle to reset the angle
When you turn the bread board, the line will turn to the same side.

Share

    Recommendations

    • Electronics Tips & Tricks Challenge

      Electronics Tips & Tricks Challenge
    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest

    47 Discussions

    0
    None
    arynasti

    1 year ago

    sory,,

    i m the newbe to student have know build quadkopter with arduino make flight controler, can help me please..

    0
    None
    ontreus

    6 years ago on Introduction

    Blz Otávio
    o Codigo do arduino_gyro.pde
    é o codigo do processing e não do arduino
    Vc poderia corigir.
    Agradeço pelo tutorial.

    1 reply
    0
    None
    ontreus

    7 years ago on Introduction

    Olá Otávio, me chamo ontreus e também me encontro em São Paulo.
    Na busca por informações sobre Giroscópio, cheguei no seu tutorial.
    Bem, possuo os seguintes componentes:
    - Giroscopio0 XV-3500CB PROTOTYPE PCB
    - Arduino Duemilanove
    - Servo Motor
    Estou tentando fazer o mesmo desse link que segue:
    http://www.youtube.com/watch?v=xkVRZC0e8Vk
    Depois de testes verifiquei que este meu sensor possui um sinal muito baixo na porta analógica
    Denominada de ANA0 e verifiquei que você usou dois capacitores para amplificar o sinal.
    Meu sensor possui já uma saída de sinal digital compatível com l2c pois assim o sinal fica melhor
    Pergunto que capacitores você usou em seu projeto e conectado aonde?

    1 reply
    0
    None
    otaviouspontreus

    Reply 7 years ago on Introduction

    Para este seu sensor voce nao precisa dos capacitores.

    O melhor seria usar o I2C para nao ter problemas de interferencia.

    0
    None
    vivekchopra

    7 years ago on Step 3

    i need algorithm of above code for my LABVIEW programming... so plz post it here...

    0
    None
    paolo.mosna

    7 years ago on Introduction

    Dear otaviousp, sorry if I bother you, but I'm trying to reproduce your realization using an Arduino Mega BT (Blue Tooth) 368 and what I get as output visualization is a very unstable angle indicator also with gyro device laying on the table without movements.

    To verify A2D (arduino Analog to Digital) converter I connected analog input 0 to ground. With this configuration the A/D converter is giving me strange values ranging (randomly) from a minimum of 0 to a maximum of 64.
    Is that possible? Do you know something about poor A/D conversion with arduino.
    What kind of dynamic shows your realization?

    Thanks for any comment.

    5 replies
    0
    None
    paolo.mosnaotaviousp

    Reply 7 years ago on Introduction

    Currently I'm using a LPR510AL on a micro board which provides an output voltage which represents the angular acceleration.

    I fixed the A/D conversion problem on arduiono using a resistive partition on AREF pin on arduino board. AREF is now connected to GND with a 10microF, and with a 10K resistor to Vcc (5V). This give me a reference voltage for A/D of 3.77 V.

    Yes, I'm using your sample code.
    But I'm still having problem with angular value computation.

    The angular value is slowly drifting apart when gyro is kept steady
    (no force applied).

    But I have just found out that in your code you have this constraint:

    if(teta>-1 && teta<1) teta=0; //avoid drift error
    teta = teta + ( valor * time ) / 1000;

    to avoid drifting.
    I would like to change it to:

    deltaTeta = ( valor * time ) / 1000; // Angle infinitesimal increment

    if(deltaTeta>-1 && deltaTeta<1) {
    deltaTeta=0; //avoid drift error
    }

    teta = teta + deltaTeta;

    Probably this would avoid spurious variation is steady state.
    But I haven't yet tried this solution.
    I would let you know as soon I get tested with new code.

    Thanks.

    0
    None
    otaviousppaolo.mosna

    Reply 7 years ago on Introduction

    Have you change de offset value?

    Make a simple code to return the value read from your analog port with your gyro steady. The value show often is yout offset. Just replace this value on the code.

    This can help improve the steady problem.

    0
    None
    paolo.mosnaotaviousp

    Reply 7 years ago on Introduction

    Yes correct.
    This is what I'm doing. I'm reading teta, the A/D output (from 0 to 1023) which is the gyro output voltage read with the arduino A/D converter.

    The gyro output from the A/D has a range from 328 to 330 [count] when gyro is in a steady state ( I do not know if this variability is normal in a gyro or not). So I did use value 329 as offset inside the equation used to compute teta.

    This produce, anyway, a drifting value for teta which keeps increasing or decreasing (depending on the value i choose for offset).
    Also tuning the offset value using 0.1 resolution (let's suppose 329.4 instead of 329.0) can improve performance in the sense that I reduce the speed with which teta is increasing (decreasing) when gyro is in steady state but i?m not able to stop it.

    I do not want to bother you too much with this problem.
    Probably I would try with a different gyro with a I2C interface with integrated A/D conversion.

    Thanks for you help.
    Paolo.


    0
    None

    OK I got the solution to my problem.
    The problem was related to the fact that I was querying the arduino's A/D converter to fast, a was not giving enough delay in between two consecutive readAnalog() calls.

    Introducing a delay of at least 50 ms definitively solved my problem.
    By the way I did find a solution to increase speed in A/D conversion on arduino.
    Here are few lines of "code":

    #ifndef cbi
    #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
    #endif
    #ifndef sbi
    #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
    #endif

    #define FASTDAC 1

    #if FASTDAC
    #define DELAY 10 // delay in ms
    #else
    #define DELAY 50
    #endif

    so you can use less than 10 ms as delay time in between A/D calls.
    I tested this with 5 ms delay and A/D conversion worked grate.

    Thanks.

    0
    None
    perfo

    8 years ago on Introduction

    Hello, I'm interested in finding out more about these solid state gyros. My question is:- If you had the device on the worktop and it read say 20 degrees. If you pick it up and drive around the block then put it back on the table in exactely the same place would it still show 20 degrees? what kind of accuracy can you get with these? Lastely where can I get chep ones in the UK to play with? Thanks, and sorry for so many questions...

    1 reply
    0
    None

    ok so this is the code i would need just to get the angle



    float sens= 0.512;
    float offset= 316 ;
    float count= 0;
    float valor =0;
    float first_time, time;
    float teta=0;


    //####################################################################//
    // //
    // Gyro //
    // Scale Factor 2.5mV/ �/s = 0.512 counts/ �/s //
    // Offset 316 counts = 1562mV = 1.562V //
    // ADC 4.88 mV/count // 0.2048 count/mV //
    // //
    //####################################################################//

    count =0;
    for(int i=0;i<20;i++){

    count = count + arduino.analogRead(0);

    }

    count = count /20;

    valor = (count - offset ) / sens;

    time=millis()-first_time;

    first_time=millis();

    teta=teta+valor*time/1000;

    if(teta>-1 && teta<1) teta=0; //avoid drift error

    4 replies
    0
    None

    ...ok just one other thing if I use a gyro with a accelerometer I would need this:

    Angle=HPF*(teta) + LPF*(arctg( Ay / sqrt( Ax^2 + Az^2 ))

    (from your other instructable)

    0
    None

    Yes, but if you gonna use a 6DOF sensor, i think it is better to use the angle from only one direction. That way you have the rotation in on direction off the acellerometer plus the rotation from your gyro. 3 times this configuration.

    Sorry my bad english and worst explanation.