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);
}