element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Start a Movement Challenge
  • Challenges & Projects
  • Design Challenges
  • Start a Movement Challenge
  • More
  • Cancel
Start a Movement Challenge
Forum Implementing StallGuard using Arduino
  • Blog
  • Forum
  • Projects
  • DC
  • Leaderboard
  • Files
  • Members
  • More
  • Cancel
  • New
Join Start a Movement Challenge to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 5 replies
  • Subscribers 45 subscribers
  • Views 645 views
  • Users 0 members are here
  • trinamic motor control
  • CoolStep
  • tmc5272-eval-kit
  • StallGuard 2/4
  • tmc5272
  • start a movement
  • tmc5272 evaluation kit
Related

Implementing StallGuard using Arduino

taifur
taifur 6 months ago

StallGuard is a sensorless load measurement technology for stepper motors. Implementing StallGuard in a stepper motor driver eliminates the need for reference or limit switches and reduces the complexity and cost where precise referencing is required such as 3D printers and CNC machines. Trinamic Motion Controller like TMC5272 supports StallGuard and CoolStep technology and using those features together we can make a cost effective and power efficient solution. TMC IDE offers tuning and testing StallGuard and CoolStep very effectively and we can take advantage from it during custom firmware design for MCU for controlling TMC5272 like stepper controllers. 

StallGuard allows us to stop the motor to a specific load and we can set the sensitivity for the TMC5272. We can also set the upper and lower velocity threshold for enabling the StallGuard. Several registers of TMC5272 are involved with StallGuard and CoolStep and we need to programmatically control those registers from MCU. With the help of TMC IDE and the TMC5272 datasheet I was able to successfully implement StallGuard and CoolStep from Arduino. 

image

image

Images are captured from TMC5272 datasheet. The sg_stop bit of SW_MODE register is responsible for enabling or disabling of StallGuard2 or 4. 

This is the sample test code I wrote for Arduino for controlling StallGuard2.

/*******************************************************************************
* Copyright © 2023 Analog Devices Inc. All Rights Reserved.
* This software is proprietary to Analog Devices, Inc. and its licensors.
*******************************************************************************/

#include <SPI.h>

extern "C" {
#include "TMC5272_register_map.h"
#include "tmc5272.h"

}

/* 
 * Arduino Pins   Eval Board Pins
 * 51 MOSI        32 SPI1_SDI
 * 50 MISO        33 SPI1_SDO
 * 52 SCK         31 SPI1_SCK
 * 53 SS          30 SPI1_CSN
 * GND            23 CLK16 -> use internal 
 * 23 DIO         19 nSleep 
 * GND            2 GND
 * +5V            5 +5V_USB
 * 27 iRefR2      35 IREF_R2
 * 29 iRefR3      36 IRREF_R3
 * 31 uartMode    20 USEL/ for uart mode it should be HIGH
 */

#define IC_ID 0

int nSleep = 23;
int iRefR2 = 27;
int iRefR3 = 29;
int uartMode = 31;

void tmc5272_readWriteSPI(uint16_t icID, uint8_t *data, size_t dataLength) {
  digitalWrite(PIN_SPI_SS, LOW);
  delayMicroseconds(10);

  for (uint32_t i = 0; i < dataLength; i++) {
    data[i] = SPI.transfer(data[i]);
    Serial.println(data[i]);
  }

  delayMicroseconds(10);
  digitalWrite(PIN_SPI_SS, HIGH);
}

void setup() {
  Serial.begin(115200);

  // put your setup code here, to run once:
  pinMode(PIN_SPI_SS, OUTPUT);
  pinMode(nSleep, OUTPUT);
  pinMode(iRefR2, OUTPUT);
  pinMode(iRefR3, OUTPUT);
  pinMode(uartMode, OUTPUT);

  digitalWrite(PIN_SPI_SS, HIGH);
  digitalWrite(nSleep, LOW); // Disabling standby
  digitalWrite(iRefR2, LOW);
  digitalWrite(iRefR3, LOW);

  
  digitalWrite(uartMode, LOW);
  SPI.begin();
  SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE3));
  
  delay(10);
  
  //set resistance to 10K
  digitalWrite(iRefR2, HIGH);
  digitalWrite(iRefR3, HIGH);

  initMotorOne(IC_ID);
  
  //tmc5272_rotateMotor(IC_ID, 0, 0x00005000);

  tmc5272_writeRegister(IC_ID, TMC5272_TPWMTHRS(0), 0x1500000); //this is the upper velocity for StealthChop2 voltage PWM mode, ST2 is enable if TSTEP>=TPWMTHRS
  tmc5272_writeRegister(IC_ID, TMC5272_TCOOLTHRS(0), 0x50000);//lower velocity threshold for switching CoolStep and StallGuard2/4 features, enable if TCOOLTHRS >= TSTEP
  tmc5272_writeRegister(IC_ID, TMC5272_THIGH(0), 0x0);//upper velocity threshold for switching CoolStep, disabled if TSTEP <= THIGH
  tmc5272_writeRegister(IC_ID, TMC5272_SG4_THRS(0), 30); //stall detection threshold 
  //tmc5272_writeRegister(IC_ID, TMC5272_SW_MODE(0), 0x0);
  field_write(IC_ID, TMC5272_SW_MODE_SG_STOP_FIELD(0), 1); //enable stop by StallGuard2/4
 
  tmc5272_writeRegister(IC_ID, TMC5272_RAMPMODE, 0x0); //set position mode  
  tmc5272_writeRegister(IC_ID, TMC5272_VSTART(0), 0); //set maximum valocity
  tmc5272_writeRegister(IC_ID, TMC5272_VSTOP(0), 10); //set maximum valocity
  tmc5272_writeRegister(IC_ID, TMC5272_TVMAX(0), 0); //disable jerk reduction
  tmc5272_writeRegister(IC_ID, TMC5272_AMAX(0), 50000); //set maximum acceleration
  tmc5272_writeRegister(IC_ID, TMC5272_VMAX(0), 100000); //set maximum valocity
  tmc5272_writeRegister(IC_ID, TMC5272_DMAX(0), 1000); //set maximum deacceleration
  tmc5272_writeRegister(IC_ID, TMC5272_D2(0), 1); 
  tmc5272_writeRegister(IC_ID, TMC5272_XACTUAL(0), 0); //clear actual position
  tmc5272_writeRegister(IC_ID, TMC5272_XTARGET(0), 500000); //set a target position
  delay(10000);
  field_write(IC_ID, TMC5272_RAMP_STAT_STATUS_SG_FIELD(0), 0); //disable stop by StallGuard2/4
  field_write(IC_ID, TMC5272_SW_MODE_SG_STOP_FIELD(0), 0); //disable stop by StallGuard2/4
  tmc5272_writeRegister(IC_ID, TMC5272_XTARGET(0), 0); //return to home position
}

void loop() {
  
  int32_t value1 = tmc5272_readRegister(IC_ID, TMC5272_RAMP_STAT(0));
  int32_t value2 = tmc5272_readRegister(IC_ID, TMC5272_SG4_THRS(0));

  Serial.print("Received Data: ");
  Serial.print(value1, HEX);
  Serial.print("  ");
  Serial.println(value1);
  
}
 

I added some helpful comments in the code which can help you to easily understand the code and operation. In the code the driver is configure to drive the stepper motor in position mode. A target position is set to XTRAGT register. Motor moves clockwise to achieve the position and the StallGuard is enable in this journey. After reaching the target a 10s pause is set. The motor returns to home position again and this time the StallGuard is disable.   

Watch the demo video below for the above code:

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

  • Sign in to reply
  • Cancel

Top Replies

  • mp2100
    mp2100 6 months ago +1
    I very much like the concept of stall guard. Thank you for posting this.
Parents
  • Christopher678
    Christopher678 6 months ago

    Super Cool

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • taifur
    taifur 6 months ago in reply to Christopher678

    Its really a cool feature. Thank you.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • taifur
    taifur 6 months ago in reply to Christopher678

    Its really a cool feature. Thank you.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube