Python and Arduino issues
Question: I’m having some issues, trying to control a servo connected to a arduino board
from python. In the program I write a value 0 – 180 and send it to the
Arduino, the Arduino should than turn the servo to the selected place. The
problem is that it’s seams that the data sent from python is not read
correctly. (or written correctly). After much googling, trying and failing I’m
still having the same issue. When sending a data from python, te servo moves
from start position, to almost centre and than back to starting point,

I now changed the code so the Arduino now reply, back to python, the data it
receive, and I don’t understand what’s going on. I enter the value 1. And the
Arduino reply with : b’5. If I write 1. The Arduino reply with b’2. .. If I
write 2, it respond with b’5, and the same if I write 5 :S(and it's not always
the same)

The Python code i Use:

    
    
    import serial
    
    def sendSerialData():
      global set_ser, run
      set_ser = serial.Serial()
      set_ser.port="COM4"
      set_ser.baudrate=9600
    
      print('**********************')
      print('* Serial comunicator *')
      print('**********************\n')
      run = 0
      while run==0:
        print('type \'open\'to open serial port. \ntype \'close\' anyplace in the procram to close serial port:')
        openSerial = input(': ').lower()
        if (openSerial == "open"):
          set_ser.close()
          set_ser.open()
          while set_ser.isOpen():
            output = input('\nType what you want to send, hit enter\n:  ')
            set_ser.write(output.encode())
    
            print('Arduino is retriving: ')
            print(set_ser.read())
            if (output == "close"):
              set_ser.close()
              print ('Closed')
              a = 1
        elif (openSerial == "close"):
          set_ser.close()
          a = 1
        else:
          print ('open or close')
    sendSerialData()
    

The Arduino code:

    
    
    int pos = 0;    // variable to store the servo position
    
    int incomingByte= 180;
    void setup() {
      myservo.attach(5);  // attaches the servo on pin 5 to the servo object
      Serial.begin(9600);
    
    }
    
    void loop() {
    
        byte incomingByte = Serial.read();
        pos = incomingByte;
        myservo.write(pos); 
    
        Serial.print(pos);
        delay(500);
    

Here is the output in the program:

    
    
    type 'open'to open serial port. 
    type 'close' anyplace in the procram to close serial port:
    : open
    
    Type what you want to send, hit enter
    :  100
    Arduino is retriving: 
    b'\xff'
    
    Type what you want to send, hit enter
    :  1
    Arduino is retriving: 
    b'2'
    
    Type what you want to send, hit enter
    :  5
    Arduino is retriving: 
    b'5'
    
    Type what you want to send, hit enter
    :  2
    Arduino is retriving: 
    b'5'
    
    Type what you want to send, hit enter
    

Does anybody know how to fix this? Do I need to convert binary to decimal. I'v
tryed to declare incommingByte as int and byte, but the result are not
eksaktly the same but almost.

Thankful for any help.

i use: Python 3.4, pyserial and Windows 10.


Answer: I think your problem lies in the arduino side of the code. when you send data
over the serial protocol, all plain text(including integers) gets converted
into ASCII. For example, a lowercase "a" gets converted into a 97, so when you
say:

    
    
    byte incomingByte = Serial.read();
    pos = incomingByte;
    myservo.write(pos); 
    

You're trying to write ASCII numbers to a servo, so if you write 42, you might
get 5250 instead of 42. A possible way to fix this would be to cast the byte
as a char:

    
    
    byte incomingByte = Serial.read();
    pos = char(incomingByte);
    myservo.write(pos);
    

You might also want to do this only when you are receiving data:

    
    
    if (Serial.available() > 0) {
    byte incomingByte = Serial.read();
    pos = char(incomingByte);
    myservo.write(pos);
    

Hope this helped!

-Dave

