Using Python and Pyserial to move stepper motor with Arduino

Hey Everyone,

I am new to pyserial (python module) and arduino. I would like to use pyserial to connect to the arduino, which I believe I have already done through the USB port ASB0.

I now would like to send over the necessary bit information to move the stepper motor in steps, as well as at different constant speeds. How do I do that? Do I also need to have the arduino software running simultaneously?

I am aware of the serial.read and serial.write commands, but I am a little confused about the communication with machine code and python code.

Any help would be greatly appreciated.

Tom

Here's what you need to do. First, make an Arduino program that can run by itself and move the stepper motor based on commands you send from the serial monitor. You will likely want to invent your own communications standard. You could do something like DYYYYY where D is a 1 or 0 that represents the direction of the motion and YYYYY represents the number of steps it should take. You could add more terms for speed, acceleration etc. From there your Arduino program will have to parse this data and use one of the stepper libraries to run the motor.

If you can get this working with serial monitor then porting this to python is fairly easy. You just need to replace your serial monitor inputs with serial data from pyserial. The python program will only be running on your PC and the only link between the Arduino and your PC is the serial line. I found the pyserial examples online quite helpful, so give those a glance.

Go ahead and try to get started, and if you get stuck post back with questions. I have done almost exactly what you are trying to do before. I will post some of my code here if I can find it back.

thank you, sir!

Would very much appreciate your code!

I finally had the time to search through my archives and find the code. I attached the files that I submitted here. The modified dates will be listed as today, as I did redact my college ID# from them.

pingAndMotor is the Arduino sketch, and final_project.py is the main python code. The other python files are classes, and the rest of the files are either the documentation I submitted with my project, or saved files that can be loaded by the application.

To give a sense of what the program did, it controlled a sensor that was made by mounting an ultrasonic sensor to the top of a stepper motor. The program took distance measurements at every step of the motor (200 steps per revolution) to make a map of the room. It also permits the user to move the sensor and conduct more measurements there. You can run the program without the sensor by loading the included files, but most likely you just want to see the code.

Note on Libraries: The python code uses the pyserial library which you are familiar with and the cs1graphics library which you might not be. It is found here. http://www.cs1graphics.org/

The code should be commented quite well, as it was being graded on that, but let me know if you have any questions.

PythonSerialProj.zip (255 KB)