Need wiring & code help - single axis stepper motor control

Hey guys and gals,

Full disclaimer…code is NOT my thing. I’m a mechanic. Robots and computers are cool and fun but code…I don’t get it. I’m trying and I’ve read a lot here on the forum but I’m running out of time.

So I need help with wiring & code for a single axis stepper motor control using Arduino Uno and Big Easy Driver running a Mercury stepper motor.

The basic code is working, running the motor between two micro switches but I need to introduce a solenoid after the left switch is ‘tripped’. I found this circuit (link) and ordered the components >

[

My solenoid is 12V, 24W, 2.0A.

I think what I need to do is assign or define an output as LOW but I need for the solenoid to remain energized for 500 milliseconds while the motor begins its trip to the right limit switch.

In the code below, after the left limit switch is tripped, there’s a delay(1000); then I need to engage my solenoid for 500ms and at the same time, spin the motor to the right limit switch. Maybe all I really need to do is pull the output LOW after the delay and the motor will start?

Can someone help with the code? What pin should I use? Do I need to pull it LOW? Can you weigh in on the wiring schematic? Does it look good?

Thank you! - Rich

#define stepPin 2
#define dirPin 3
#define pinLimitswitchLeft 4  // these can be either left or right depending on motor wiring and dir pin.
#define pinLimitswitchRight 5 // these switches must be N.O. types
int stepDelay = 100; // this is the step delay in microseconds. Reduce this to speed up motor rotation and vice versa.

void setup()
{
pinMode (stepPin, OUTPUT);
pinMode (dirPin, OUTPUT);
pinMode (pinLimitswitchLeft, INPUT_PULLUP);
pinMode (pinLimitswitchRight, INPUT_PULLUP); 
}

void loop()
{
digitalWrite(dirPin, HIGH);  // set direction pin, the direction that the motor will rotate is dictated by it's wiring
while (digitalRead(pinLimitswitchLeft) == HIGH) // move motor until left limit switch is pressed.
{
  stepMotor(); 
}

delay(1000);

digitalWrite(dirPin, LOW);  // set direction pin, the direction that the motor will rotate is dictated by it's wiring
while (digitalRead(pinLimitswitchRight) == HIGH) // move motor until right limit switch is pressed.
{
  stepMotor(); 
}

delay(500);

}

void stepMotor()
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay); 
}

solenoid_driver.pdf (17 KB)](http://playground.arduino.cc/uploads/Learning/solenoid_driver.pdf)

Try this:

#define stepPin 2
#define dirPin 3
#define pinLimitswitchLeft 4  // these can be either left or right depending on motor wiring and dir pin.
#define pinLimitswitchRight 5 // these switches must be N.O. types
#define SolenoidPin 6

int stepDelay = 100; // this is the step delay in microseconds. Reduce this to speed up motor rotation and vice versa.
unsigned long SolenoidTriggerTime;

void setup()
{
pinMode (SolenoidPin, OUTPUT);
pinMode (dirPin, OUTPUT);
pinMode (dirPin, OUTPUT);
pinMode (pinLimitswitchLeft, INPUT_PULLUP);
pinMode (pinLimitswitchRight, INPUT_PULLUP); 
}

void loop()
{
digitalWrite(dirPin, HIGH);  // set direction pin, the direction that the motor will rotate is dictated by it's wiring
while (digitalRead(pinLimitswitchLeft) == HIGH) // move motor until left limit switch is pressed.
  {
  stepMotor(); 
  }

delay(1000);
SolenoidTriggerTime=millis();
digitalWrite(SolenoidPin, HIGH);
digitalWrite(dirPin, LOW);  // set direction pin, the direction that the motor will rotate is dictated by it's wiring
while (digitalRead(pinLimitswitchRight) == HIGH) // move motor until right limit switch is pressed.
  {
  stepMotor();
  if((millis() - SolenoidTriggerTime) > 500)
    {
    digitalWrite(SolenoidPin, LOW);
    }
  }

delay(500);

}

void stepMotor()
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay); 
}

Compiled, not tested.

Thanks wildbill! I should be able to set this up tomorrow!

I finally received parts and built up my solenoid circuit but that code didn't work wildbill. Motor doesn't spin at all. I reloaded the original code at the beginning of this thread and that still works.

Not sure if you'll see this but if you have any ideas, or if anybody else might be able to help?

I can't see anything wrong with the code - the only difference in the first few steps is the addition of pinmode for the solenoid pin - the first section of loop is unchanged, so I'd expect that to behave as the original did.

That suggests some kind of wiring issue - how is is wired up? In particular, how are you powering the motors and solenoid?

Also, in the spirit of walk before you can run, can you get the solenoid working with a simpler sketch without the motors hooked up?

Thanks for the message...

The Arduino has a dedicated 12V source. The Big Easy Driver and solenoid share power from a Velleman PS3003 30V-3.0A power supply.

Running the original sketch with the solenoid circuit connected to the Arduino pin 6 is good.

The solenoid circuit was built from a schematic found in the wiki playground (see link).

I verified the darlington transistor pins with the data sheet: 1-base, 2-collector, 3-emitter.

I am using the 2.2K ohms resistor. My solenoid is 12V, 2.A.

I am using a 1N4004G Rectifier (couldn't find the MUR340).

I'll go try to make the solenoid work. Thanks!

wildbill, Yes, the solenoid will work.

Now, this aint pretty but it did confirm the solenoid circuit. Just add power and the solenoid is energized.

(I’m not sure how I was supposed to add the code to this post)

#define SolenoidPin 6

int stepDelay = 100; // this is the step delay in microseconds. Reduce this to speed up motor rotation and vice versa.
unsigned long SolenoidTriggerTime;

void setup()
{
pinMode (SolenoidPin, OUTPUT);

}

void loop()
{

SolenoidTriggerTime=millis();
digitalWrite(SolenoidPin, HIGH);

if((millis() - SolenoidTriggerTime) > 500)
{
digitalWrite(SolenoidPin, LOW);
}
}

...of course, the solenoid does not turn off...

There’s a copy & paste error in my code - the pinmode for the dirpin is set twice. One of them should be setting it for the steppin.

NICE! Man I never saw that. Thank you!

I am up and running wildbill!!!

Thank you so much for your help!