ADI Trinamic Blog

We transform digital information into physical motion.

 
  • Newsletter
  • Blog Ethics
  • Legal/Impressum
  • Main Website
Menu
  • Newsletter
  • Blog Ethics
  • Legal/Impressum
  • Main Website
  • How to drive a stepper motor with your Raspberry Pi 3/2 using a TMC5130-EVAL

    In this start up guide you get explained how to connect your Raspberry Pi 3 or 2 for the basic operation of the TMC5130-EVAL board. It will use the internal Motion Controller and +3V3 logic supply for the TMC5130. The wiring will be limited to the basic functionallity to communicate via SPI. The internal CLK for the TMC5130 will be used respective the CLK16 pin (Shown on figure 1) has to be tied to GND. The SPI CLK frequency of the Raspberry Pi will be set up to be 1MHz.

    Preparation

    With a fresh installation of Raspbian (In this tutorial Raspbian Jessy Lite – Version March 2017 – Release date 2017-03-02) first update the libraries:

    sudo apt-get update
    
    sudo apt-get dist-upgrade

    This may take a while since it will upgrade all packages that are installed to the newest version if available. Furthermore it will upgrade dependencies or remove them if they have been replaced with other dependencies.

    Download and install the bcm2835 library. Note XXX is a place holder for the latest version. For this guide version “1.52” was used. In this case XXX is replaced by “1.52”. You can check the latest version by scrolling to the bottom of the following page: http://www.airspayce.com/mikem/bcm2835/

    wget http://www.airspayce.com/mikem/bcm2835/bcm2835-XXX.tar.gz
    
    gunzip bcm2835-XXX.tar.gz
    
    tar xvf bcm2835-XXX.tar
    
    cd bcm2835-XXX
    
    ./configure
    
    make
    
    sudo make check // This should show you a pass message
    
    sudo make install

    Afterwards restart your PI by typing.

    sudo reboot -h 0

     

    Wiring

    The wiring is very simple. You will need 8 jumper wires. As a reference you can use the TMC5130-Eval_v15_01_Schematic.pdf. You will find the signals that are on each pin.

    Signal Raspberry Pi 2 TMC5130-EVAL
    +5V +5V (2) +5V (5)
    GND GND (39) GND (2)
    TMC5130 enable GND (9) DIO_0 (8)
    TMC5130 CLK GND (6) CLK16 (23)
    MOSI SPI0 MOSI (19) SPI1_SDI (32)
    MISO SPI0 MISO (21) SPI1_SDO (33)
    SPI CLK SPI0 SCLK (23) SPI1_SCK (31)
    Chip select (CS) SPI0 CS0 (24) SPI1_CSN (30)

     

    Pinheader of TMC5130-EVAL

     

    Raspberry Pi 3 GPIO Header

    Raspberry Pi 3 GPIO Header – Source: https://www.element14.com/community/docs/DOC-73950/l/raspberry-pi-3-model-b-gpio-40-pin-block-pinout

    Raspberry 3 wired up with TMC5130-EVAL

    Raspberry 3 wired up with TMC5130-EVAL

     

    Please note that the TMC5130-EVAL has to be powered with the supply voltage e.g. 24V. To avoid damage only disconnect or connect a motor without supply voltage.

    Raspberry Pi Code

    An example code to initialize the TMC5130 is shown below. It will clear the reset flag in the GCONF register, sets example chopper configurations and sets the run and hold current for the motor. Furthermore it will set the ramping mode to velocity mode, an acceleration and a velocity. Be aware that the motor runs as soon as you execute the script.
    You can download the code directly with your pi by typing:

    wget http://blog.trinamic.com/wp-content/uploads/2017/04/spi-5130.c
    #include <bcm2835.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
      bcm2835_init();
      bcm2835_spi_begin();
    
      bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
      bcm2835_spi_setDataMode(BCM2835_SPI_MODE3); // The default
      bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); // The default
      bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
      bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
    
      // Send 5 byte to the slave and simultaneously read a byte back from the slave
    
      // Read GCONF to clear reset flag
      uint8_t gconf[40] = { 0x01, 0x00, 0x00, 0x00, 0x00 };
    
      bcm2835_spi_transfernb(gconf, gconf, 5);
    
      printf("Reply of TMC5130: %02x %02x %02x %02x %02x \n", gconf[0], gconf[1], gconf[2], gconf[3], gconf[4]);
    
      // Write chopper configs
      uint8_t chop_conf[40] = { 0xEC, 0x00, 0x01, 0x00, 0xC5 };
      bcm2835_spi_transfernb(chop_conf, chop_conf, 5);
      printf("Reply of TMC5130: %02x %02x %02x %02x %02x \n", chop_conf[0], chop_conf[1], chop_conf[2], chop_conf[3], chop_conf[4]);
    
      // Write IRUN=10 IHOLD=2
      uint8_t current[40] = { 0x90, 0x00, 0x06, 0x0A, 0x02 };
    
      bcm2835_spi_transfernb(current, current, 5);
    
      printf("Reply of TMC5130: %02x %02x %02x %02x %02x \n", current[0], current[1], current[2], current[3], current[4]);
    
      // Write - Set RAMPMODE to 1 (Velocity mode)
      uint8_t ramp_mode[40] = { 0xA0, 0x00, 0x00, 0x00, 0x01 };
    
      bcm2835_spi_transfernb(ramp_mode, ramp_mode, 5);
    
      printf("Reply of TMC5130: %02x %02x %02x %02x %02x \n", ramp_mode[0], ramp_mode[1], ramp_mode[2], ramp_mode[3], ramp_mode[4]);
    
      // Write - Set acceleration to AMAX = 50000
      uint8_t amax[40] = { 0xA6, 0x00, 0x00, 0xC3, 0x50 };
    
      bcm2835_spi_transfernb(amax, amax, 5);
    
      printf("Reply of TMC5130: %02x %02x %02x %02x %02x \n", amax[0], amax[1], amax[2], amax[3], amax[4]);
    
      // Write - Set velocity to VMAX = 200000
      uint8_t vmax[40] = { 0xA7, 0x00, 0x03, 0x0D, 0x40 };
    
      bcm2835_spi_transfernb(vmax, vmax, 5);
    
      printf("Reply of TMC5130: %02x %02x %02x %02x %02x \n", vmax[0], vmax[1], vmax[2], vmax[3], vmax[4]);
    
    
      // End SPI communication
      bcm2835_spi_end();
    
    
      bcm2835_close();
      return 0;
    }

    Compiling and running the code

    Please use the following command to compile the code.

    gcc –o spi-test spi-5130.c –lbcm2835

    This compiles the file spi-5130.c

    Now you are able to execute the example.

    sudo ./spi-test

    Related Pages

    • TMC5130A-TA
    • TMC5130-EVAL

    Share this:

    • Twitter
    • Facebook
    • LinkedIn
    • Pinterest
    • Print
    • Email

    Related

    April 7, 2017 / Lars Jaskulski / 17

    Categories: Code Snippet, Open Source Hardware, Products, Projects, Tutorial

    How to drive a stepper motor with your Arduino Mega using a TMC5130-EVAL How to drive a stepper motor closed loop with your Arduino Uno using a TMC4361A-EVAL + TMC2130-EVAL

    Comments are currently closed.

    17 thoughts on “How to drive a stepper motor with your Raspberry Pi 3/2 using a TMC5130-EVAL”

    • parovelb says:
      July 26, 2017 at 2:54 pm

      Good reference. And how to connect a RPi2/3 to a TMCM 6110 RS485 with a db9 connector?

    • Lars Jaskulski says:
      July 26, 2017 at 3:22 pm

      Hello, thanks for your feedback! Connecting a Raspberry Pi with a TMCM-6110 sounds interesting. We do not have an example ready but it sounds like a great idea. Are you currently working on that?

    • parovelb says:
      July 27, 2017 at 8:07 am

      Yes, I am working on that. I have trouble finding the complete pinout diagram for the rs485 connector of the module. In the hardware manual only the DATA+ (pin 7), DATA- (pin 2) and POWER(GND) (pin 3) are mentioned.

    • Lars Jaskulski says:
      July 27, 2017 at 9:31 am

      Hi, that is correct. The TMCM-6110 comes with a 2-wire RS-485 interface (Half-Duplex).
      Furthermore it seems that you will need additional RS-485 transceiver Hardware for the Pi. You’ll find ready to use modules / addons but could develop your own electronic as well.
      I could think of using a USB to RS-485 converter connected to the Raspberry Pi as well. Sureley depends on the compatibility of the drivers but might be worth a try.

    • A Hacking says:
      October 22, 2017 at 9:32 pm

      I have a few questions:

      Any example code for Python?

      As I understand the 5130 chip enables real time control of the stepper motor which is a huge benefit when working with a system like the RPi.

      How would you implement the end-stop detection in python via the stepper motor?

      Thanks

      Adam

    • Lars Jaskulski says:
      November 21, 2017 at 12:02 pm

      Hi Adam,

      Currently we do not have code examples available for python but this is a good idea for a future blog post.
      The end-stop detection is mainly dependent on the stallGuard2 feature. First a SPI communication to the chip is necessary. After that you can configure the responsible registers fitting for your needs.

      Below a screen capture on youtube that shows the necessary steps when using our GUI – TMCL IDE and the TMC5130-EVAL-KIT.
      https://youtu.be/exlx1mW2lP4

      You’ll find additional information on stallGuard2 in the TMC5130 datasheet and in an application note.
      https://www.trinamic.com/fileadmin/assets/Support/Appnotes/AN002-stallGuard2.pdf

      I hope that helps and feel free to share your results with the RPi using python + stallGuard2.

      Best regards,
      Lars

    • Patrick says:
      January 17, 2018 at 1:04 pm

      Hello,

      could someone explain to me how I can convert a number like 512000 into the 4 Sequences I need for the SPI transfer.
      I´m programming in C and I have no idea how to do this. It should be automatically converted in my program for example for Xtarget.

      thanks in advance

      Patrick

    • Lars Jaskulski says:
      January 22, 2018 at 4:31 pm

      Hello Patrick,

      This is a simple conversion from decimal to hexadecimal. 512000 dec equals 7D000 in hex. This can either be done by your own functions or I am sure you’ll find functions on the web that can be easily implemented.

      The datagram length of the SPI communication with the TMC5130 is 40 bit. As SPI in most cases work with 8 bit respective 1 byte the 40 bit are seperated into these sequences.

      Hope that helps. If you need more guidance, please let us know.

      Best regards,
      Lars

    • John says:
      March 13, 2018 at 3:55 pm

      Hello,

      Potentially a very stupid question, can you drive a TMC 2130 using a raspberry pi? If so can you point to somewhere I can find out more about doing so?

      All the best,

      John

    • Lars Jaskulski says:
      March 13, 2018 at 6:31 pm

      Hi John,

      You can use the same example that is been used for the TMC5130. As the TMC2130 is driven via external STEP/DIR you need to have a source to provide the step signal and the direction signal from a output pin of the RasPi.

      You can use the TMC2130 datasheet for further information on the register settings.

      Hope that helps as a starting point.

      Best regards,
      Lars

    • yanick says:
      August 27, 2018 at 4:17 pm

      HI Lars

      I have a question.
      I did everything as it says in this description. but when I compile I get an error:

      pi @ raspberrypi: ~ $ gcc -o spi-test spi-5130.c -lbcm2835
      gcc: error: -o: No such file or directory
      gcc: error: spi-test: No such file or directory
      gcc: error: -lbcm2835: No such file or directory
      pi @ raspberrypi: ~ $

      what is the problem? I installed everything as described

      Best regards,
      yanick

    • Lars Jaskulski says:
      September 11, 2018 at 9:19 am

      Hi Yanick,
      Sound to me that the source file is not in the directory you are currently working in. Have you fixed this issue in the mean time? Let me know and we can go on checking what might be the reason here.
      Best regards,
      Lars

    • Jade yang says:
      October 6, 2018 at 7:59 am

      Hello,
      I am testing the tmc5130 using tmc5130-eval and TMCL-IDE 3.0.
      I want to get the all register values at TMCL-IDE3.0, and I will copy these values to our new hardware system.

      I have checked your register browser, but I think the values in register browser are not the exact register values in tmc5130-eval board.
      For example, if i change the amax value, amax register value in register browser is not changed.

      Best regards.

    • Lars Jaskulski says:
      October 8, 2018 at 12:44 pm

      Hi Jade,
      You can read the register values in the register browser by clicking on the icon shown in the screenshot below. Please note it will read registers with RC (read and clear) as well and therefore clear set flags.
      The write only registers do not get updated when using for example the position mode but the read button Read button in register browser will update the values when clicked.
      TMC5130 Register Browser - Read all values
      Hope this helps!
      Best regards,
      Lars

    • Rhom says:
      October 28, 2018 at 11:03 pm

      Hi,
      nice example…
      i want to stop the motors after 1 turn, which command i have to use?
      I thought with:
      uint8_t ramp_mode[40] = { 0xA0, 0x00, 0x00, 0x00, 0x03 };
      bcm2835_spi_transfernb(ramp_mode, ramp_mode, 5);
      i can set the motors in status hold but it doesnt work,
      can you please advice me?

    • Lars Jaskulski says:
      November 14, 2018 at 9:32 am

      Hi,

      If you want to stop the motor after one turn in position mode you simply set the target position to one revolution. With 256 micro step resolution and a 200 ful step motor per turn this would be position 51200 (256 multiplied with 200).

      Best regards,
      Lars

    • Sebastian Müller says:
      November 19, 2018 at 10:09 pm

      @Yanick,

      I had the sam problem, compiling with gcc.

      Changing the parameter order fixed the problem

      gcc ./spi-5130.c -lbcm2835 -o spi-test

      seb

Recent Posts

  • Guest Blog: UBC Thunderbots
  • Guest Blog: TURAG
  • Guest Blog: School of Engineering Telecom Physics University of Strasbourg
  • Guest Blog: STAR Dresden
  • Pushing the Limits of Stepper Motor Control in 3D Printing

Social

  • View trinamic.mc’s profile on Facebook
  • View Trinamic_MC’s profile on Twitter
  • View trinamic’s profile on GitHub
  • View UC4SHA5_GAw1Wbm2T2NWpWbA’s profile on YouTube

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Newsletter

 

Info

Waterloohain 5
22769 Hamburg
+49 40 514 806 40

Tags

3D printer 3d printing arduino BLDC cDriver code code snippet coolStep drive Driver ease of use ethercat eval kit evaluation kit Field Oriented Control FOC GUI guide heliostat How-To IC linear stage motion controller motor driver open source hardware programming rtmi servo servo controller IC setup silentstepstick stealthChop stepper stepper motor stepper motor driver stepper motor driver ic stepper motors technology TMC4671 tmc5130 TMCL TMCL-IDE TRINAMIC TRINAMIC Motion Control tuning

Tag Cloud

    3D printer 3d printing arduino BLDC cDriver code code snippet coolStep drive Driver ease of use ethercat eval kit evaluation kit Field Oriented Control FOC GUI guide heliostat How-To IC linear stage motion controller motor driver open source hardware programming rtmi servo servo controller IC setup silentstepstick stealthChop stepper stepper motor stepper motor driver stepper motor driver ic stepper motors technology TMC4671 tmc5130 TMCL TMCL-IDE TRINAMIC TRINAMIC Motion Control tuning

Pages

  • Blog Ethics
  • Newsletter
  • Player Embed
  • Search Videos
  • User Videos
  • Video Category
  • Video Tag

Categories

  • Competition
  • Guest blog
  • Industry News
  • Jobs
  • Myths about Motors
  • Open Source Hardware
  • Products
  • Projects
    • DIY
    • University Projects
  • Research
  • Social
  • Software
  • technology
  • Trade Shows
  • Tutorial
    • Code Snippet
  • Uncategorized

Copyright © 2015 TRINAMIC BlogTheme created by PWT. Powered by WordPress.org

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.