Hello, I'm using the Nema 17 stepper motor and the A4988. (links are on the bottom of the post, where we bought them. + datasheet)
Vref = 1.2V
Rs = 0.1 ohm
Im using a bluetooth module (HC-05) to control the stepper motor, but i have the same problem without the bluetooth module. The problem: The stepper motor turns in a random direction whenever i tell it to turn. So it goes 1 time in the right direction and 1 time i nthe wrong for example.
And if i tell it to turn a full 360° it sometimes doesnt do that as well.
It feels like it is compeltely random.
Any advice?
#define dirPin 13
#define stepPin 12
#define stepsPerRevolution 200
int Incoming_value = 0;
void setup() {
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0){
Incoming_value = Serial.read();
Serial.print(Incoming_value);
Serial.print("\n");
if(Incoming_value == 5){
digitalWrite(dirPin, HIGH);
delay(500);
for (int i = 0; i < 1 * stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
}
else if(Incoming_value == 10){
digitalWrite(dirPin, LOW);
delay(500);
for (int i = 0; i < 1 * stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
}
}
Serial.print("geen inlezing");
Serial.print("\n");
}```
**Link to stepper motor :**
https://nl.aliexpress.com/item/32895756331.html?spm=a2g0o.order_list.order_list_main.85.187579d2rl8Z48&gatewayAdapt=glo2nld&fbclid=IwAR2uqCDHy7Cu45VnMzcmQpOkPmK5QAkRu0yNm5KCtKMrTOrooyG_TfR1uis
**links to driver :** https://nl.aliexpress.com/item/1005005626845259.html?pdp_npi=3%40dis!USD!5.39!4.26!!!!!%402102e2b216960721763781491d0a9f!12000033796908158!affd!!&dp=073196d53103ff31ea195434b9db809b&af=1711154&cv=47843&afref=https%3A%2F%2Finfica.com&mall_affr=pr3&utm_source=admitad&utm_medium=cpa&utm_campaign=1711154&utm_content=47843&aff_fcid=03260e1359f44e5394bbbbf0e90cb568-1696163092935-01926&aff_fsk&aff_platform=api-new-product-query&sk&aff_trace_key=03260e1359f44e5394bbbbf0e90cb568-1696163092935-01926&terminal_id=bf1c05f4fa834abc9d0303fbb58dbf49&afSmartRedirect=y&fbclid=IwAR0Lna3k6QgcNZsYsY5m3PCUfV1CEZfd6gpy1AxTFi0Cu0567v3ONEE_1yQ
I do not immediately see something wrong with your code. What are you sending? Text (as with serial monitor) or binary data? If text,the value 10 that you check for is the linefeed (`'\n``).
Your topic has been moved to a more suitable location on the forum. Please read the sticky topics in Uncategorized - Arduino Forum.
Thanks for using code tags in your first post It did not quite work out because the ``` need to be on their own line. I've fixed the }```.
I would connect the HC05 to a software serial port or extra hardware serial port if one is available and save Serial for program upload, debugging and monitoring program flow and variable values, at least while developing code. Having 2 things (HC05 and serial monitor) connected to the same port can cause problems.
You might want to have a look at the serial input basics tutorial to see how to make up data packets, receive and parse data packets reliably.
Hello,
I'm sending actual numbers on the serial monitor, and it works fine, I've done the exact same with another project.
It is the stepper motor which is not doing what I'm asking.
I'm sending data (numbers) from my app made with MIT app Inventor. That all works great, I've done it before. But the problem is about the stepper motor. Why does he spin in random directions, and doesn't always make full circles?
The Serial Monitor sends text, not numbers.
If you send 5, you are sending the character '5', not the number 5.
So to compare, you compare with
if(Incoming_value == '5')
Now your problem comes when you send 10 as that is two characters; '1' followed by '0'. Have a look at the link that @groundFungus provided in post #3; once you have it working with the serial monitor, you can concentrate on the MIT app.
Your other topic on the same subject deleted. If you have extra details or have changed the code then please add them to this topic in a new reply
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.
Can your motor jump from standstill to 1000 steps per second (300 RPM for a 200 step per rev motor), without missing steps?
Try this for your for loops:
for (int i = 0; i < 1 * stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
} // 75 RPM