Let’s say we’re working together in a shared office space where you can rent some desk for a certain time. For your convenience, the tables can be adjusted in height. What if the system already learned your preferred settings for standing and sitting and automatically adjusts as you wish as soon as you walk up to the table? Slight correction? Just grab your phone and make some finer adjustments.
This tutorial shall give a simple starter project for web-accessible motion applications. We’re working on a Raspberry Pi 3B+ with Raspian installed in combination with a TMC5130-BOB stepper driver breakout board. The basic principle will be the same for different drivers. The driver strength of this particular chip is probably too small for the shown application but it’s perfect for a first test on the desk.
The TMC5130 is a stepper motor driver with .
Below you can find the 10 steps to take. It might be that some of them are already done in your case or you know a better way of doing it. Please feel free to share your wisdom
1. Set up Network & Interfacing
First we connect keyboard, mouse & display to Raspberry Pi and connect it to a network via wifi or cable. In the following steps we assume that the Raspi has access to the internet.

Now we should set static IP addresses for networks in order to access the Raspberry via networks & sub networks. In this tutorial, we use the following:

WIFI (wifi) http://192.168.178.184/

LAN (eth0) http://192.168.178.185/

Now we can make the Raspi accessible through your favorite machine. Therefore we open the configuration on the console:
sudo raspi-config
With the Interfacing options we can enable SSH, SPI and GPIOs:


The SPI and the GPIOs will be required in order to access the driver chip.
Enabling SSH allows us to address the
sudo apt-get install xrdp # install remote desktop service
For both you can log in with your standard Raspi credits.
2. Update
For the best user experience, we should make sure that all software is up to date. Run update/upgrade/dist-upgrade one after the other and reboot if packages were installed when running one of these commands. Start over until there are no updates left to install:
sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade sudo reboot
3. Install SPI Driver
The SPI driver can be found in the bcm2835 library. For this guide
You can check the latest version by scrolling to the bottom of the following page: http://www.airspayce.com/mikem/bcm2835/.
Run one line after the other.
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-X.XX.tar.gz gunzip bcm2835-X.XX.tar.gz tar xvf bcm2835-X.XX.tar cd bcm2835-X.XX ./configure make sudo make check # This should show you a pass message sudo make install sudo reboot -h 0
4. Download and install Apache Server, PHP and common Libraries
There are countless tutorials of that on the web. Here are the required lines for the most common setup for PHP7. Run one line after the other:
sudo apt-get install apache2 sudo service apache2 start [stop] sudo groupadd www-data sudo usermod -g www-data www-data sudo apt-get install apache2 php7.0 php7.0-curl php7.0-gd php7.0-imap php7.0-json php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-xmlrpc libapache2-mod-php7.0
5. Download Project Files
We put the project files directly to the web server directory. The following lines will navigate there, unpack the files, remove the archive and replace the default ‘hello world’ landing page:
cd /var/www/html/ sudo wget http://blog.trinamic.com/wp-content/uploads/2019/03/TMC5130_Raspi_Webserver.zip # download project archive sudo unzip TMC5130_Raspi_Webserver.zip sudo rm TMC5130_Raspi_Webserver.zip #remove archive sudo rm index.html # remove old hello world sudo chown -R $USER ./ # set current user as owner
Of course you can download the package directly in order to have a look upfront:
http://blog.trinamic.com/wp-content/uploads/2019/03/TMC5130_Raspi_Webserver.zip
6. Wire Everything Up
Here’s the pinout.
RASPBERRY PI | BOB -------------------------- (17) +3V3 | VCC_IO (19) SPI_MOSI | SDI (21) SPI_MISO | SDO (23) SPI_SCK | SCK (24) SPI_CE0_N | CSN (25) GND | GND Put connect following pins of the BOB to GND, too: CLK16 (enables internal clock) DRV_ENN (enables driver)


Now we can also connect the motor. Make sure that the motor is never disconnected when the driver is active. Therefore I highly recommend to always connect the motor first and then power the system. In this
7. Test TMC5130

Now we can compile the C source code that parametrizes the driver:
cd /var/www/html/TMC5130/ gcc ./src/TMC5130.c ./src/SPI.c -lbcm2835 -o TMC5130 # compile
You can find everything in the src sub-directory. I suggest to access it with your favorite text editor that has built-in SSH (SFTP) support.
cd /var/www/html/TMC5130/src/

The code includes the access to the chip registers, simple functions for movements and a standard initialization. It utilizes the SPI interface in order to communicate with the chip. All in all it’s just around 120 lines plus defines in the header.
In the TMC5130_init function you can find the basic initialization. Please check whether the programmed current of around 0.7A RMS is suitable for your motor. There is also a second example for 0.25 A RMS.
//* INITIALIZE void TMC5130_init(unsigned char motor) { tmc5130_writeInt(motor, TMC5130_GCONF, 4); // Enable stealthCHop (en_pwm_mode) tmc5130_writeInt(motor, TMC5130_TPWMTHRS, 500); tmc5130_writeInt(motor, TMC5130_PWMCONF, 0x000401C8); tmc5130_writeInt(motor, TMC5130_CHOPCONF, 0x000100C3); //tmc5130_writeDatagram(motor, TMC5130_IHOLD_IRUN, 0, 7, 5, 2); // 0.35Apeak / 0.25ARMS tmc5130_writeDatagram(motor, TMC5130_IHOLD_IRUN, 0, 7, 16, 2); // 0.00Apeak / 0.71ARMS //Reset position tmc5130_writeInt(motor, TMC5130_RAMPMODE, TMC5130_MODE_POSITION); tmc5130_writeInt(motor, TMC5130_XTARGET, 0); tmc5130_writeInt(motor, TMC5130_XACTUAL, 0); //Standard values for speed and acceleration tmc5130_writeInt(motor, TMC5130_VSTART, 1); tmc5130_writeInt(motor, TMC5130_A1, 30000); tmc5130_writeInt(motor, TMC5130_V1, 50000); tmc5130_writeInt(motor, TMC5130_AMAX, 10000); tmc5130_writeInt(motor, TMC5130_VMAX, 200000); tmc5130_writeInt(motor, TMC5130_DMAX, 10000); tmc5130_writeInt(motor, TMC5130_D1, 30000); tmc5130_writeInt(motor, TMC5130_VSTOP, 10); }
With this line you can run the compiled program:
sudo /var/www/html/TMC5130/TMC5130 # run program
Make sure the system is powered for the test. Otherwise, nothing will happen. If everything is OK, the motor starts moving with constant velocity.
Our program is not clever enough to stop the motor yet – that’s the next step. If you want to stop it, just unpower it. This will also reset the driver chip.
8. Demo Program
Before starting, we have to

Now that is done we can compile the whole project:
cd /var/www/html/TMC5130/ gcc ./src/*.c -lbcm2835 -o project # compile all c files
Everything worked well? Now we can test the program with different parameters. Please try one line after the other:
sudo ./project i #initialize motor sudo ./project r 100000 # rotate motor with velocity 100000 sudo ./project r -100000 # rotate motor with velocity -100000 sudo ./project 0 # stop motor sudo ./project t 51200 100000 # move to target 51200 with velocity 100000 sudo ./project b 51200 100000 # move by 51200 steps with velocity 100000 sudo ./project b -51200 100000 # move by -51200 steps with velocity 100000 sudo ./project g 33 # read register with decimal address 33 sudo ./project s 33 0 # write register with decimal address 33 to 0
In the main loop of the project.c file, we are looking at the different ASCII commands (i,r,0,t,b,g,s,d) and the corresponding parameters and use the TMC5130 code to perform different movements.
By the way, the number of 51200 steps is exactly one revolution for the most common hybrid stepper motors. Typically these motors have 200 full steps. The driver runs with 256 microsteps per default. 200*256=51200.
The given velocity of 100000 is here around 77500pps. You can calculate it with the following formula:
v[pps]=v*fclk/2^24
As we are using the internal clock of the chip of around 13MHz, this gives:
v[pps] = 100000 * 13000000 / 2^24
As we know that one
Further information regarding real world units can be found in the datasheet.
9. Make Programs Accessible
Our two motor control programs work well now. But as they are using important hardware blocks of the Raspi you need certain rights. Therefore we need to allow the www-data user to access our programs as
sudo visudo
Here we can add the following lines to the bottom:
www-data ALL=NOPASSWD: /var/www/html/TMC5130/project www-data ALL=NOPASSWD: /var/www/html/TMC5130/TMC5130
Now we can save the file and exit.
10. Check Out the Web Project

You can find all web relevant files in the web subfolder:
cd /var/www/html/TMC5130/web
We linked the index.html before. This means we can now open our browser and access the web project on the Raspi via its IP address (whichever you use http://192.168.178.184 or http://192.168.178.185) directly from our browser. (If you call it from the Raspi directly it’s of course http://localhost/)

All the forms are defined in the index.html file. Submitting a form by clicking a button will call the run.php with all given parameters. The run.php is called in an iframe, which is shown in the feedback field. Therefore all responses will be shown there.
The run.php translates the commands from the web interface into calls of our little project program, which will move the motor. Most commands are triggering or stopping
There is one additional command d (|<>|), which will start the demo, where the motor is moved back and forth. In
Summary
Of course you can build way way fancier programs, utilize java script and sweet graphics. But I hope this project gives you a brief idea of how to get started.
This project is meant as a starter for different projects around automation. You can shut your window blinds, open your cat flap or rock your baby to sleep. Very similar implementations allow stronger drivers like the TMC5160 or TMC5161, but also smaller two-axis systems that are e.g. based on the TMC5041.
As the actual driving of the motors doesn’t consume
For the driver itself, we’re working with standard C code. Therefore you can also use it on any microcontroller or platform in order to make it work in different environments.
Please feel free to add a comment if this post was helpful, about what you made out of it or if there is anything I can improve.
Thomas