아두이노와 Processing

esp32 L293D DC모터 제어 실험

coding art 2021. 2. 2. 10:16
728x90

esp32 보드에서 L239D 칩을 사용하여 직류모터를 회전시켜 보도록 하자. L239D 칩 배선은 유사하지만 PWM 신호를 공급하는 방법이 아두이노 우노와는 다르다는 점에 유의하자.

 

esp32 wroom32 CPUGPIO pinout을 살펴보면 GND 와 전원 5V 3.3V를 제외한 모든 핀들이 100% PWM 핀임을 알 수 있다. 따라서 모터 입력을 위한 PWM 핀 선택이 자유롭다.

 

 

 

아래의 핀 다이아그램을 참조하면서 다음과 같이 배선 작업을 하자.

Vcc1esp32 3.3V를 공급한다.

enable1Pin14번 핀에 연결하며 ledcAttachPin 명령을 사용하여 pwmChannelenable1Pin 과 내부적으로 연결시킨다. 12번이나 13번 핀도 PWM 핀들이므로 Attach 가능하다.

motorPin114번 바로 옆의 27번 핀에 연결하고 전원이 ON되면 setup()에서 항상 HIGH 로 설정한다.

모터 + 전원 선으로부터의 배선은 L293D3번째 핀인 Output1 에 연결한다.

esp32 보드의 GND L293D4번째 핀인 GND 와 연결한다.

esp32 보드의 5V 전원을 L293D 8번째 핀인 Vcc2 핀에 연결한다.

L239D 9번째 핀에 esp32 보드의 3.3V핀과 연결한다.

모터의 나머지 배선을 L239D 12번 핀 GND에 연결한다. 4개의 GND가 있으므로 점퍼 선 거리를 감안하여 GNd 선택이 가능하다.

타이머 설정 및 Attach

esp32의 타이머 중 freq = 25kHz, pwmChannel = 0, resolution = 8 을 설정하자.

setup()에서 motorPin1 enable1PinpinMode 명령을 사용하여 OUTPUT 모드로 설정한다.

analogWrite 명령에 해당하는 ledcWritefreq, pwmChannel, resolution으로 setup한다.

ledcAttachPin 명령을 사용하여 enable1PinpwmChannel로 설정한다.

 

참고로 4핀 커넥터를 사용하는 25kHz PWM 쿨러에서 주파수 25kHz255에 해당 하도록 dutyCycle 값을 코딩한다.

다음은 실제 점퍼선 배선 사례이다.

//esp_motor_control_01

// Motor A

int motor1Pin = 17; //27,17

int enable1Pin = 5; //pwm입력핀,12,13,5

int rotating_time = 5000;

 

// Setting PWM properties

const int freq = 25000;

const int pwmChannel = 0;

const int resolution = 8;

 

void setup() {

// sets the pins as outputs:

pinMode(motor1Pin, OUTPUT);

pinMode(enable1Pin, OUTPUT);

pinMode(pwmChannel, OUTPUT);

// configure LED PWM functionalitites

ledcSetup(pwmChannel, freq, resolution);

 

// attach the channel to the GPIO to be controlled

ledcAttachPin(enable1Pin, pwmChannel);

digitalWrite(motor1Pin, HIGH);

digitalWrite(enable1Pin, HIGH);

Serial.begin(9600);

 

// testing

Serial.print("Testing DC Motor...");

}

 

void loop() {

int dutyCycle = 120;

ledcWrite(pwmChannel, dutyCycle);

Serial.println(dutyCycle);

delay(rotating_time);

 

dutyCycle = 255;

ledcWrite(pwmChannel, dutyCycle);

Serial.println(dutyCycle);

delay(rotating_time);

 

}