Skip to content

Instantly share code, notes, and snippets.

@ghent360
Last active April 20, 2019 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghent360/318405049ba3e06b900c88b9174170d8 to your computer and use it in GitHub Desktop.
Save ghent360/318405049ba3e06b900c88b9174170d8 to your computer and use it in GitHub Desktop.
Example of using TMC5130-BOB in RAMP generation mode with the TMCStepper library
#include <SPI.h>
#include <TMCStepper.h>
// Uses hardware Serial for Arduino UNO
// Wiring Arduino -> TMC5130-BOB
// 11 (MOSI) -> SDI
// 12 (MISO) -> SDO
// 13 (SCK) -> SCK
// 10 -> CSN
// 9 -> DRV_EN
// +5V -> VCC_IO
// GND -> GND
//
// In addition to the Arduino, connect pin CLOCK16 to GND uing a jumper.
// Connect motor to A1,A2,B1,B2 pins
// Connect power more than 9V and less than 36V to VS and ground from that power to GND
#define EN_PIN 9 // Enable
#define CS_PIN 10 // Chip select
#define R_SENSE 0.1 // Match to your driver
TMC5130Stepper driver = TMC5130Stepper(CS_PIN, R_SENSE);
void setup() {
SPI.begin();
pinMode(EN_PIN, OUTPUT);
pinMode(CS_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW);
digitalWrite(CS_PIN, HIGH);
Serial.begin(9600);
driver.begin();
driver.GCONF(0);
// CHOPCONF
driver.toff(3);
driver.hstrt(4);
driver.hend(1);
driver.tbl(2);
driver.chm(0);
// IHOLD_RUN
driver.rms_current(800); // 800ma motor current
driver.TPOWERDOWN(10);
driver.en_pwm_mode(true);
driver.TPWMTHRS(500);
// PWMCONF
driver.pwm_autoscale(true);
driver.pwm_freq(0); // 2/1024
driver.pwm_ampl(200);
driver.pwm_grad(1);
// RAMP parameters
driver.A1(1000); // A1 = 1 000 Initial acceleration
driver.V1(50000); // V1 = 50 000 Acceleration threshold velocity V1
driver.AMAX(500); // AMAX = 500 Acceleration above V1
driver.VMAX(300000); // Maximum speed
driver.DMAX(700); // DMAX = 700 Deceleration above V1
driver.D1(1400); // D1 = 1400 Deceleration below V1
driver.VSTOP(10); // VSTOP = 10 Stop velocity (Near to zero)
driver.RAMPMODE(0); // RAMPMODE = 0 (Target position move)
driver.XACTUAL(0);
driver.XTARGET(0);
}
void loop() {
driver.XTARGET(512000);
delay(20000);
Serial.print("Position ");
Serial.println(driver.XACTUAL());
driver.XTARGET(0);
delay(20000);
Serial.print("Position ");
Serial.println(driver.XACTUAL());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment