Connect servo motor using python class

Hi, I am facing issues with the write() function. How can I rotate the servo motor? Can anyone help me figure out the error?
// Could not connect to serial port 'Digital pin 9' due to error: "port" must be None or a string, not <class 'pyfirmata.pyfirmata.Pin' //

import pyfirmata
from servomotor_communication import ServoMotorCommunication
from pyfirmata import SERVO


class ServoMotorController(ServoMotorCommunication):
    def __init__(self, serial_port=None, baud_rate=9600, board=None, servo_pin=None):
        super().__init__(serial_port, baud_rate, board, servo_pin)
        self.__angle = None
        self.__servo_pin = None

    def rotate_servo(self,angle):
        if angle < 0 or angle > 180:
            raise ValueError("Angle must be between 0 and 180 degrees.")

        self.__angle = angle
        self.serial_port= pin
        print(self.__servo_pin,self.__angle)
        self.board.digital[serial_port].mode = SERVO
        self.board.digital[serial_port].write(angle)

    def test_servo(self):
        while True:
            print("\t 1: Open the doors")
            print("\t 2: Close the doors")
            choice = input("Enter your choice:")
            if not choice.isdigit():
                print("Must be a valid number")
                continue

            try:
                choice = int(choice)
                if choice == 1:
                    self.rotate_servo(180)
                elif choice == 2:
                    self.rotate_servo(0)
                else:
                    raise ValueError("Invalid choice. Please choose 1 or 2.")

            except ValueError as ex1:
                print(ex1)
                continue



serial_port = '/dev/tty2'
baud_rate = 115200
board = pyfirmata.Arduino(serial_port)
pin = board.get_pin('d:9:s')

start_communication: ServoMotorCommunication = ServoMotorCommunication(serial_port=serial_port, baud_rate=baud_rate,
                                              board=board, servo_pin=pin)
start_control = ServoMotorController()
#message = f"The Servo Motor is connected to {serial_port} on pin {pin} with baud rate of {baud_rate}"
#start_control.logger.log_info(message)
start_control.test_servo()

Hello kamu2423

Welcome to worldbest Arduino forum.

Do you have droped you request in the correct forum, don“t you?

Arduino is using C and C++.

Have a nice day and enjoy coding in C++.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.