Hi,
I've prototyped a circuit to drive a stepper motor up and down when pushbuttons are held down ands included a homing function with a microswitch. I'd like to shrink this project and be able to use attiny85 instead of a Arduino Uno, since I'm only using 5 pins.
The wiring looks like this
Aansluitschema_stepper_2buttons_bb.pdf (838 KB)
And the code:
/* This example assumes a step/direction driver with Step on pin 9, Direction on pin 8
* And an input switch on pin 3. The switch is a switch to ground, with pin 3 pulled
* high with a pullup resistor. When the switch is turned on (closed, i.e. goes low)
* the the sepper motor steps at the rate specified (104 RPM in this code, with
* 1/8th microstepping of a 200 steps/rev motor)
*/
#define RPMS 250.0
#define STEP_PIN 9
#define DIRECTION_PIN 8
#define CW_PIN 3
#define CCW_PIN 2
#define home_switch 10
#define STEPS_PER_REV 200
#define MICROSTEPS_PER_STEP 8
#define MICROSECONDS_PER_MICROSTEP (1000000/(STEPS_PER_REV * MICROSTEPS_PER_STEP)/(RPMS / 60))
int steps; //toegevoegd als afstandsmeter
void setup() {
Serial.begin(2000000);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIRECTION_PIN, OUTPUT);
digitalWrite(STEP_PIN, LOW);
digitalWrite(DIRECTION_PIN, LOW);
pinMode(home_switch, INPUT_PULLUP);
pinMode(CW_PIN,INPUT); // button 1
pinMode(CCW_PIN, INPUT); // button 2
/////// Start Homing procedure of Stepper Motor at startup /////////
while (digitalRead(home_switch)) { // Do this until the switch is activated
digitalWrite(DIRECTION_PIN, HIGH); // (HIGH = ccw / LOW = cw
digitalWrite(STEP_PIN, HIGH);
delay(5); // Delay to slow down speed of Stepper
digitalWrite(STEP_PIN, LOW);
delay(5);
}
while (!digitalRead(home_switch)) { // Do this until the switch is not
// activated
digitalWrite(DIRECTION_PIN, LOW);
digitalWrite(STEP_PIN, HIGH);
delay(10); // More delay to slow even more while
// moving away from switch
digitalWrite(STEP_PIN, LOW);
delay(10);
}
steps=0; // Reset position variable to zero
Serial.println(steps);
} ///// End of homing procedure which is executed before pressing
// buttons for control.. /////
void loop() {
byte cwVal = digitalRead(CW_PIN);
byte ccwVal = digitalRead(CCW_PIN);
if( cwVal == LOW) {
if (steps > 0) { // To make sure the Stepper doesn't go beyond
// the Home Position
digitalWrite(DIRECTION_PIN, HIGH); // (HIGH = countercw / LOW = cw
digitalWrite(STEP_PIN, HIGH);
//delay(1);
delayMicroseconds(10);
digitalWrite(STEP_PIN, LOW);
//delay(1);
delayMicroseconds(10);
steps--; // Decrease the number of steps taken
Serial.println(steps);
}
}
if (ccwVal == LOW){
if (steps < 30000) { // Maximum steps the stepper can move away
// from the Home Position
digitalWrite(DIRECTION_PIN, LOW);
digitalWrite(STEP_PIN, HIGH);
// delay(1);
delayMicroseconds(10);
digitalWrite(STEP_PIN, LOW);
//delay(1);
delayMicroseconds(10);
steps++; // Increase the number of steps taken
Serial.println(steps);
}
}
}
I've seen that the Attiny85 needs to be fed a maximum of 5,5v on VCC
but is unable to supply 5v from the chip (like uno/nano), so I figured I should source 5v from the input on which the VCC is powered as well? What do you guys think, are the more challenges i'm faced with when trying to shrink this project down?