L298N ESP32
ENA D14
IN1 D27
IN2 D26
GND GND
Battery 6V (ใช้รางถ่านชาร์จ 18650 2 ก้อน)
GND -
12v +
DC motor
OUT1 +
OUT2 -
Potentiometer ESP32
+ Vin
GND GND
Signal D34
// Motor A
int motor1Pin1 = 27;
int motor1Pin2 = 26;
int enable1Pin = 14;
int speedMotor = 0;
int VOL = 34;
int data = 0;
// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
// int dutyCycle = 200;
void setup() {
// 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...");
delay(2000);
}
void loop() {
data = analogRead(VOL);
if (data < 2048){
int speedMotor = map(data, 0,2048,255,0);
Serial.print("speedMotor=");
Serial.println(speedMotor);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
ledcWrite(pwmChannel, speedMotor);
}else{
int speedMotor = map(data-2048, 0,2048,0,255);
Serial.print("speedMotor=");
Serial.println(speedMotor);
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
ledcWrite(pwmChannel, speedMotor);
}
delay(200);
}