티스토리 뷰

728x90

M5Stack Korea인 WIZnet에서 운영하는 메이커 컨텐츠 커뮤니티 사이트의 후원을 받아서 작성되었습니다.

출처

M5Stack : 레고 Power function 모터 제어(L298N 사용)

L298N 모듈은 아두이노로 RC Car를 만들기 위해서 많이 사용하는 모터 드라이버 모듈로 정방향과 역방향으로 2개의 모터 제어가 가능하고, 속도 조절도 됩니다.

예제에서는 Lego Power function 모터를 L298N으로 제어하고 L298N은 M5Stack으로 명령을 실행하고 있습니다.

M5Go Port

PORT A : GPIO 21, 22 pin
PORT B : GPIO 26, 36 pin
PORT C : GPIO 16, 17 pin

배선

M-Motor의 경우 별도의 전원 연결이 필요없이, L298N에 C1, C2핀만 연결하면 됩니다.

  • ENA     IN1       IN2
  • 26pin   21pin     22pin

Lego Power function 연결 케이블 핀번호

Lego Power function M-Mortor 연결

L298N 연결

Lego Li-Battery Box 연결

M5Go의 Port A와 Port B에 Grove 캐이블 연결

M5Stack 아두이노 소스

예제는 26번 핀을 PWM제어로 속도를 설정하고, 정방향으로 회전시키고, 다시 역방향으로 회전 시켜보았습니다.

#include <M5Stack.h>

// Motor A
int motor1Pin1 = 21; 
int motor1Pin2 = 22; 
int enable1Pin = 26; 

// Setting PWM properties
const int freq = 10000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 150;

void setup() {

  // 초기화
  M5.begin(true, false, true);
  M5.Lcd.setTextFont(4);
  M5.Lcd.setCursor(40, 100);
  M5.Lcd.print("L298N Motor Driver");
  
  // sets the pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  
  // configure LED PWM functionalitites
  ledcSetup(pwmChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(enable1Pin, pwmChannel);

  Serial.begin(115200);

  // testing
  Serial.print("Testing DC Motor...");
}

void loop() {

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor forward with increasing speed
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  while (dutyCycle <= 255){
    ledcWrite(pwmChannel, dutyCycle);   
    Serial.print("Forward with duty cycle: ");
    Serial.println(dutyCycle);
    dutyCycle = dutyCycle + 5;
    delay(500);
  }
  dutyCycle = 255;

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor forward with increasing speed
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  while (dutyCycle > 150){
    ledcWrite(pwmChannel, dutyCycle);   
    Serial.print("Backwards with duty cycle: ");
    Serial.println(dutyCycle);
    dutyCycle = dutyCycle - 5;
    delay(500);
  }
  dutyCycle = 150;
}

실행 영상

M5Stack 물품 구매는 <네이버 검색/쇼핑에서 M5StackKorea>를 검색하시거나, M5Stack 공식 파트너인 <위즈네트 쇼핑몰: Shop.wiznet.io> 으로 접속하세요.

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함