ArduinoA
Arduino8mo ago
ad0

BASIC STEPPER CONTROL WITH PI AND WAVESHARE STEPPER HAT

I have tried for hours to get the stepper motor to just spin a couple of times with cpp and all it does currently is lock the motor. there are no wiring issues because im literally using a waveshare stepper hat so it couldn't get any simpler. I had no problem with arduino and individual a4988s in the past either.

include <lgpio.h>
#include <unistd.h>  // for usleep
#include <iostream>

int main() {
    // Open GPIO chip
    int h = lgGpiochipOpen(0);
    if (h < 0) {
        std::cerr << "Failed to open gpiochip0\n";
        return 1;
    }

    // Motor 1 pins (same as your Python)
    const int STEP1 = 19;   // GPIO19
    const int DIR1  = 13;   // GPIO13
    const int EN    = 12;   // GPIO12

    // Motor 2 pins (second driver on HAT)
    const int STEP2 = 18;   // GPIO18
    const int DIR2  = 24;   // GPIO24
    const int EN2   = 4;    // GPIO4

    // Claim outputs
    lgGpioClaimOutput(h, 0, STEP1, 0);
    lgGpioClaimOutput(h, 0, DIR1, 1);
    lgGpioClaimOutput(h, 0, EN,   0);

    lgGpioClaimOutput(h, 0, STEP2, 0);
    lgGpioClaimOutput(h, 0, DIR2, 0);
    lgGpioClaimOutput(h, 0, EN2,  0);

    // Step both motors
    for (int i = 0; i < 200; ++i) {
        lgGpioWrite(h, STEP1, 1);
        lgGpioWrite(h, STEP2, 1);
        usleep(1000);
        lgGpioWrite(h, STEP1, 0);
        lgGpioWrite(h, STEP2, 0);
        usleep(1000);
    }

    lgGpiochipClose(h);
    return 0;
}
Was this page helpful?