-1

I'm controlling the position of a servo with Python and an Arduino. I type in the angle in python and it sends to the Arduino which then moves the servo to that specific angle. Arduino code:

#include <Servo.h>

Servo servo;

void setup()
{
  servo.attach(9);
  Serial.begin(9600);
}

void loop()
{
  while(Serial.available())
  {
    int pos = Serial.parseInt();
    servo.write(pos); 
  }
}

Python code:

import serial

port = serial.Serial('COM9' ,9600, timeout=1)

while True:
        data = input("Enter servo position: ")
        byte_data = data.encode()
        port.write(byte_data)

Everything works fine for a while and I can control the servo, but then it suddenly gives me this error:

File "C:\Users\User\Projects\p1\venv\lib\site-packages\serial\serialwin32.py", line 317, in write
    raise SerialException("WriteFile failed ({!r})".format(ctypes.WinError()))
serial.serialutil.SerialException: WriteFile failed (PermissionError(13, 'the device does not recognize the command', None, 22))

It happens pretty randomly, and I have no idea why.

2
  • If you're powering the servo from the 5V, I would verify that you're not browning out the board.
    – timemage
    Jun 11 at 21:39
  • Is this a serial interface over USB? (Please add the information to your questions by editing it.) Additionally, please watch the hardware manager what it shows for the connection's hardware. Jun 13 at 6:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.