Gyro Controlled Servo (Help Please)

Im trying to make a servo that is controlled by a gyroscope axis.
Here is what I have:
Gyro: Parallax L3G4200D ( )
Servo: Tower Pro Micro Servo (9g)
Arduino Uno (R3)
(I’m using and I2C connection for the gyro)


Servo (+)_____Arduino 5v
Servo (-)_____Arduino GND
Servo (S)_____Arduino D6
Gyro (Gnd)_____Arduino GND
Gyro (VIN)_____Arduino 5v
Gyro (SCL)_____Arduino A5
Gyro (SDA)_____Arduino A4


#include <Wire.h>
#include <Servo.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24

int L3G4200D_Address = 105; //I2C address of the L3G4200D

int x;
int y;
int z;

Servo myservo;  // create servo object to control a servo 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 

void setup()


  delay(1500); //wait for the sensor to be ready 
  myservo.attach(6);  // attaches the servo on pin 6 to the servo object 


void loop() 
  val = y;            // reads the value of the gyro y axis (change if you use other axis) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
 // example servo knob program modified

void getGyroValues(){

  byte xMSB = readRegister(L3G4200D_Address, 0x29);
  byte xLSB = readRegister(L3G4200D_Address, 0x28);
  x = ((xMSB << 8) | xLSB);

  byte yMSB = readRegister(L3G4200D_Address, 0x2B);
  byte yLSB = readRegister(L3G4200D_Address, 0x2A);
  y = ((yMSB << 8) | yLSB);

  byte zMSB = readRegister(L3G4200D_Address, 0x2D);
  byte zLSB = readRegister(L3G4200D_Address, 0x2C);
  z = ((zMSB << 8) | zLSB);

int setupL3G4200D(int scale){
  //From  Jim Lindblom of Sparkfun's code

  // Enable x, y, z and turn off power down:
  writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);

  // If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
  writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);

  // Configure CTRL_REG3 to generate data ready interrupt on INT2
  // No interrupts used on INT1, if you'd like to configure INT1
  // or INT2 otherwise, consult the datasheet:
  writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);

  // CTRL_REG4 controls the full-scale range, among other things:

  if(scale == 250){
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
  }else if(scale == 500){
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);

  // CTRL_REG5 controls high-pass filtering of outputs, use it
  // if you'd like:
  writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);

void writeRegister(int deviceAddress, byte address, byte val) {
    Wire.beginTransmission(deviceAddress); // start transmission to device 
    Wire.write(address);       // send register address
    Wire.write(val);         // send value to write
    Wire.endTransmission();     // end transmission

int readRegister(int deviceAddress, byte address){

    int v;
    Wire.write(address); // register to read

    Wire.requestFrom(deviceAddress, 1); // read a byte

    while(!Wire.available()) {
        // waiting

    v =;
    return v;
 // servo and gyro control using arduino
// gyro program is from bildr (

When I run it the servo just moves to the edge of it"s range.
Any ideas?
Thank you soo muuch!! :slight_smile:

Print out the values that the gyro is returning.

What is the point of this program?

There is no serial, it does not return values.

I am trying to move a laser on the servo with a glove with the gyro mounted on it.

Then use serial, and print out some values.
That is the ONLY way you are going to figure out what is going wrong.

You do know, of course, that gyros return the rate of rotation , and not the angle.

Servo (+)_____Arduino 5v

You should never use the 5V Arduino output to power motors or servos. Doing so will almost always cause problems.