Trang chủ Điện - Điện tửArduinoMạch Điều Khiển Động Cơ Bước ULN2003 Với Arduino – Tutorial

Mạch Điều Khiển Động Cơ Bước ULN2003 Với Arduino – Tutorial

bởi Admin
6 lượt xem 5 Phút đọc

Phần cứng cần thiết:

28BYJ48

ULN2003-XD                                                 ULN2003-XL 

Sơ đồ chân LCD Keypad Shield:

Mô tả:

Sơ đồ kết nối:

Thư viện:

  • Vào mục Sketch --> Include Library --> Manage Libraries

Code:

#include <AccelStepper.h>
#include <LiquidCrystal.h>
//LCD pin to Arduino
const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
 
//const int pin_Backlight = 10;
 
LiquidCrystal lcd(pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);
#define HALFSTEP 8
 
// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2 11     // IN2 on the ULN2003 driver 1
#define motorPin3  12     // IN3 on the ULN2003 driver 1
#define motorPin4  13     // IN4 on the ULN2003 driver 1
 
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
char ledState;
void setup() {
  stepper1.setMaxSpeed(1000);
  stepper1.setAcceleration(2000);
    //stepper1.setSpeed(2000);
    //stepper1.moveTo(2048);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print(" ULN2003   ");
  lcd.setCursor(0, 1);
  lcd.print("  STEP_MOTOR  ");
 
}//--(end setup )---
 
void loop() {
 
    //Change direction when the stepper reaches the target position
 
    int x;
  x = analogRead(0);
    if (x < 60) {
      lcd.setCursor(15, 0);
      lcd.print("+");
        if (stepper1.distanceToGo() == 0) {
          stepper1.moveTo(stepper1.currentPosition() - 4096);
        }
    }
    else if (x < 200) {
      lcd.setCursor(15, 0);
      lcd.print("+");
        if (stepper1.distanceToGo() == 0) {
          stepper1.moveTo(stepper1.currentPosition() - 1000);
        }
    }
    else if (x < 400) {
      lcd.setCursor(15, 0);
      lcd.print("-");
        if (stepper1.distanceToGo() == 0) {
          stepper1.moveTo(stepper1.currentPosition() + 1000);
        }
 
    }
    else if (x < 600) {
      lcd.setCursor(15, 0);
      lcd.print("-");
        if(stepper1.distanceToGo() == 0) {
          stepper1.moveTo(stepper1.currentPosition() + 4096);
        }
    }
    else if (x < 800) {
      lcd.print("Select");
    }
    //if (Serial.available() > 0)
    //{
    //  ledState = Serial.read();
 
    //  switch (ledState)
    //  {
    //  case '0':
    //  {
    //      if (stepper1.distanceToGo() == 0) {
    //          stepper1.moveTo(stepper1.currentPosition() + 4096);
    //      }
    //      lcd.setCursor(0, 0);
    //      lcd.print("ULN2003 STEPMOTOR");
    //      /*lcd.setCursor(0, 0);
    //      lcd.print("Position: +500");*/
    //     lcd.setCursor(0, 1);
    //      lcd.print("PROG: 0");
    //      ledState = ' ';
    //      break;
    //  }
    //  case '1':
    //  {
    //      if (stepper1.distanceToGo() == 0) {
    //          stepper1.moveTo(stepper1.currentPosition() - 4096);
    //      }
    //      lcd.setCursor(0, 0);
    //      lcd.print("Position: -500");
    //      lcd.setCursor(0, 1);
    //      lcd.print("PROG: 1");
    //      ledState = ' ';
    //      break;
 
    //  }
    //    case '2':
    //  {
    //      if (stepper1.distanceToGo() == 0) {
    //          stepper1.moveTo(-stepper1.currentPosition());
    //      }
    //      lcd.setCursor(0, 1);
    //      lcd.print("PROG: 2");
    //      ledState = ' ';
    //      break;
    //  }
    //  }
 
    //}
  stepper1.run();
}

Video

Để lại bình luận

Focus Mode

Đã phát hiện trình chặn quảng cáo

Vui lòng hỗ trợ chúng tôi bằng cách tắt tiện ích chặn quảng cáo (AdBlocker) trên trình duyệt của bạn khi truy cập trang web của chúng tôi.