Control Stepper motor 28BYJ-48 ( ULN2003) + Joystick with Arduino




schematic diagram

Arduino connect ULN2003 Driver

ULN2003 IN1 ⟶ Arduino D8
ULN2003 IN2 ⟶ Arduino D9
ULN2003 IN3 ⟶ Arduino D10
ULN2003 IN4 ⟶ Arduino D11


Arduino connect Joystick

The Joystick VCC ⟶ Arduino 5V
The Joystick GND ⟶ Arduino Gnd
The Joystick X ⟶ Arduino A0


Arduino Sketch


#include <Stepper.h>

// Number of steps per revolution of INTERNAL motor in 4-step mode
#define STEPS_PER_MOTOR_REVOLUTION 48 

// Number of steps per revolution of OUTPUT SHAFT (= gear reduction; 2048 steps)
#define STEPS_PER_OUTPUT_REVOLUTION 48 * 64

// Declare 'small_stepper' variable
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 8, 10, 9, 11);

// Declare 'Steps2Take' variable
int  Steps2Take;
int joyX = A0;
int joyVal;
void setup()
{
// Nothing  (Stepper Library sets pins as outputs)
  Serial.begin(9600);
}

void loop()
{
   joyVal = analogRead(joyX);
  Serial.println( joyVal);

  Serial.println();
  Serial.println("2");
  if( (joyVal == 0) || (joyVal > 1000) ){
   Steps2Take  = -48;// -STEPS_PER_OUTPUT_REVOLUTION / 2;  // Rotate CW 1/2 turn
    if( joyVal > 1000) Steps2Take = Steps2Take *-1;
  small_stepper.setSpeed(500); 
  small_stepper.step(Steps2Take);
  delay(10);
  }


}

ความคิดเห็น