Arduino / Python stepper motor control

Hi all,
I'm looking to make a really simple CNC machine that effectively will run like a lathe with only 1 longitudinal axis and a rotary chuck. It doesn't need to go fast and it doesn't need large motors as the forces will be really low.
I have a arduino uno, a small stepper and the EasyDriver from sparkfun. Using just the arduino software I can drive the stepper around no problem.
What I want to do is to be able to send info to the arduino from my PC that will be generating some very basic code to tell the motor things like speed, direction, how many steps etc. I then want to have the arduino grab that data and use it to run the motor.
Is Python a good choice? So far I'm having a hell of a time getting it to talk to the arduino - all I've managed to do is make it open the serial port which resets the arduino but when I try send data (a number to make the LED flash a set amount of times) I get nothing.

If anyone has any suggestions on how I might be able to approach this problem better, if python is a good choice or if there is a system or some example code out there does the same job I would appreciate any help. Obviously I done my share of googling and I haven't had any luck with anything I've found.


Use whatever PC language you are most familiar with. Python is fine. Personally I use JRuby.

I suggest you write a short Arduino sketch that sends some text to the PC every 5 seconds or so and learn how to receive that with Python. You can test that the Arduino code works using the IDE Serial Monitor so you can concentrate on the Python stuff.

If you've got as far as the Python program causing the Arduino to reset you are making good progress. Have you the correct baud rate, 8 data bits and 1 stop bit set in the Python code?


Thanks for the tip on with the serial monitor - thats helping a lot!

I've got the point where I can send a number, say 6400 from python to arduino, have arduino make it into 6400 again and send it back. The code that the arduino uses to get the 6400 is this ( most of which was taken from an arduino cookbook I found online):

int ch;
int value;
void setup(){

void loop(){

else {

So i'm able to turn my LED on and off. This may be the wrong forum to post this question on and perhaps i'll switch it over.

In Matlab when I had a useful bit of code working I would save it as a function where I could call it from the main script by giving it a couple of inputs and it would spit back out an output that I could then use. I think this keeps the code pretty tidy and simplifies the main program. Is there anyway to do this in arduino? From searching I learned that arduino does have functions but they had to be in the same sketch as the rest of the code. I think I might be able to save it off somewhere separate if it was a library? I'm not sure if its worth it or not.
What I want to end up with is a program that receives info from the serial port, runs it through the short code to change it back to a numeric value, then assigns that value to a variable that will be used in the main script; i.e. number of steps, sends something back to python to request the next number and goes through it again until I have all the info I need to feed to the stepper motor to get it to run. Eventually I might be able to send a long string from python that includes all the info in one line - I'm not sure.

Does this sound like a sane way to do things?

A good way to communicate with the Arduino is to reserve two byte values (perhaps 253 and 254) as start and end indicators. Then the software (on both the PC and on the Arduino) waits for the start character then saves all the chars up to the end character. It's also a good idea if the first character after the start char represents the number of characters to be sent.

Of course the downside is that you can't use those two chars as data.

Regarding functions it is easy to split your Arduino code into functions. I would take things step by step. When you are comfortable using functions within a single .ino file you could then move on to having a second (or as many as you like) .ino file in the same directory as your main sketch. The Arduino IDE will load all the .ino files in alphabetical order after the main sketch.

You could re-package some of your functions into a library but I haven't felt the need to do that.