Hello all,
I'm making a system to which I can temporarly attach my automata and turn the crank with a motor.
This will be only for exhibition, I don't want the motorisation to be definitive. You can have a look at my prototype on this post on my instagram. (The noise problem has been solved by using a TMC2209 driver.
Anyway this was just for context, I think I have pretty much sorted everything but as this is my first arduino (or even electronics) project I would like to know what you think about my setup and code, if there is anything I can optimise or if I made anymistake.
Basically, what I want is for people to:
- see the LED ON, to let them know the machine is ready
- press a button, LED OFF
- launch the motor for a choosen number of rotations
- wait 2 seconds
- LED ON
Here is my schematic
Here is my code:
#include <ezButton.h>
#include <AccelStepper.h>
#define ENA_PIN 9 // Enable
#define STEP_PIN 3 // Step
#define DIR_PIN 2 // Direction
#define START_BUTTON_PIN 12 // Button to start the loop
#define READY_TO_START_LED_PIN 11 // LED, On when not running, OFF when running
// Motor parameters
const float MOTOR_STEP_ANGLE = 1.8; // Step angle of stepper motor
const int MICRO_STEPS = 8; // micro steps setting of driver
const float MAX_SPEED = 500;
const float MAX_ACCELERATION = 200;
// Set the movement of the automaton
const int ROTATION_NUMBER = 20; //number of rotations needed
const int ROTATION_DIRECTION = 1; //rotation direction, 1 = clockwise, -1 = anticlockwise
bool is_running= false; //state of the motor
int total_steps; //numbers of steps the motor will have to make to turn the number of times needed
// Initialize stepper motor
AccelStepper stepper = AccelStepper(stepper.DRIVER, STEP_PIN, DIR_PIN);
ezButton start_button(START_BUTTON_PIN);
void setup() {
pinMode(ENA_PIN, OUTPUT);
digitalWrite(ENA_PIN, LOW);
// setup the buttons or inputs
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(READY_TO_START_LED_PIN, OUTPUT);
digitalWrite(READY_TO_START_LED_PIN,HIGH);
start_button.setDebounceTime(50);
// Convert the number of turns needed in steps (not really working, e.g.: If ROTATION_NUMBER=100 motor goes backward for just a few rotations)
total_steps = round(ROTATION_NUMBER*360 / MOTOR_STEP_ANGLE * MICRO_STEPS)*ROTATION_DIRECTION;
// setup the stepper motor
stepper.setMaxSpeed(MAX_SPEED);
stepper.setAcceleration(MAX_ACCELERATION);
}
void loop() {
start_button.loop();
if (!is_running && start_button.isPressed()){
is_running= true;
digitalWrite(READY_TO_START_LED_PIN,LOW);
stepper.moveTo(total_steps);
}
if(is_running){
stepper.run();
if(stepper.distanceToGo() == 0){
is_running= false;
stepper.setCurrentPosition(0);
delay(2000);
digitalWrite(READY_TO_START_LED_PIN,HIGH);
}
}
}
A few questions:
-
According to my calculations (wich are probably wrong) the TMC driver is in 8 microsteps mode.
What are the advantges/disadvantages of augmenting the microsteps ? -
should I connect the TMC to 5v or 3.3v ?
-
I plan on soldering the final PCB on this: ELEGOO boards
Do you have any advices on how to properly do it ? -
On my power supply, should I connect ground and -V ?
Thanks for you help.