Remove these ads by
Signing UpStep 1: Install
In order to make it work with older versions of the Arduino, you will need to trim a few pins off the motor shield. However, this is not, at all, recommended.
Insert the motor shield pins into the socket of the Arduino Uno.











































Visit Our Store »
Go Pro Today »




avrdude: stk500_getsync(): not in sync: resp=0x00
any help?
/*
This software will step (as opposed to wave or continuous drive) a two phase
4 wire stepper motor using the Arduino Mega 2560 and the Motor Shield Rev. 3
Of course, most of the examples I saw for stepper motor circuits didn't use
the Mega 2560. This didn't prove to be an problem because the
Motor Shield Rev 3. is compatible with the Mega 2560. Just got to be careful
with the pin alignment when you stack the two boards.
While you can drive a stepper motor using the Motor Shield, trying to
understand it can be a bit difficult since the references on the schematic
have to do with driving a regular motor, not a stepper motor.
So, to make the code more understandable, I redefined or relabeled the Pin Outs.
Note that Digital Out Pin 12 on the Shield corresponds to PWM 12 on
the Mega 2560 and so on. If you are looking at the PIN Out for the CPU,
this corresponds to PB6.
As you probably already know, to energize a coil, you have to
connect one end to a positive voltage and the other end to ground.
This way, current will flow from Positive to Ground. You can also
cause current to flow in the opposite direction by changing the voltages
in the other direction (Ground one end and apply a Positive voltage to the other).
If you are new to Adruino programming, make sure you use the exact case as
shown. For example, Output is not the same as OUTPUT.
Also, don't forget the semicolon on the end of a program statement.
*/
/* There are many different ways(algorithms) that you use to drive
your stepper motor. Some provide more torque, some provide greater
power savings. The method you chose will depend on your
over all design goals.
The algorithm I coded up is know as the Full Step, Two Phase On.
This implies that booth phases will be powered or energized at the
same time. It has only four states per step so it's probably the
easiest to code up.
Sate A1 B1 A2 B2
1 H H L L
2 L H H L
3 L L H H
4 H L L H
If you execute states 1 - 4, it will turn clockwise.
If you execute states 4 - 1, it will turn counter clockwise.
*/
// Redfine Pin Outs
//This is used to enable channel one on the driver chip.
//This corresponds to the PWMA on the driver schematic.
const int EnableOne = 3;
//This is used to enable channel two on the driver chip.
//This corresponds to PWMB on the driver schematic.
const int EnableTwo = 11;
//This corresponds to DIRA (referenced in two places) on the driver schematic.
const int CoilOneSideA = 12;
//Note that we are not going to define anything for the CoilOneSideB. The reason
//why is because it is controlled by the value at IN2 on the driver chip.
//The schematic shows it's connected to the output of a NAND gate. The symbol is
//incorrect, it is an Exclusive Nor Gate. The truth table for it is:
//
// X Y F
// 0 0 1 ******
// 0 1 0
// 1 0 0 ******
// 1 1 1
//
//The inputs to this gate are shown to be DIRA and BRAKE-A_DISABLE.
//If we set Break-A_Disable to a low or logic zero, the ouput(*) will be the opposite
//the other input, DIRA. This is just what you need, a way to Invert DIRA.
//So now all you need to do is set the value for DIRA (CoilOneSideA) and the other
//side of the coil will always be at the opposite value.
//Note: If your design just used a driver chip, you would need to drive or set the other
//side of the coil independently.
const int BreakADisable = 9;
//This corresponds to DIRB (Referenced in two places) on the schematic.
//Note that we are not going to define anything for the CoilTwoSideB for the same
//reason as discussed as above.
const int CoilTwoSideA = 13;
const int BreakBDisable = 8;
//This corresponds ro the SNSA on the schematic. If you use it, understand that it's analog.
//Right now it's not used.
const int CurrentSenseA = 0;
//This corresponds ro the SNSB on the schematic. If you use it, understand that it's analog.
//Right now it's not used.
const int CurrentSenseB= 1;
//I don't have a good feel for how long of a delay is needed but zero is
//not going to work to well.
int WaitTime = 300; //Setting this to longer values allows you watch each channel's LED
void setup() {
//Set Pin Modes
pinMode(EnableOne, OUTPUT);
pinMode(EnableTwo, OUTPUT);
pinMode (CoilOneSideA, OUTPUT);
pinMode (BreakADisable, OUTPUT);
pinMode (CoilTwoSideA, OUTPUT);
pinMode (BreakBDisable, OUTPUT);
}
void loop()
{
// This is the main code loop. It's where we energize the colis in on direction
// and then in the other.
int x = 100;
for (x = 0; x <= 25; x++)
{
StepForward();
// StepBackward();
}
}
void StepForward()
{
/*This is the step forward driver code*/
//Enable both Changeless at the same time.
digitalWrite(EnableOne,HIGH);
digitalWrite (EnableTwo,HIGH);
//Technically only have to do this once but I didn't want to code up
//the logic to detect if this had been done before.
digitalWrite(BreakADisable,LOW);
digitalWrite(BreakBDisable,LOW);
//Now energize the coils in following sequence
digitalWrite(CoilOneSideA,HIGH);
digitalWrite(CoilTwoSideA,HIGH);
//Need to give the motor a finite amount of time to move before you try to make it move again.
//this will also help reduce any moment over shoot or bounce.
delay(WaitTime);
digitalWrite(CoilOneSideA,LOW);
//Leave CoildTwoSideA set to HIGH
delay(WaitTime);
//Leave CoilOneSideA set to Low
digitalWrite(CoilTwoSideA,LOW);
delay(WaitTime);
digitalWrite(CoilOneSideA,HIGH);
//Leave CoilTwoSideA set to low
delay(WaitTime);
//Now that you are done with the step, disable both Changeless.
digitalWrite(EnableOne,HIGH);
digitalWrite (EnableTwo,HIGH);
}
void StepBackward()
{
/*This is the step backard driver code*/
//Enable both Channles at the same time.
digitalWrite(EnableOne,HIGH);
digitalWrite (EnableTwo,HIGH);
//Technically only have to do this once but I didn't want to code up
//the logic to detect if this had been done before.
digitalWrite(BreakADisable,LOW);
digitalWrite(BreakBDisable,LOW);
//Now energize the coils in foolowing sequence
digitalWrite(CoilOneSideA,HIGH);
digitalWrite(CoilTwoSideA,LOW);
//Need to give the motor a fin tie amount of time to move before you try to make it move again.
//this will also help reduce any moment over shoot or bounce.
delay(WaitTime);
digitalWrite(CoilOneSideA,LOW);
//Leave CoildTwoSideA set to LOW
delay(WaitTime);
//Leave CoilOneSideA set to Low
digitalWrite(CoilTwoSideA,HIGH);
delay(WaitTime);
digitalWrite(CoilOneSideA,HIGH);
//Leave CoilTwoSideA set to HIGH
delay(WaitTime);
//Now that you are done with the step, disable both Channels.
digitalWrite(EnableOne,HIGH);
digitalWrite (EnableTwo,HIGH);
}
Thanks from a noob
thanks.
thanks.......
Thanks for any advise.
Justin
Any pin not being used by the Arduino shield can be set up as a switch input.
Check out intro to Arduino to get started.
What I'm trying to do is to be able to press a button that makes the stepper turn x amount of steps at x rpm in one direction and then automatically stop until I press the button again. The other button would do the same thing but in the opposite direction. Can you show me a code that would do this?
Thanks much, justinmayer
I
// 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;
void setup() {
//Setup Channel A
pinMode(dirA, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(dirB, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeB, OUTPUT); //Initiates Brake Channel A pin
You should check out the link thedanger just posted:
http://krisarnold.com/2012/02/03/arduino-and-the-official-motor-shield-r3/
There are many ways to do this, but one way which might work for you is....
psudo-code:
if switch 1 triggered to "on"{
if switch 2 triggered left{
move motor left
}
else{
move motor right
}
}
So far the code uploaded without any errors but the stepper does not spin when I press the button but the brakes are on either way!
Any ideas? Thanks, Justin
// Include the Stepper Library
#include
// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
const int switchPin = 7;
// The amount of steps for a full revolution of your motor.
// 360 / stepAngle
const int STEPS = 200;
// Initialize the Stepper class
Stepper myStepper(STEPS, dirA, dirB);
void setup() {
// Set the RPM of the motor
myStepper.setSpeed(50);
//
pinMode(switchPin, INPUT);
// Turn on pulse width modulation
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, HIGH);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, HIGH);
// Turn off the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, LOW);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, LOW);
}
void loop()
{
if (digitalRead(switchPin == HIGH)
){
// Turn on pulse width modulation
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, HIGH);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, HIGH);
// Turn off the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, LOW);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, LOW);
}
else
{
// Turn off pulse width modulation
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, LOW);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, LOW);
// Turn on the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, HIGH);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, HIGH);
}
}
// Include the Stepper Library
#include
// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
const int switchPin = 7;
// The amount of steps for a full revolution of your motor.
// 360 / stepAngle
const int STEPS = 200;
// Initialize the Stepper class
Stepper myStepper(STEPS, dirA, dirB);
void setup() {
// Set the RPM of the motor
myStepper.setSpeed(50);
// Turn on pulse width modulation
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, HIGH);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, HIGH);
// Turn off the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, LOW);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, LOW);
}
void loop() {
// Move the motor X amount of steps
if (digitalRead(switchPin) == HIGH)
myStepper.step(800);
}
Each part of the code that looks like this is a single step:
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, HIGH); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A
There has to be an easy way to cycle through these with code, but I can't work through that at this given moment. I will post some more code when I find myself with some free time. However, searching for "Arduino stepper motor code" might bring up something useful...
http://krisarnold.com/2012/02/03/arduino-and-the-official-motor-shield-r3/
You can use the free pins from the Arduino to wire to the shield's required pins and run them side-by-side. This would require remapping the unused pins to the required pins on the second shield.
I don't want to take apart what I already have and switch to a stepper motor.
I need to add direction button and start-stop button any one can help please?
for this code
/*************************************************************
Motor Shield Stepper Demo
by Randy Sarafan
For more information see:
http://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/
*************************************************************/
int delaylegnth = 30;
void setup() {
//establish motor direction toggle pins
pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) CH A
pinMode(8, OUTPUT); //brake (disable) CH B
}
void loop(){
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, HIGH); //Sets direction of CH A
analogWrite(3, 1023); //Moves CH A
delay(delaylegnth);
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, LOW); //Sets direction of CH B
analogWrite(11, 1023); //Moves CH B
delay(delaylegnth);
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, LOW); //Sets direction of CH A
analogWrite(3, 1023); //Moves CH A
delay(delaylegnth);
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, HIGH); //Sets direction of CH B
analogWrite(11, 1023); //Moves CH B
delay(delaylegnth);
}
I´ve two motors 6V and I need to supply with an external battery.
Which battery I will manage through the two engines Arduino Motor Shield?