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 functionality 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-getdist-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 placeholder 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

Afterward, 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 
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 a 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

Here the code:

#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

Still need help? Contact Us Contact Us