Python turn a server using Ardiuno

Hi all!
I finally got my Ardunio code to actually turn my servo! I am now wanting to switch it over to python but see that I cannot directly call a sketch from python....

I did see pyserial but I am not really understanding the process of using it...

Basically I want to be able to call a function that tells ardiuno to turn my servo 45 degrees wait 5 seconds and go back to original position. I got it to work in the setup part of Ardiuno but have had zero luck with python 3.5

I would be grateful for any help or suggestions!

Congrats on getting your Arduino code to turn your servo!

Your next step would be to change your sketch so that the servo only moves when it receives serial input. Look at the Example here -> Serial.read() - Arduino Reference. Instead of printing incomingByte back to the serial like the example, you'd want to run the code that you currently have running in setup() that moves your servo. You'd be best off making your servo move only when it sees the correct character (eg: if (incomingByte == 'S') moveservo();).

You could then test that this works with the Arduino Serial Monitor. Open the monitor to connect to the COM port used by your Arduino, and try sending the 'S' character. You should see your servo move each time that you do.

The last step would be to use python in place of the serial monitor. Pyserial is the way to go. Use it to open up the same COM port you were opening with the Arduino Serial Monitor, and then write the character 'S' to that serial port. You should see your servo each time you run your Python script.

This will give you a very rough link between your python scripts and your Arduino controlling your servo. There are many ways to improve this, but hopefully it's enough to get you started.

This Python - Arduino demo may help to get you started.

As @BigBobby has said it is a good debugging technique to be able to control the Arduino with the SerialMonitor.

You may also find some useful ideas in Serial Input Basics which was written more recently than the Python demo.

...R

Excellent set of Pythonesque Arduino tuts here.