Arduino Stepper Motor Control with Python

Hello,

I'm trying to control a stepper motor using Arduino and Python. I am using an Arduino UNO R3 board, Arduino Motor Shield, and a bipolar stepper motor (4 wires). I'm fairly new to Arduino, but from what I have understood so far (and read/found online) I have managed to make the stepper motor move in a loop of 200 steps forwards, 200 backwards, with a delay of 2 seconds. I have copied my code below.

I have a larger python program which I would like to integrate the stepper motor into, and so I would like to be able to move a certain number of steps by sending a command via python. Is this possible? Various Google searches have suggested using the 'serial' code, but I am unsure on how to implement this as nobody seemed to be using the same motor shield.

Could anyone possibly offer any advice/bits of code to help with this please? I think I've provided all details, but let me know if I've missed anything.

Many thanks,

Adam

Arduino Code:

#include <Stepper.h>

const int stepsPerRev = 200;

// Initialize the stepper library on the motor shield
Stepper myStepper(stepsPerRev, 12,13);

// give the motor control pins names:
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
int x = 0;

void setup() {
Serial.begin(9600);

// Set the PWM and brake pins so that the direction pins can be used to control the motor:
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);

// Initialize the serial port:
Serial.begin(9600);

// Set the motor speed:
myStepper.setSpeed(6);
}

void loop() {

myStepper.step(200);
myStepper.step(-200);

delay(2000);

}

Have a look at this:

python -> arduino

Thank you. After quickly looking at that it seems very useful. I will be going over it in detail to follow the steps and will then hopefully be able to apply it to my code. Ideally I would like to have a function in Python so that I could type some code, for example, myStepper.move(NumberOfSteps). I take it that's possible? Is there anything I should be aware of that changes when you introduce the motor shield?

Is there anything I should be aware of that changes when you introduce the motor shield?

Like what? Not sure I understand the question.

Ideally I would like to have a function in Python so that I could type some code, for example, myStepper.move(NumberOfSteps).

You can make your python program do whatever you want, haven't used it personally, but I'd assume it communicates with the Arduino over Serial, and your Arduino program interprets those commands into actions.

adambl:
but I am unsure on how to implement this as nobody seemed to be using the same motor shield.

There is no reason why a motor shield should interfere with serial data. However a motor shield is a poor choice for driving a stepper motor. Have a look at stepper motor basics.

You may also be interested in serial input basics. The code is fairly similar to the Python-Arduino code but it was written more recently and may be tidier.

I have a Python program that controls the 3 axes on my small lathe.

You should NOT use the delay() function in your "real" program. It is OK for testing, but it blocks the Arduino from doing anything else. The demo several things at a time illustrates how to manage timing with millis()

...R

Thanks both - those links will help out a lot!

Much appreciated!