Digital – Analog – Mechanical

Code Breakdown

If you are familiar with writing Sketches, this process will be quick and easy; however, if this is the first time you have worked with Arduino, I suggest you visit the Getting Started section on the Arduino website [5].

Arduino has been kind enough to include a library that supports stepper motors, which makes developing a sketch for controlling a stepper very simple (Listing 1).

Listing 1

Single Stepper Motor Sketch

01 #include <Stepper.h>
02
03 int StepA1Pin = 14;
04 int StepA2Pin = 15;
05 int StepA3Pin = 16;
06 int StepA4Pin = 17;
07
08 Stepper motor(512, StepA1Pin, StepA2Pin, StepA3Pin, StepA4Pin);
09
10  void setup()
11 {
12   pinMode(StepA1Pin, OUTPUT);
13   pinMode(StepA2Pin, OUTPUT);
14   pinMode(StepA3Pin, OUTPUT);
15   pinMode(StepA4Pin, OUTPUT);
16
17   while (!Serial);
18
19    Serial.begin(9600);
20    motor.setSpeed(10);
21 }
22
23 void loop()
24 {
25   if (Serial.available())
26   {
27     int steps = Serial.parseInt();
28     motor.step(steps);
29   }
30 }

After including the stepper.h library (line 1), the four pins used to drive the stepper are initialized in lines 3-6. The next command tells the Stepper.h library which pins are connected to the motor driver. The motor being used has 48 steps; however, the motor incorporates a reduction gearbox of 1:16, which translates into 16x32=768 steps, as reflected in line 8.

Lines 12-15 set the behavior of the pins used by the Arduino (input or output), and line 19 tells the Arduino to listen for commands coming from COM4. Line 20 sets the speed at which you want the stepper motor to move – the lower the value, the slower the stepper motor turns. However, increasing the value could cause the stepper motor to stop responding because it wouldn't be able to parse the pulses it receives.

Line 27 parses the COM4 commands into steps, and line 28 drives the step count to the Stepper.h motor function, causing the stepper to move. Now, you want to upload your sketch to the Arduino Mega.

Uploading the Sketch

Clicking the right arrow in the Arduino IDE toolbar or pressing Ctrl+U uploads the sketch. Once the sketch has been uploaded, you want to open the Serial Monitor to send "steps" to the motor. To open the Serial Monitor, click Tools | Serial Monitor or press Ctrl+Shift+M.

Not only does the Serial Monitor display data sent from the Arduino, you can also send data from the Serial Monitor to the Arduino by entering text in the text box. To begin, try a value of 500. This value should cause the motor to turn roughly 360 degrees. Entering a negative value will cause the motor to turn counterclockwise.

Now that you have a functioning stepper motor and can make it move in either direction, you can go ahead and wire the other two stepper motors according to Figures 4 and 7. After wiring all three stepper motors, you can augment the code to accept serial data and direct it to the correct motor driver (Listing 2). As you can see, a few changes have been made. The key changes are the additions of:

Listing 2

Multi-Stepper Motor Sketch

01 #include <Stepper.h>
02
03 int StepPins[] = {4,5,6,7,10,11,12,13,14,15,16,17};
04 int pinCount = 12;
05
06 Stepper motorA(512, StepPins[8], StepPins[9], StepPins[10], StepPins[11]);
07 Stepper motorB(512, StepPins[4], StepPins[5], StepPins[6], StepPins[7]);
08 Stepper motorC(512, StepPins[0], StepPins[1], StepPins[2], StepPins[3]);
09
10  void setup()
11 {
12    for(int thisPin-0; thisPin < pinCount; thisPin++) {
13      pinMode(stepPins[thisPin], OUTPUT);
14    }
15    while (!Serial);
16
17    Serial.begin(9600);
18    motorA.setSpeed(10);
19    motorB.setSpeed(10);
20    motorC.setSpeed(10);
21 }
22
23 void loop()
24 {
25   if (Serial.available())
26    {
27      int tMotor = Serial.parseInt();
28      int steps = Serial.parseInt();
29      switch(tMotor){
30        case 1: motorA.step(steps);break;
31        case 2: motorB.step(steps);break;
32        case 3: motorC.step(steps);break;
33      }
34    }
35 }
  • an array to optimize variable declarations,
  • two additional motor functions,
  • two additional setSpeed functions, and
  • a switch case to parse serial data.

From the Serial Monitor, you can now send data to Arduino to control the three stepper motors as follows:

1 500 2 500 3 500

The 1 500 tells motor A to turn 500 steps (i.e., 360 degrees), 2 500 tells motor B to turn 500 steps, and 3 500 tells motor C to turn 500 steps.

Now you have a fully functional three-tier stepper motor system accepting serial data from a laptop and driving whichever stepper motor you choose.

Buy this article as PDF

Express-Checkout as PDF

Pages: 7

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content