코드 검색

레슨 55-1: 푸시 버튼을 사용하여 단일 DC 모터 제어하기

레슨 55-1: 푸시 버튼을 사용하여 단일 DC 모터 제어하기

이 튜토리얼에서는 L298N 모터 컨트롤러 모듈, 푸시 버튼, 가변 저항(포텐셔미터)을 사용하여 단일 DC 모터를 제어하는 방법을 배웁니다. 버튼을 누르면 모터를 시계 방향 또는 반시계 방향으로 회전시키고, 정지시킬 수 있으며, 가변 저항을 사용하여 속도를 제어할 수 있습니다. 이 프로젝트는 Arduino 프로젝트에서 모터 제어를 이해하는 데 훌륭하며 하드웨어 인터페이싱에 대한 실습 경험을 제공합니다. 더 자세한 설명을 원하시면 이 튜토리얼과 함께 제공되는 비디오(비디오 0:45 지점)를 꼭 확인하세요.

L298N Motor Controller Module
L298N 모터 컨트롤러 모듈

하드웨어 설명

이 프로젝트의 주요 구성 요소는 L298N 모터 컨트롤러로, DC 모터의 방향과 속도를 제어할 수 있게 해줍니다. H-브리지 구성을 사용하여 모터가 양방향으로 회전할 수 있습니다. 이 모듈은 6~35볼트의 공급 전압이 필요하므로 다양한 모터 유형에 사용할 수 있습니다. 또한 PWM(펄스 폭 변조) 기능이 있어 속도 제어가 가능합니다. 모터의 방향 제어와 정지를 위해 푸시 버튼도 사용합니다. 가변 저항은 모터의 속도를 조절하는 데 도움이 됩니다. 이러한 구성 요소들이 함께 작동하여 Arduino 프로젝트에서 모터 작동을 관리하는 간단하면서도 효과적인 방법을 제공합니다.

데이터시트 세부 정보

제조업체 STMicroelectronics
부품 번호 L298N
로직/IO 전압 5 V
공급 전압 6–35 V (VS)
출력 전류 (채널당) 채널당 최대 2A (절대 최대), 연속 1A (일반)
피크 전류 (채널당) 3 A
PWM 주파수 권장 사항 5 kHz
입력 로직 임계값 0.8 V (낮음), 2.0 V (높음)
전압 강하 / RDS(on) / 포화 2 V (일반)
열 제한 150 °C
패키지 Multiwatt15
참고 사항 / 변형 최대 2개의 모터 제어 가능
  • 연속 작동을 위해 적절한 방열판을 사용하십시오.
  • 과열을 방지하려면 속도 제어에 PWM 핀을 사용하십시오.
  • 모터 드라이버의 접지를 Arduino 접지에 연결하십시오.
  • 손상을 방지하기 위해 공급 전압에 주의하십시오.
  • 안정적인 판독을 위해 푸시 버튼에 풀업 저항을 사용하십시오.
  • 역전원 문제를 방지하려면 프로그래밍하는 동안 전원 공급 장치를 분리하십시오.
Arduino wiring for L298N with pot 3push button to control 2 motors
Arduino wiring for L298N with pot 3push button to control 2 motors
Arduino wiring for L298N with pot 3push button to control single motor
Arduino wiring for L298N with pot 3push button to control single motor

배선 지침

Arduino wiring for L298N with pot 3push button to control single motor
단일 모터 제어를 위한 포텐셔미터 및 3개의 푸시 버튼이 있는 L298N Arduino 배선

L298N 모터 컨트롤러를 Arduino에 배선하려면 배터리의 양극 단자를 L298N 모듈의 VCC 단자에 연결하고 음극 단자를 GND에 연결하는 것으로 시작하십시오. L298N의 GND 단자도 Arduino의 GND에 연결하십시오. L298N의 5V 출력은 Arduino의 5V 입력에 연결할 수 있지만, 충돌을 피하기 위해 프로그래밍 후에만 연결하십시오. 다음으로, 모터를 L298N의 출력 단자에 연결하십시오. 모터 제어를 위해 입력 핀을 다음과 같이 배선하십시오: IN1을 Arduino의 핀 2에, IN2를 핀 4에, 활성화 핀 ENA를 핀 3에 연결합니다. 푸시 버튼의 경우 각 버튼의 한쪽을 GND에 연결하고 다른 쪽은 다음과 같이 연결합니다: 방향 버튼을 핀 8에, 정지 버튼을 핀 9에 연결합니다. 마지막으로 가변 저항을 연결합니다: 한쪽은 GND에, 다른 쪽은 5V에, 중간 핀은 Arduino의 A0에 연결합니다.

Arduino wiring for L298N with pot 3push button to control 2 motors
2개의 모터 제어를 위한 포텐셔미터 및 3개의 푸시 버튼이 있는 L298N Arduino 배선

코드 예제 및 설명

다음 코드 스니펫은 모터 제어를 초기화하고 핀을 설정합니다:


#include 
#define IN1 2
#define IN2 4
#define ENA 3 

const int motor1PushButtonDirection = 8;
const int motor1PushButtonStop = 9;

void setup() {
  Serial.begin(115200);
  motor.begin();
  pinMode(motor1PushButtonDirection, INPUT_PULLUP);
  pinMode(motor1PushButtonStop, INPUT_PULLUP);
}

이 발췌문에서 L298N 모터 제어에 필요한 라이브러리를 포함하고 모터 및 푸시 버튼용 핀을 정의합니다. `setup()` 함수는 시리얼 통신을 초기화하고 버튼 핀을 풀업 저항이 있는 입력으로 설정합니다. 다음으로, 루프에서 버튼 상태를 확인하고 모터를 제어합니다:


void loop() {
  updateState1();
  if(motor1StopState == HIGH) {
    motor.brake(motor1);     
  } else {
    motor.rotate(motor1, motor1Speed, motor1Direction);    
  }
  delay(pushButtonDelayTime);
}

여기서 `loop()` 함수는 푸시 버튼의 상태를 지속적으로 확인합니다. 정지 버튼을 누르면 모터가 브레이크를 걸고, 그렇지 않으면 지정된 속도와 방향으로 회전합니다. `updateState1()` 함수는 버튼 상태를 읽고 모터 방향을 업데이트합니다:


void updateState1() {
  if(digitalRead(motor1PushButtonDirection) == LOW) {
    motor1Direction = (motor1Direction == CW) ? CCW : CW;
    Serial.println("방향 변경됨");
  }  
  if(digitalRead(motor1PushButtonStop) == LOW) {
    motor1StopState = 1 - motor1StopState; // 시작/정지 토글
  }  
}

이 함수는 방향 버튼을 누르면 모터 방향을 토글하고, 정지 버튼을 누르면 정지 상태를 토글합니다. 전체 코드는 기사 아래에 로드되므로 참고하세요.

시연 / 기대 효과

프로그램을 실행하면 푸시 버튼과 가변 저항을 사용하여 모터의 방향과 속도를 제어할 수 있습니다. 방향 버튼을 누르면 시계 방향과 반시계 방향 회전이 전환되고, 정지 버튼은 모터를 시작하거나 정지시킵니다(비디오 12:30 참조). 가변 저항 설정에 주의하세요. 너무 낮게 설정하면 모터가 시작되지 않을 수 있습니다.

챕터

  • 소개 – 0:00
  • 하드웨어 개요 – 1:30
  • 배선 지침 – 3:45
  • 코드 설명 – 6:00
  • 시연 – 12:00

이미지

L298N Motor Controller Module
L298N Motor Controller MOdule
Arduino wiring for L298N with pot 3push button to control single motor
Arduino wiring for L298N with pot 3push button to control single motor
Arduino wiring for L298N with pot 3push button to control 2 motors
Arduino wiring for L298N with pot 3push button to control 2 motors
485-Lesson 55: Controlling a DC motor using push buttons
언어: C++
/*
 * Lesson 55: Controlling DC Motors with 2 push buttons with L298N
 * Library Example for L298N Module to control with 2 push buttons
 * One Push button is used for CW/CCW
 * and one push button is used to stop the motor
 Download and resource page https://robojax.com/RJT506
 Watch video for full details of this code: https://youtu.be/diS_WkUdU-8
 * 
 * Written by Ahmad Shamshiri on October 20, 2019 at 14:58
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
 * 
 * 
 * Get this code and other Arduino codes from Robojax.com

If you found this tutorial helpful, please support me so I can continue creating
content like this. 

or make a donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define IN1 2
#define IN2 4
#define ENA 3 // ~this pin must be PWM enabled pin

const int CCW = 2; // do not change
const int CW  = 1; // do not change
#define motor1 1 // do not change
#define motor2 2 // do not change
const int pushButtonDelayTime =200;


//*** Push buttons for motor 1 started
int motor1Speed =60;// speed of motor 1 in % (60 means 60%)
const int motor1PushButtonDirection = 8;// push button for direction change
const int motor1PushButtonStop = 9;// push button for START/STOP
const int motor1Minimum =15;//30% is minimum for motor 1
// do not change below this line
int motor1Direction =CCW;//
int motor1StopState=HIGH;//Stope state of motor 1
//*** Push buttons for motor 1 started  ended
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



// use the line below for single motor
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, true);

// use the line below for two motors
//Robojax_L298N_DC_motor motor(IN1, IN2, ENA, IN3, IN4, ENB, true);

void setup() {
  Serial.begin(115200);
  motor.begin();

  //L298N DC Motor by Robojax.com
  pinMode(motor1PushButtonDirection, INPUT_PULLUP);
  pinMode(motor1PushButtonStop, INPUT_PULLUP);

   
}


void loop() {
  updateState1();//read all push 1 buttons

  if(motor1StopState ==HIGH)
  {
    motor.brake(motor1);     
  }else{
    motor.rotate(motor1, motor1Speed, motor1Direction);    
  }
  //motor.demo(1);

  //motor.rotate(motor1, motor1Speed, CW);//run motor1 at 60% speed in CW direction
 delay(pushButtonDelayTime);
  
}//

/*
 * 
 * updateState1()
 * @brief reads push buttons and updates values
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on October 13, 2019 in Ajax, Ontario, Canada
 */
void updateState1()
{

  if(digitalRead(motor1PushButtonDirection) ==LOW){
      if(motor1Direction ==CW)
      {
        motor1Direction =CCW;// 
        Serial.println("*****Now CCW"); 
      }else{
        motor1Direction =CW;//
        Serial.println("*****Now CW");  
      }

  }  

 
  if(digitalRead(motor1PushButtonStop) ==LOW)
  {
   motor1StopState =1-motor1StopState;// toggle Star/stop
  }  
}//updateState end
486-Lesson 55: Controlling 2 DC Motors with push buttons with L298N
언어: C++
/*
 * Lesson 55: Controlling 2 DC Motors with push buttons with L298N 
 * Library Example for L298N Module to control with 2 push buttons
 * One push button is used for CW/CCW  
 and one push button is used to stop the motor
 * (this code is to control 2 motors). See the first code for a single motor
  Download and resource page https://robojax.com/RJT506

 Watch video for full details of this code: https://youtu.be/diS_WkUdU-8
 * 

 * 
 * Written by Ahmad Shamshiri on October 20, 2019   at 14:58
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
 * 
 * 
 * Get this code and other Arduino codes from Robojax.com


If you found this tutorial helpful, please support me so I can continue creating 
content like this. 

or make a donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define IN1 2
#define IN2 4
#define ENA 3 // this pin must be PWM enabled pin

// motor 2 settings
#define IN3 7
#define IN4 6
#define ENB 5 // this pin must be PWM enabled pin

const int CCW = 2; // do not change
const int CW  = 1; // do not change
#define motor1 1 // do not change
#define motor2 2 // do not change
const int pushButtonDelayTime =200;


//*** Push buttons for motor 1 started 
int motor1Speed =60;// speed of motor 1 in % (60 means 60%)
const int motor1PushButtonDirection = 8;// push button for direction change
const int motor1PushButtonStop = 9;// push button for START/STOP
const int motor1Minimum =15;//30% is minimum for motor 1
// do not change below this line
int motor1Direction =CCW;//
int motor1StopState=HIGH;//Stop state of motor 1
//*** Push buttons for motor 1 started  ended
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

//*** Push buttons for motor 2 started 
int motor2Speed =60;// speed of motor 2 in % (60 means 60%)
const int motor2PushButtonDirection = 10;// push button for direction change
const int motor2PushButtonStop = 11;// push button for START/STOP
const int motor2Minimum =15;//30% is minimum for motor 2
// do not change below this line
int motor2Direction =CCW;//
int motor2StopState=HIGH;//Stop state of motor 2
//*** Push buttons for motor 2 started  ended
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


// use the line below for 1 motor
//Robojax_L298N_DC_motor motor(IN1, IN2, ENA,true);

// use the line below for two motors
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, IN3, IN4, ENB, true);

void setup() {
  Serial.begin(115200);
  motor.begin();

  //L298N DC Motor by Robojax.com
  // push buttons for motor 1
  pinMode(motor1PushButtonDirection, INPUT_PULLUP);
  pinMode(motor1PushButtonStop, INPUT_PULLUP);


  //L298N DC Motor by Robojax.com
  // push buttons for motor 2
  pinMode(motor2PushButtonDirection, INPUT_PULLUP);
  pinMode(motor2PushButtonStop, INPUT_PULLUP);
   
}


void loop() {
  updateState1();//read push buttons value for motor 1
  updateState2();//read push buttons value for motor 2
  
  if(motor1StopState ==HIGH)
  {
    motor.brake(1);     
  }else{
    motor.rotate(motor1, motor1Speed, motor1Direction);    
  }

  if(motor2StopState ==HIGH)
  {
    motor.brake(2);     
  }else{
    motor.rotate(motor2, motor2Speed, motor2Direction);    
  }  
  //motor.demo(1);

  //motor.rotate(motor1, motor1Speed, CW);//run motor1 at 60% speed in CW direction
 delay(pushButtonDelayTime);
  
}//

/*
 * 
 * updateState1()
 * @brief reads push buttons and updates values
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on Oct 13, 2019 in Ajax, Ontario, Canada
 */
void updateState1()
{

  if(digitalRead(motor1PushButtonDirection) ==LOW){
      if(motor1Direction ==CW)
      {
        motor1Direction =CCW;// 
        Serial.println("***** Motor 1 Now CCW"); 
      }else{
        motor1Direction =CW;//
        Serial.println("***** Motor 1 Now CW");  
      }

  } //if for direction push button

 
  if(digitalRead(motor1PushButtonStop) ==LOW)
  {
   motor1StopState =1-motor1StopState;// toggle Start/stop
  }// if for start/stop push button 
  
}//updateState1 end

/*
 * 
 * updateState2()
 * @brief reads push buttons and updates values
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on Oct 13, 2019 in Ajax, Ontario, Canada
 */
void updateState2()
{

  if(digitalRead(motor2PushButtonDirection) ==LOW){
      if(motor2Direction ==CW)
      {
        motor2Direction =CCW;// 
        Serial.println("***** Motor 2 Now CCW"); 
      }else{
        motor2Direction =CW;//
        Serial.println("***** Motor 2 Now CW");  
      }

  } //if for direction push button 

 
  if(digitalRead(motor2PushButtonStop) ==LOW)
  {
   motor2StopState =1-motor2StopState;// toggle Start/stop
  }// if for start/stop push button 
    
}//updateState2 end

필요할 수 있는 것들

파일📁

다른 파일들