I am trying to write C++ code using the pigpio library to control a stepper motor. I want to use SPI mode to modify a few registers and use step/dir signals to control the motor's rotation direction and the number of steps. I'm a bit lost because I can't find an example that fits my needs. I have connected the default SPI pins on the Raspberry Pi to the driver (see the TMC2130 datasheet). The following code is my attempt, but when I examine the output with a logic analyzer, I don't see a clock signal, and the MOSI channel shows spikes instead of square waves. I have added a handle returned by the spiOpen function but I still have the same issues.

#include <iostream>
#include <pigpio.h>
#include <unistd.h>

using namespace std;

const uint8_t DIR_PIN = 27;
const uint8_t STEP_PIN = 22;
const uint8_t SPI_CHANNEL = 0;
const uint32_t SPI_SPEED = 500000;  // Fréquence SPI en Hz

void init() {
    gpioSetMode(DIR_PIN, PI_OUTPUT);
    gpioSetMode(STEP_PIN, PI_OUTPUT);

int spiHandle;

void setMicrostepResolution(int resolution) {
    uint8_t txBuf[2];
    txBuf[0] = 0x6c;  
    txBuf[1] = resolution;

    if (spiWrite(spiHandle, (char*)txBuf, 2) < 0) {
        cerr << "Erreur d'écriture SPI" << endl;

void stepMotor() {
    for (int i = 0; i < 2000; ++i) {
        gpioWrite(STEP_PIN, 1);
        gpioWrite(STEP_PIN, 0);

int main() {
    if (gpioInitialise() == -1) {
        cerr << "Échec de l'initialisation de pigpio." << endl;
        return -1;

    // Ouvrir le canal SPI
    spiHandle = spiOpen(SPI_CHANNEL, SPI_SPEED, 0);
    if (spiHandle < 0) {
        cerr << "Erreur d'initialisation SPI" << endl;
        return -1;


    while (true) {
        gpioWrite(DIR_PIN, 0);
        cout << "Le moteur tourne dans le sens horaire" << endl;

        gpioWrite(DIR_PIN, 1);
        cout << "Le moteur tourne dans le sens antihoraire" << endl;

    return 0;
New contributor
Juliette Lavender is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Welcome. You should indicate which model you are using (and note that pigpio does not work with the Pi 5).
    – goldilocks
    Commented Jun 10 at 17:31
  • Thanks, yes sorry. I am using a raspberry pi 3b+ Commented Jun 11 at 9:13
  • Best place to add that info (re 3B+) is in the Tags field immediately below your question. Tags has an "autocomplete" feature that makes it easier to find the correct tag. I've added it, but please think about that in the future. The biggest problem here is lack of information in the questions. Model # & OS version are (typically) two very basic items.
    – Seamus
    Commented Jun 11 at 16:06

1 Answer 1


You have fundamental errors in your SPI code.

You don't save the handle returned by spiOpen which is a parameter to spiWrite.

I have never used pigpio in SPI mode but the documentation states:-

int spiOpen(unsigned spiChan, unsigned baud, unsigned spiFlags)
This function returns a handle for the SPI device on the channel. Data will be transferred at baud bits per second. The flags may be used to modify the default behaviour of 4-wire operation, mode 0, active low chip select.
int spiWrite(unsigned handle, char *buf, unsigned count)
This function writes count bytes of data from buf to the SPI device associated with the handle.

Your code fails to check the value returned by spiWrite

  • Great thank you I will look into that Commented Jun 11 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.