### Servo gimbal page 2/2

Servo based gimbal project

Help me by sharing this post

## PART 2 - The code

The code is not that complicated. This is how it works. The gimbal moves, the IMU detects the angle change and it changes the PID values. The PID is used to fast react at the angle change. The PID will change the PWM width of the signals connected to the motors in order to keep the same angle. Usually, gimbals have a joystick to change the desired angle. In this case the desired angle will be always 0 till future versions.

Downlaod full code:

``````
/*We know taht the min value of PWM signal is -90 (usingservo.write) and the max is 90. So that
tells us that the PID value can/s oscilate more than -90 and 90 so we constrain those values below*/
if(roll_PID < -90){roll_PID = -90;}
if(roll_PID > 90) {roll_PID = 90; }
if(pitch_PID < -90){pitch_PID = -90;}
if(pitch_PID > 90) {pitch_PID = 90;}
``````

I map the values to a range between -90 and 90 degrees. But you could change that value. Also, my gimbal had a maximum of -/+20 degrees for thr X axis and -/+50 degrees for the y axis. Then we write these values using the analogWrite() function and control the servos.

Go back ←

Help me by sharing this post

Alfawise U30 2.8 inch Touch Screen DIY Desktop 3D Printer - U30 EU Plug Black only \$\$162.89

AFFILATE

Creality3D CR - 10S 3D Printer - EU Plug Upgrade Version Coffee and Black only \$\$352.94

Creality 3D Ender-3 V-slot Prusa I3 DIY 3D Printer Kit 220 x 220 x 250mm Printing Size - EU Plug Black only \$\$153.84

Alfawise U20 Large Scale 2.8 inch Touch Screen DIY 3D Printer - EU - U20 EU Plug Black only \$\$253.39