Skip to content

Arduino MEGA 2560 - "Hello World" #33

Description

@rudibr

First, thanks for making this library available!!!
Second, appologies if this is a dumb post, but I am totally new to steppermotors.

I have taken the "TMC5160_SPI.ino" example, to try and control a stepper motor through a TMC5160T controller.
The TMC5160T is connected to the Arduino as specified below.
Most of the connections seems to be unneded, since the "TMC5160_SPI.ino" example only uses two pins, SPI_CS and SPI_DRV_ENN.

I have three questions:

  1. Does SPI_CS in the "TMC5160_SPI.ino" example corrospond to the CFG3/CS pin on TMC5160T?
  2. Does SPI_DRV_ENN in the "TMC5160_SPI.ino" example corrospond to the EN pin on TMC5160T?
  3. What are all the other pins for?

PS:
I have not yet connected the motor to the controller and tried the program below. I'm affraid to fry the controller and/or the motor :-O

`// Include TMC5160 library. https://github.com/tommag/TMC5160_Arduino
#include <TMC5160.h>

// Simple "Hello World" program, to communicate with stepper motor through TMC5160 controller from an Arduino MEGA 2560
// Based on the "TMC5160_Arduino/examples/TMC5160_SPI/TMC5160_SPI.ino" example

// NEMA 17 2 x Nema 17 Bipolar 59Ncm(83.55oz.in) 2A 42x48mm 4 Wires w/ 1m Cable & Connector, Model 17HS19-2004S1
// https://www.omc-stepperonline.com/de/nema-17-bipolar-59ncm-84oz-in-2a-42x48mm-4draehte-w-1m-kabel-verbinder-17hs19-2004s1

//
// TMC5160 --> Aduino pin connections
// VM --> 12V (red)
// GND --> GND (black)
// VIO --> 5V (red)
// GND --> GND
// A1 --> to A+ on NEMA17 stepper
// A2 --> to A- on NEMA17 stepper
// B1 --> to B+ on NEMA17 stepper
// B2 --> to B- on NEMA17 stepper
// EN --> D23 (white)
// CFG1/MOSI --> D25 (orange)
// CFG2/SCK --> D27 (blue)
// CFG3/CS --> D29 (green)
// CFG0/MISO --> D31 (brown)
// CLK --> D33 (gray)
// STP --> D35 (purple)
// DIR --> D37 (yellow)

const uint8_t SPI_CS = 29; // CS pin in SPI mode
const uint8_t SPI_DRV_ENN = 23; // DRV_ENN pin in SPI mode

TMC5160_SPI motor = TMC5160_SPI(SPI_CS); //Use default SPI peripheral and SPI settings.

void setup()
{
// USB/debug serial coms
Serial.begin(9600);

pinMode(SPI_DRV_ENN, OUTPUT);
digitalWrite(SPI_DRV_ENN, LOW); // Active low

// This sets the motor & driver parameters /!\ run the configWizard for your driver and motor for fine tuning !
TMC5160::PowerStageParameters powerStageParams; // defaults.
TMC5160::MotorParameters motorParams;
motorParams.globalScaler = 98; // Adapt to your driver and motor (check TMC5160 datasheet - "Selecting sense resistors")
motorParams.irun = 31;
motorParams.ihold = 16;

SPI.begin();
motor.begin(powerStageParams, motorParams, TMC5160::NORMAL_MOTOR_DIRECTION);

// ramp definition
motor.setRampMode(TMC5160::POSITIONING_MODE);
motor.setMaxSpeed(400);
motor.setAcceleration(500);

Serial.println("starting up");

delay(1000); // Standstill for automatic tuning
}

void loop()
{
uint32_t now = millis();
static unsigned long t_dirchange, t_echo;
static bool dir;

// every n seconds or so...
if ( now - t_dirchange > 3000 )
{
t_dirchange = now;

// reverse direction
dir = !dir;
motor.setTargetPosition(dir ? 200 : 0);  // 1 full rotation = 200s/rev

}

// print out current position
if( now - t_echo > 100 )
{
t_echo = now;

// get the current target position
float xactual = motor.getCurrentPosition();
float vactual = motor.getCurrentSpeed();
Serial.print("current position : ");
Serial.print(xactual);
Serial.print("\tcurrent speed : ");
Serial.println(vactual);

}
}`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions