I'm new to programming and I want to control a stepper motor using rc transmitter. This is the code I modified from but the motor does not turn. Any help would be highly appriciated. These are the products I'm using
How are you wiring the step/dir type stepper to the Arduino using the Stepper library. Please post a schematic. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.
The Stepper library is not appropriate for your driver. You should use a library like AccelStepper or the MobaTools stepper library (from among many). Personally, I find the MobaTools library easier to learn. Both are available through the IDE library manager.
Sorry, but that is not the way to wire that driver to an Uno. It will not work that way, no way.
There are 2 ways to go. Wire all of the - terminals to ground and each + terminal to an output pin or wire all of the + terminals to Vcc and the - terminals to output pins. I would go with the former. That is shown in Figures 3 and 4 of page 4 of the manual that you linked.
Then to control the stepper you can write your own code. See Robin2's simple stepper code. Or use a library that is written for that type of driver. The Stepper library is not. Like I posted, the AccelStepper library or the MobaTools stepper library are written for that type of driver.
You may be able to leave the enable disconnected and the motor will run, but check the manual.
This code works good even though it is vibrating, the problem starts when I put angles. I'm not sure if its the way I add angles to this code or maybe the stepper can't turn to an angle when using this transmitter.
#include <Stepper.h>
int ch2 = 0;
const int stepsPerRevolution = 200;
// initialize the stepper library:
Stepper myStepper(stepsPerRevolution, 9, 10);
void setup() {
pinMode(2, INPUT); //from RC reciever ch2 SC switch on Transmitter
myStepper.setSpeed(600);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
Serial.begin(9600);
}
void loop() {
//get current values of RC receiver
ch2 = pulseIn(2, HIGH, 25000);
if (ch2 < 1200)
{
Serial.println("clockwise");
myStepper.step(100);
}
else if (ch2 > 1700)
{
Serial.println("counterclockwise");
myStepper.step(-100);
}
}
I will try to explain why the Stepper library will not work. Here is the output from the Arduino to the driver from the Stepper library turning the stepper. Note that there are nice step pulses, but the dir output is also pulsed. Each time that that dir pin changes state, the stepper motor reverses. So it is just oscillating back and forth. Thus the vibrating.
Now here is the output from the AccelStepper library (MobaTools would be much the same). This is set up to output 10 steps pulses, reverse the motor and 10 more steps. See how the dir remains in one state while the motor runs in that direction? That is what the step/dir driver, like yours, wants to see.