# Using a gyroscope to move a servo-motor

Hello, I need help implementing a servo-motor to rotate according to a gyroscope. The motor should first start at a pulse width of 1ms, which corresponds to 0 degrees. Then it should rotate from 0 to 180 degree, going from 1ms to 2ms pulse width. I used the ‘yaw’ value obtained from the gyroscope to set the pulse width. For some reason this code made the servo-motor unresponsive to the gyroscope; the motor rotating continuously 360 degrees.

Test.ino (3.14 KB)

For informed help, please read and follow the instructions in the "How to use this forum" post.

Post your code using code tags, and post links to the devices like the servo, and the MPU-6050 module.

In particular please clarify the statement below. Most servos can't do this, they break.

the motor rotating continuously 360 degrees

How do you think this is supposed to work? Explain the relationship between OCR1A and the angle, and what happens if the angle is negative.

``````void set_angle(float angle) {
OCR1A = angle; //change pulsewidth incrementally
}
``````

OCR1A sets the pulse width of the servo-motor, thus changing its position. If the angle is negative it goes in reverse. I attached the link to the MPU-6050 module. Download the zip file and paste it in the Arduino library folder. https://github.com/jarzebski/Arduino-MPU6050

Johann_142: Hello, I need help implementing a servo-motor to rotate according to a gyroscope. The motor should first start at a pulse width of 1ms, which corresponds to 0 degrees. Then it should rotate from [u]0 to 180 degree,[/u] going from 1ms to 2ms pulse width. I used the 'yaw' value obtained from the gyroscope to set the pulse width. For some reason this code made the servo-motor unresponsive to the gyroscope; [u]*the motor rotating continuously 360 degrees. *[/u]

If you need the servo to rotate 0 to 180deg, why are you using a 360Deg servo or a continuous servo? A continuous servo is not controlled the same way as a 0 to 180deg servo. What model Arduino are you using? Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? How are you powering your project?

Is there a reason you are not using the Servo library? Why use an interrupt instead of just polling the IMU?

Can you post a link to specs/data of the servo?

Thanks.. Tom.. :)