Go Down

Topic: Shrinkifying a project - UNO to attiny85 (Read 661 times) previous topic - next topic

LarsHuisman

May 26, 2019, 12:04 pm Last Edit: May 26, 2019, 12:08 pm by LarsHuisman
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


And the code:
Code: [Select]
/* 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?

PaulRB

Why are you using a stepper motor? A DC motor with a gearbox would give you the same function without the need for any Arduino/chip at all.

dmjlambert

#2
May 27, 2019, 12:47 am Last Edit: May 27, 2019, 05:14 am by dmjlambert
The thing that is most challenging about ATtiny85 is getting used to doing without serial communication to the serial monitor of the IDE, for troubleshooting sketches.   Once I am spoiled with something like an UNO where I can sprinkle serial print statements in my code to let me know what the sketch is doing, I don't really like to lose that ability.   If the sketch is extremely simple, then it is easy to port.   I would not consider shrinking something with a complex sketch to ATtiny85 unless I was trying to get ready to go into production making hundreds of the project.   Even then, I would probably just pick an SMD ATmega328p.  When I want to shrink something for a project, usually I use a Pro Mini or DF Robot Beetle.   

DrAzzy

Virtually no microcontroller itself can supply power to other things. On the nano/uno/etc, they have a 5v pin, connected to the same +5v rail that powers the microcontroller on the board - that's coming from the board (ie, the part that you'd be making for your ATTiny85 project), not the controller. So there's nothing any different here, other than that since you're not using a ready-made board, you need to take responsibility for that.
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

LarsHuisman

Why are you using a stepper motor? A DC motor with a gearbox would give you the same function without the need for any Arduino/chip at all.
I figured I should go for the easiest controllable motor type and choose a stepper for my project, also because the speed of the motor wasn't fixed beforehand.



The thing that is most challenging about ATtiny85 is getting used to doing without serial communication to the serial monitor of the IDE, for troubleshooting sketches.   Once I am spoiled with something like an UNO where I can sprinkle serial print statements in my code to let me know what the sketch is doing, I don't really like to lose that ability.   If the sketch is extremely simple, then it is easy to port.   I would not consider shrinking something with a complex sketch to ATtiny85 unless I was trying to get ready to go into production making hundreds of the project.   Even then, I would probably just pick an SMD ATmega328p.  When I want to shrink something for a project, usually I use a Pro Mini or DF Robot Beetle.   
Good thing to point out. I have to admit I jumped to a conclusion too fast, I also didn't realize that I would need additional hardware like a crystal and programmer to work with the attiny85 but hey, we'll see what the future will bring.

Virtually no microcontroller itself can supply power to other things. On the nano/uno/etc, they have a 5v pin, connected to the same +5v rail that powers the microcontroller on the board - that's coming from the board (ie, the part that you'd be making for your ATTiny85 project), not the controller. So there's nothing any different here, other than that since you're not using a ready-made board, you need to take responsibility for that.
Allright, thanks a lot for clarifying this!

PaulRB

I figured I should go for the easiest controllable motor type and choose a stepper
But stepper motor is considerably more difficult to control than a DC motor. So your choice did not fit your own criteria.
because the speed of the motor wasn't fixed beforehand.
The speed of a DC motor can also be controlled, and controlled more easily than a stepper motor. So that's not a good reason to choose a stepper motor either.

Willem43

Quote
I also didn't realize that I would need additional hardware like a crystal and programmer to work with the attiny85 but hey, we'll see what the future will bring.
.
The ATtiny85 is a great MCU to use if you do not need the extra pins or memory of the 328P.  No need for external crystal or anything - you can clock it at 16MHz internally using the PLL.

Using Arduino as ISP with an UNO makes it easy to program.  I use an I2C LCD display for debugging.  You can use print almost as freely as with the serial monitor (only requirement is the need to position the cursor before the print - no big deal).

Make sure you use @DrAzzy's ATtiny core (get it here).

Willem.


Go Up