A comprehensive guide to setting up and using the StepperMotor library on Raspberry Pi. Test rig:
- Raspberry Pi 5
- 64-bit Bookworm
- 2 x 28BYJ-48 motors
Ensure that your system is equipped with the necessary tools:
sudo apt install -y git build-essential
First, fetch and install the WiringPi library:
git clone https://github.com/WiringPi/WiringPi.git
cd WiringPi
git checkout tags/3.2
./build debian
sudo dpkg -i /home/pi/WiringPi/debian-template/wiringpi_3.2_arm64.deb
Clone the StepperMotor repository and compile the source code:
git clone https://github.com/bmagsalan/StepperMotor.git
cd StepperMotor
make
sudo make install
# Refresh the library cache
sudo ldconfig /usr/local/lib
Add your user to the gpio
group to allow operation without sudo
:
sudo usermod -a -G gpio pi
newgrp gpio
Compile your application with the following commands:
g++ -o test.bin main.cpp -lsteppermotor -lwiringPi
Execute your compiled application:
./test.bin
Here is a simple example to control stepper motors using the library:
#include <StepperMotor.h>
#include <iostream>
#include <string>
int main() {
StepperMotor motor1(5, 6, 13, 19);
StepperMotor motor2(14, 15, 18, 23);
std::string command;
int steps, speed;
bool ccw;
std::string stepMode;
std::cout << "Motor Control (commands: '1 start [steps] [cw/ccw] [speed] [full/half/wave]', '1 stop', '2 start [steps] [cw/ccw] [speed] [full/half/wave]', '2 stop', 'exit'):" << std::endl;
while (true) {
std::cin >> command;
if (command == "1") {
std::cin >> command;
if (command == "start") {
std::cin >> steps >> command >> speed >> stepMode;
ccw = (command == "ccw");
motor1.start(steps, ccw, speed, stepMode);
} else if (command == "stop") {
motor1.stop();
}
} else if (command == "2") {
std::cin >> command;
if (command == "start") {
std::cin >> steps >> command >> speed >> stepMode;
ccw = (command == "ccw");
motor2.start(steps, ccw, speed, stepMode);
} else if (command == "stop") {
motor2.stop();
}
} else if (command == "exit") {
break;
}
}
return 0;
}
To control a motor:
1 start 4096 cw 15 "full"
For more information, visit the following links:
This guide ensures a smooth setup and operation of the StepperMotor library for users of all levels.