Skip to content

Instantly share code, notes, and snippets.

@Bumblefuck
Last active January 30, 2017 06:56
Show Gist options
  • Save Bumblefuck/949f30f55111432ff5a8620ead430401 to your computer and use it in GitHub Desktop.
Save Bumblefuck/949f30f55111432ff5a8620ead430401 to your computer and use it in GitHub Desktop.
/*
Arduino controlled Bipolar NEMA 34 stepper rotary axis for milling machine.
Copyleft AvE channel on Youtube
Use and Abuse, Improve as you see fit
Wiring connections
Arduino Pin 8 to stepper driver pulse +5V
Arduino Pin 9 to stepper driver direction +5V
Arduino Ground to stepper driver signal ground Pulse -ve, Dir -ve, enable -ve
Arduino +5 volts to on/off switch to stepper driver enable +5V
Helpful hints:
1. Pick the size of motor you need for the torque/power you need. Don't know? FIND OUT!
2. Determine the voltage and current of the stepper motor.
3. Determine the stepper motor driver that will power your stepper motor with some extra fudge factor (this stuff is cheap). Big Easy driver or Adafruit motor shield for small stuff. "CNC stepper driver" for big stuff.
4. Determine the power supply you need for the input power, voltage and current your stepper needs. Add extra fudge factor.
5. Get an Arduino. (Like you don't already have 3 of them kicking around).
6. Cut and Paste the code to the Arduino IDE; upload to Arduino.
7. Hook up wires per the diagram on the stepper driver box and the Arduino Sketch.
8. Hook up a switch from +5V from Arduino to the "enable" terminal of the stepper motor.
9. Hook up all 5V signal ground from the stepper driver (negative) to the arduino ground.
10. Set the stepper driver DIP switches for the max current, holding torque and steps per revolution you require.
11. Triple check your work. Cross your fingers and toes. Plug in the power and hit the "enable" switch.
*/
int count = 0; // sets up integer variable called count. count value set to 0
const int pulseset = 2400; // sets up a constant integer (does not change) called pulseset. We'll use this to set the amount of steps that the stepper turns; 2400 change this to change how far it rotates.
const int directionPin = 8; // sets up a constant integer (does not change) called directionPin. On arduino pin 8
const int pulsePin = 9; // sets up a constant integer (does not change) called pulsePin. On arduino pin 9. The stepper driver gives one step (or microstep) to the motor for every rising edge of 5v pulse from the arduino. 1uS minimum on time (driver dependant)
const int pulseTime = 1000; // sets up a constant integer (does not change) called pulsetime. This sets how long the step pulse from the arduino is on or off. set at 1000uS. Change this to change how fast the stepper rotates. Doesn't seem to have much effect below 10.
void setup() // the setup function runs once when you press reset or power the board
{
pinMode(pulsePin, OUTPUT); //sets the pulsePin (arduino pin 8) as an output
pinMode(directionPin, OUTPUT); // sets the directionPin (arduino pin 9) as an output
}
void loop() // this is the main program that runs over and over again
{
count = 0; // this resets the count value to zero
digitalWrite (directionPin, HIGH); // turns on the directionpin to +5V and tells the stepper driver to make steps in forward
while ( count < pulseset) // this checks the value of the count variable. While it is less or equal to the pulseset value, it will go through the commands withing the curly brakets.
{
digitalWrite(pulsePin, HIGH); // turns on the pulsePin to +5V and tells the stepper driver to make a step
delayMicroseconds(pulseTime); // this pauses the program for the amount of time in microseconds set by the sensorValue constant integer
digitalWrite(pulsePin, LOW); //turns off the pulsePin
delayMicroseconds(pulseTime); // pause program so stepper driver is ready to receive next step command. This doesn't have to be the same as the sensorValue. It could be any number, say if you wanted to turn the stepper as quickly as possible.
count ++; // adds 1 to the value of count, when count reaches value of pulse set we will exit the while loop and continue to the next command below
}
count = 0; // this resets the count value to zero. I'm cheating here and using this like PLC ladder rung. I know the program works one rung at a time (one command at a time in sequence)
// so it won't go back to the while loop above, it will start with the delay function and then the while loop below.
delay (2000); // this pauses the program 2000 milliseconds to pause the rotary axis between direction changes.
digitalWrite (directionPin, LOW); // turns off the directionPin and tells the stepper driver to take steps in reverse
while ( count < pulseset)
{
digitalWrite(pulsePin, HIGH);
delayMicroseconds(pulseTime);
digitalWrite(pulsePin, LOW);
delayMicroseconds(pulseTime);
count ++;
}
delay (2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment