Arduino Uno - L298N

OS/Arduino 2017.12.24 02:44 Posted by 파란크리스마스

출처 : Tutorial - L298N Dual Motor Controller Module 2A and Arduino - Tronixlabs Australia

배선

L298N 스펙

  1. DC motor 1 "+" or stepper motor A+
  2. DC motor 1 "-" or stepper motor A-
  3. 12V jumper - remove this if using a supply voltage greater than 12V DC. This enables power to the onboard 5V regulator
  4. Connect your motor supply voltage here, maximum of 35V DC. Remove 12V jumper if >12V DC
  5. GND
  6. 5V output if 12V jumper in place, ideal for powering your Arduino (etc)
  7. DC motor 1 enable jumper. Leave this in place when using a stepper motor. Connect to PWM output for DC motor speed control.
  8. IN1
  9. IN2
  10. IN3
  11. IN4
  12. DC motor 2 enable jumper. Leave this in place when using a stepper motor. Connect to PWM output for DC motor speed control.
  13. DC motor 2 "+" or stepper motor B+
  14. DC motor 2 "-" or stepper motor B-

L298N 제어 소스

//모터A 컨트롤
int enA = 6;
int IN1 = 7;
int IN2 = 5;
  
//모터B 컨트롤
int enB = 3;
int IN3 = 4;
int IN4 = 2;
 
#define MOTER_SPEED 255 // MAX 255
  
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
}
  
void loop() {
  analogWrite(enA, MOTER_SPEED);
  analogWrite(enB, MOTER_SPEED);

  /*
  digitalWrite(enA,HIGH);
  digitalWrite(enB,HIGH);
  */
  bothMotorStart(); // 전진
  delay(3000);
  turnLeft(); // 왼쪽으로 턴
  delay(3000);
  turnRight(); // 오른쪽으로 턴
  delay(3000);
  motorA_Rotation(); // 왼쪽만 정방향
  delay(3000);
  motorB_Rotation(); // 오른쪽만 정방향
  delay(3000);
  motorA_Reverse(); // 왼쪽만 역방향
  delay(3000);
  motorB_Reverse(); // 오른쪽만 역방향
  delay(3000);
  stopAllMotor(); // 정지
  delay(3000);
}
  
//모터A,B 정회전
void bothMotorStart()
{
    digitalWrite(IN1,HIGH);
    digitalWrite(IN2,LOW);
    digitalWrite(IN3,HIGH);
    digitalWrite(IN4,LOW);
}
  
//모터A,B Stop
void stopAllMotor()
{
    digitalWrite(IN1,LOW);
    digitalWrite(IN2,LOW);
    digitalWrite(IN3,LOW);
    digitalWrite(IN4,LOW);
}
  
//모터A 역회전, 모터B 정회전
void turnLeft()
{
    digitalWrite(IN1,LOW);
    digitalWrite(IN2,HIGH);
    digitalWrite(IN3,HIGH);
    digitalWrite(IN4,LOW);
}
  
//모터A 정회전, 모터B 역회전
void turnRight()
{
    digitalWrite(IN1,HIGH);
    digitalWrite(IN2,LOW);
    digitalWrite(IN3,LOW);
    digitalWrite(IN4,HIGH);
}
  
//모터A 정회전, 모터B Stop
void motorA_Rotation()
{
    digitalWrite(IN1,HIGH);
    digitalWrite(IN2,LOW);
    digitalWrite(IN3,LOW);
    digitalWrite(IN4,LOW);
}
  
//모터A Stop, 모터B 정회전
void motorB_Rotation()
{
    digitalWrite(IN1,LOW);
    digitalWrite(IN2,LOW);
    digitalWrite(IN3,HIGH);
    digitalWrite(IN4,LOW);
}
  
//모터A 역회전, 모터B Stop
void motorA_Reverse()
{
    digitalWrite(IN1,LOW);
    digitalWrite(IN2,HIGH);
    digitalWrite(IN3,LOW);
    digitalWrite(IN4,LOW);
}
  
//모터A Stop, 모터B 역회전
void motorB_Reverse()
{
    digitalWrite(IN1,LOW);
    digitalWrite(IN2,LOW);
    digitalWrite(IN3,LOW);
    digitalWrite(IN4,HIGH);
}