0

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi! this was helping me further, Because you made med realize the arduino only read the first character in the string. Google now gave me tha answer.
1

Dave helped me realize it was something with the arduino code and it only read 1 caracter of the string. afer some googling I found that to read the data corectly i have to use `

Serial.parseInt()`

the

if (Serial.available() > 0)

did fix the problem with the servo moving back to starting position; thanks Dave!

my arduino code no looks like:

int pos;    // variable to store the servo position
int incomingByte;
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
void setup() {




  myservo.attach(5);  // attaches the servo on pin 5 to the servo object
  Serial.begin(9600);

}

void loop() {

if (Serial.available() > 0) {
int incomingByte = Serial.parseInt();

myservo.write(incomingByte);


}

delay(500);
}

I kan now Control my servo from the software by typing a number between 0 and 180 degrees:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.