Advertisement
ScienceGeyser

TMC2209Tester

May 26th, 2024
516
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <TMCStepper.h>
  2. #include <SoftwareSerial.h>
  3.  
  4. // Define pins
  5. int upPin = A0;
  6. int downPin = A1;
  7. int upSpeed;
  8. int downSpeed;
  9. int upInput;
  10. int downInput;
  11. int enableInput = 4;
  12. int enableOutput = 5;
  13. const int TX_PIN = 7;
  14. const int RX_PIN = 8;
  15. #define dirPin 2
  16. #define stepPin 3
  17.  
  18. // Define motor parameters
  19. const float R_SENSE = 0.11f;
  20. const uint8_t DRIVER_ADDRESS = 0b00;
  21. const int MICROSTEPS = 32;
  22. const int STEPS_PER_REVOLUTION = 200;
  23.  
  24. // UART communication
  25. SoftwareSerial softSerial(RX_PIN, TX_PIN);
  26. TMC2209Stepper driver(&softSerial, R_SENSE, DRIVER_ADDRESS);
  27.  
  28. void setup() {
  29.   Serial.begin(9600); // Initialize serial communication for debugging
  30.   pinMode(stepPin, OUTPUT);
  31.   pinMode(dirPin, OUTPUT);
  32.   pinMode(enableInput,INPUT);
  33.   pinMode(enableOutput, OUTPUT);
  34.   digitalWrite(enableOutput, HIGH);
  35.   softSerial.begin(19200); // Initialize software serial for TMC2209
  36.  
  37.   driver.begin();
  38.   driver.toff(5);
  39.   driver.rms_current(600);
  40.   driver.microsteps(MICROSTEPS);
  41.   driver.en_spreadCycle(false); // Disable SpreadCycle
  42.   driver.pwm_autoscale(true); // Enable automatic current scaling
  43.   driver.pwm_freq(2); // Set PWM frequency
  44.  
  45.   Serial.println("Driver initialized successfully"); // Debug statement
  46. }
  47.  
  48. void loop() {
  49.  upInput = analogRead(upPin);
  50.  upSpeed = map(upInput, 0, 1023, 45, 3);
  51.  downInput = analogRead(downPin);
  52.  downSpeed = map(downInput, 0, 1023, 45, 3);
  53.  //Serial.print("UP  ");
  54.  //Serial.print(upInput);
  55.  //Serial.print("     DOWN  ");
  56.  //Serial.println(downInput);
  57.  //Serial.println(digitalRead(enableInput));
  58. if(digitalRead(enableInput)==1){
  59.   digitalWrite(enableOutput, LOW);
  60. }else{
  61.   digitalWrite(enableOutput, HIGH);
  62. }
  63.  
  64.  
  65.  
  66.  if(upInput> 40){
  67.     digitalWrite(dirPin, LOW);
  68.     digitalWrite(stepPin, HIGH);
  69.     delay(upSpeed);
  70.     digitalWrite(stepPin, LOW);
  71.     delay(upSpeed);
  72.  }
  73.  
  74.  if(downInput> 40){
  75.     digitalWrite(dirPin, HIGH);
  76.     digitalWrite(stepPin, HIGH);
  77.     delay(downSpeed);
  78.     digitalWrite(stepPin, LOW);
  79.     delay(downSpeed);
  80.  }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement