Julius Brenz Pingol
Julius Brenz Pingol

Reputation: 1

MG 996R Servo Motor Raspberry Pi Pico Angle Control

The MG996R is not working properly im using raspberry pi pico, it works sometimes. But once it work it won't buldge to sweep again what should i do? The connections are:

Brown to GND Orange to VCC (5V) Yellow to GPIO 16 (signal)

import machine
import time

# Set up the PWM pin connected to the servo
servo_pin = machine.Pin(15)  # Change to the pin you are using
servo = machine.PWM(servo_pin)
servo.freq(50)  # Set PWM frequency to 50Hz, standard for servos

# Function to set servo angle
def set_servo_angle(angle):
    # Convert the angle to duty cycle
    min_duty = 1000  # Corresponds to 0 degrees
    max_duty = 9000  # Corresponds to 120 degrees
    duty = int(min_duty + (angle / 120) * (max_duty - min_duty))
    servo.duty_u16(duty)

try:
    while True:
        set_servo_angle(0)    # Move to 0 degrees
        time.sleep(1)
        set_servo_angle(60)   # Move to 60 degrees
        time.sleep(1)
        set_servo_angle(120)  # Move to 120 degrees
        time.sleep(1)
        set_servo_angle(0)    # Move back to 0 degrees
        time.sleep(1)

except KeyboardInterrupt:
    # Cleanup on exit
    servo.deinit()

I've tried everything to spin it off but once i run the code there are times its not rotating and there are times it will rotate what should i do?

PS: How do i make the servo back to its 0 angle? and i dont use any power supply, suggest me if its necessary.

Upvotes: 0

Views: 87

Answers (0)

Related Questions