Skip to content

Instantly share code, notes, and snippets.

@YuruPuro
Created March 25, 2023 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuruPuro/193fb9f3ff15ae60525a53f04ce2f9e9 to your computer and use it in GitHub Desktop.
Save YuruPuro/193fb9f3ff15ae60525a53f04ce2f9e9 to your computer and use it in GitHub Desktop.
// Sample sketch for operating the stepping motor 28BYJ-48 and motor driver UKN2003A using the Arduino standard library
#include <Stepper.h>
#define PIN_BLUE 4
#define PIN_PINK 5
#define PIN_YELLOW 6
#define PIN_ORANGE 7
#define MOTOR_STEPS (2048)
Stepper stepper(MOTOR_STEPS, PIN_BLUE, PIN_YELLOW, PIN_PINK, PIN_ORANGE);
void setup() {
stepper.setSpeed(10);
}
void loop() {
// Rotate the motor 90 degrees to the left
stepper.step(512);
delay(1000) ;
// Rotate the motor 180 degrees to the right
stepper.step(-1024);
delay(1000) ;
// Rotate the motor 270 degrees to the left
stepper.step(1536);
delay(1000) ;
// Rotate the motor 360 degrees to the right
stepper.step(-2048);
delay(1000) ;
}
// Sample sketch for direct operation of stepping motor 28BYJ-48 and motor driver UKN2003A
#define PIN_BLUE 7
#define PIN_PINK 6
#define PIN_YELLOW 5
#define PIN_ORANGE 4
// Defines 4 types of operation patterns to rotate the stepping motor clockwise
static uint8_t Pattern0R[] = {2,0x08,0x02 } ;
static uint8_t Pattern1R[] = {4,0x08,0x04,0x02,0x01 } ;
static uint8_t Pattern2R[] = {4,0x0C,0x06,0x03,0x09 } ;
static uint8_t Pattern3R[] = {8,0x08,0x0C,0x04,0x06,0x02,0x03,0x01,0x09 } ;
// Defines 4 types of operation patterns to rotate the stepping motor to the left
static uint8_t Pattern0L[] = {2,0x04,0x01 } ;
static uint8_t Pattern1L[] = {4,0x01,0x02,0x04,0x08 } ;
static uint8_t Pattern2L[] = {4,0x03,0x06,0x0C,0x09 } ;
static uint8_t Pattern3L[] = {8,0x09,0x01,0x03,0x02,0x06,0x04,0x0C,0x08 } ;
void setup() {
pinMode(PIN_BLUE,OUTPUT) ;
pinMode(PIN_PINK,OUTPUT) ;
pinMode(PIN_YELLOW,OUTPUT) ;
pinMode(PIN_ORANGE,OUTPUT) ;
digitalWrite(PIN_BLUE,LOW) ;
digitalWrite(PIN_PINK,LOW) ;
digitalWrite(PIN_YELLOW,LOW) ;
digitalWrite(PIN_ORANGE,LOW) ;
}
void stepMove(uint8_t *PTN,int STEPS,int SPEED) {
int n = PTN[0] ;
for (int r=0;r<STEPS;r++) {
for (int i=1;i<=n;i++) {
digitalWrite(PIN_BLUE,(((PTN[i] & 0x08) != 0) ? HIGH :LOW)) ;
digitalWrite(PIN_PINK,(((PTN[i] & 0x04) != 0) ? HIGH :LOW)) ;
digitalWrite(PIN_YELLOW,(((PTN[i] & 0x02) != 0) ? HIGH :LOW)) ;
digitalWrite(PIN_ORANGE,(((PTN[i] & 0x01) != 0) ? HIGH :LOW)) ;
delay(SPEED) ;
}
}
}
void loop() {
// 1 rotation by changing the control pattern every 90 degrees
stepMove(Pattern0R,128,25);
delay(1000) ;
stepMove(Pattern1R,128,10);
delay(1000) ;
stepMove(Pattern2R,128,5);
delay(1000) ;
stepMove(Pattern3R,128,1);
delay(1000) ;
// 1 rotation by changing the control pattern every 90 degrees
stepMove(Pattern0L,128,25);
delay(1000) ;
stepMove(Pattern1L,128,10);
delay(1000) ;
stepMove(Pattern2L,128,5);
delay(1000) ;
stepMove(Pattern3L,128,1);
delay(1000) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment