I'm in my high school engineering class and can't figure out how to code things. I need to make a stepper motor spin using an IR sensor. I've tried my best couldn't figure it out. I have a code right now I'll put it here please let me know what I'm doing wrong
#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6);
void setup()
{
pinMode(7,INPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(7)== HIGH)
{
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(1000);
}
else
{
Serial.println("stop");
delay(1000);
}
}
kmin
October 11, 2024, 5:51pm
3
the_realest:
using an IR sensor
What is that IR sensor detecting? Is it active high or low sensor?
1 Like
we are unable to make the stepper spin when the IR sensor detects something
DaveX
October 11, 2024, 5:56pm
5
What isn't working as expected?
Does it print the "clockwise" and "stop" messages as expected when you tweak the IR sensor? If so, it means your sensor and sensor-reading code is working.
DaveX
October 11, 2024, 6:02pm
6
If you don't set the speed, it may try to step faster than the motor can physically move.
Try:
#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6);
void setup()
{
pinMode(7,INPUT);
Serial.begin(9600);
myStepper.setSpeed(60); // <<<< set a speed other than "infinite"
}
void loop() {
if (digitalRead(7)== HIGH)
{
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(1000);
}
else
{
Serial.println("stop");
delay(1000);
}
}
What do you have connected between pins 3, 4, 5 & 6 and the motor?
1 Like
Yeah I made them both work separately and then ended up having trouble in the combining phase but ended up figuring it out in the end.
Thanks for telling me to set a speed I think that's what helped me finally get it up and running.
Here's the fully finished code
#include <Stepper.h>
const int stepsPerRevolution = 360;
Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6);
void setup()
{
pinMode(7,INPUT);
Serial.begin(9600);
myStepper.setSpeed(60);
}
void loop() {
if (digitalRead(7)== LOW)
{
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(1000);
}
else
{
Serial.println("stop");
delay(1000);
}
}
1 Like
system
Closed
April 12, 2025, 11:48pm
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.