Driving a Step Motor

Instead of a servo, a step motor can also be connected to the HelvePic32. In this example I use the cheap and well known 28BYJ-48 Step Motor in the 5V setup and the common ULN2003 driver chip. The 28BYJ-48 has a gearbox in from that takes 2048 steps for the shaft to make a full rotoation.

With all the versions available for this motor, the colors of the cables may vary. I therefore added a simple cable schema.

The code is exactly the same as for the Arduino setup:

/*
    Based on Stepper exemple V1.01 11/30/2013 by terry@yourduino.com
    */
#define LEFT 0
#define RIGHT 1
uint8_t nP[2][8] = {{0,17, 9,10,11,12,13,14},{18,17, 1, 2, 3, 6, 7, 8}}; // pins of version 1.0 using DP32 bootloader

#include <AccelStepper.h>
#define FULLSTEP 4
#define HALFSTEP 8
// motor pins
#define motorPin1  nP[RIGHT][3]     // Blue   - 28BYJ48 pin 1
#define motorPin2  nP[RIGHT][2]     // Pink   - 28BYJ48 pin 2
#define motorPin3  nP[RIGHT][1]     // Yellow - 28BYJ48 pin 3
#define motorPin4  nP[RIGHT][0]     // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup()
{
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(50.0);
  stepper1.setSpeed(200);
  stepper1.moveTo(2048);  // 1 revolution   
}

void loop()
{
  //Change direction at the limits
  if (stepper1.distanceToGo() == 0) stepper1.moveTo(-stepper1.currentPosition());
  stepper1.run();
}

Warning: A step motor drwa a lot of current. The USB volatge supply may be replaced by a stronger voltage source. The ULN2003 can also be used with 12V if the 12V version of the motor is used.