# Arduino and IDG500 gyroscope

Hi,

I’m an Interactive design student and I am making a project, which need a gyroscope to control de rotation of an object. I bought and IDG 500 2 axis in sparkfun:

and I get the code to try it, in the same page. The code is:

int x, y; // X and Y in-plane sensing

void setup()
{
Serial.begin(9600); // sets the serial port to 9600
}

void loop()
{
Serial.print("rotational rates are x, y: “);
Serial.print(x, DEC); // print the rotational rate in the X axis
Serial.print(” "); // prints a space between the numbers
Serial.println(y, DEC); // print the rotational rate in the Y axis
delay(100); // wait 100ms for next reading
}

When I compile it, the data X and Y send me values from 270 up and down depending velocity of how I rotate the gyroscope.

The question is, how can I convert this numbers to degress to work with?

Maybe I need some maths in there and I don’t know where to start.

Hope you can let me a help.

Thanks

which need a gyroscope to control de rotation of an object

Did you mean "de-rotation"? You have to stop rotation?

how can I convert this numbers to degress

A gyro measures rate of change, so you have to integrate to measure "degrees".
Expect some drift.

Sorry, I wanted to say, the rotation.

Imagine that you are holding and spherical object.

I would like to know when you rotate the object, how many degrees are you rotating.

I know, that the gyroscope has a drift, but I don't need a exactly number every time.

The gyro measures degrees per second.
Say the gyro outputs a value indicating 90 degrees per second, but the signal lasts 1/4 of a second - how many degrees have you turned?

I don't understand what do you mean. I found an example on youtube that make what I want to make with my project.

I don't need a graphical enviroment like that. Just that, for example: when I turn 20 degrees to left it prints me "20 - left"

Thanks for your time

I don’t understand what you don’t understand - it is arithmetic, not even bordering on mathematics.

If you turn at a rate of 90 degrees per second for a quarter of a second, how far have you turned?

22.5 degrees?

Correct.
It isn’t rocket surgery, is it?

Ok. I understand this bit. I have being trying things but I don’t get what the gyroscope reads. I made that code, that doesn’t work:
int x, y, xAnt2; // X and Y in-plane sensing
int count =0;
int xpre=0; // previous x

void setup()
{
Serial.begin(9600); // sets the serial port to 9600
}

void loop()
{
x = analogRead(0)-275; // 275 is the constant value it gives so we call it 0

if (x == 0 || x == -1) { // thats to solve the drift
x = 0;
}

// So now what I want is for the positives numbers to add in a counter and the negatives values to deduce from the counter.
if (xpre != x) {
xpre = x;
if (x>0) {
count += 10;
}
if (x < 0) {
count -= 10;
}
}

Serial.print(“rotational rates are x “);
Serial.print(x, DEC);
Serial.print (” count”);
Serial.println (count,DEC); // print the rotational rate in the X axis
delay(100); // wait 100ms for next reading
}

I don’t understand what the gryoscope is reading and how to interpretate it.

To convert to degrees, you need to know the sensitivity of the gyro.
This will be on the datasheet, expressed in millivolts per degree per second or in millivolts per radian per second.
You need to work out what your analogue readings represent.
Usually, one count represents 4.8mV.
From this you can calculate the rate of turn, and then integrate (as you have started to do) to get the actual orientation.
(sorry, I'm posting from my phone, and can't get the datasheet at the moment)

Ok the sensivity is 2.0mV/°/s .

I don't know how to work with that. How to convert this numbers by programming code. I 'm sorry, I am not quite good at it.

Ok, what that means is for every degree per second rotation, the gyro puts out a signal of magnitude 2mV.
so, if you take your earlier example of 90 degrees per second, the gyro outputs 2 x 90=180mV.
If you divide 180 by 4.88, you get 36.
So, for every 90 degrees per second (of 15 rpm), your analogue input will change by 36.

What is the 4.88?

Can you write me a code to start with?

Because I understand what you mean but I don't know where to start.

The 4.88 is the minimum value resolvable by the analogue converter, in mV.see reply#9.
Sorry, I'm posting from memory (and my phone), so no code right now.

OK. I tried to make some code:

``````int x,val;  // X and new Value

void setup() {
Serial.begin(9600);      // sets the serial port to 9600
}

void loop() {
x = analogRead(0)-275;//thats is to get my 0 when starts

if (x== -1 || x == 1) { // to solve the drift
x = 0;
}
//To get the degrees ( I don't know if it's ok)
val = x*2/4.88;
Serial.println (val);
delay(200);
}
``````

That looks a reasonable start, you're subtracting the zero degrees per second offset, but then you're doing integer-only arithmetic.
To maintain some resolution, you should be using floating point, otherwise things end up heading towards zero very quickly.

You have to read the gyro at a reasonable rate.
For now, let's say 100Hz (every 10ms).

So, first attempt, untried, untested, uncompiled (obviously):

``````define GYRO_SENSITIVITY 0.002  // volts / degree / second
#define SAMPLE_RATE      100    // number of gyro samples per second
#define ZERO_OFFSET      270    // no-turn reading from gyro ADC
static float currentAngle = 0.0;

// then for each sample
float volts      = ((analogRead (gyroPin) - ZERO_OFFSET) * 5.0 )) / 1023.0;
float turnRate   = volts / GYRO_SENSITIVITY;
float turnAngle  = turnRate / (float)SAMPLE_RATE;
currentAngle += turnAngle;
``````

Now, there is scope here for some streamlining and simplification, but does the arithmetic make sense?

Thank you for the code but....i never worked in floats, and the code obiusly it doesn't compile.

``````int x,  // X
#define GYRO_SENSITIVITY 0.002;  // volts / degree / second
#define SAMPLE_RATE      100;    // number of gyro samples per second
#define ZERO_OFFSET      275;    // no-turn reading from gyro ADC
static float currentAngle = 0.0;

void setup() {
Serial.begin(9600);      // sets the serial port to 9600
}

void loop() {
// then for each sample
x = analogRead (0);
float volts=((x-ZERO_OFFSET)*5.0)/1023.0;
float turnRate= volts / GYRO_SENSITIVITY;
float turnAngle= turnRate / (float)SAMPLE_RATE;
currentAngle += turnAngle;
Serial.println (currentAngle);
}
``````

the debugger says:

gyro:4: error: expected unqualified-id before 'static'
gyro.cpp: In function 'void loop()':
gyro:13: error: expected `)' before ';' token gyro:13: error: expected `)' before ';' token
gyro:13: error: expected primary-expression before ')' token
gyro:13: error: expected `;' before ')' token
gyro:16: error: 'currentAngle' was not declared in this scope

``````int x,  // X
``````

Start with fixing this line. Comma what?

fixed! X problem. I have anothers…

gyro.cpp: In function ‘void loop()’:
gyro:13: error: expected `)' before ';' token gyro:13: error: expected `)’ before ‘;’ token
gyro:13: error: expected primary-expression before ‘)’ token
gyro:13: error: expected `;’ before ‘)’ token