Control 12DCv Moter Using L298n and esp-32

Hello everyone

I am a beginner and trying to learn things in IoT.

I just want to control 12v DC Moter using L298n Moter Driver with ESP-32

Here is my coding part

int enA = 23;
int in1 = 18;
int in2 = 19;

void setup()
{
  Serial.begin(9600);

  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  //analogWrite (enA, 255)

}

void loop()
{ 
    // move forword 
    digitalWrite(in1, HIGH); 
    digitalWrite(in2, LOW);
    delay(2000);
    
    // STOP
    digitalWrite(in1, lOW); 
    digitalWrite(in2, LOW);
    delay(2000);
   
 // move forword 
    digitalWrite(in1, LOW); 
    digitalWrite(in2, HIGH);
    delay(2000);
    
    // STOP
    digitalWrite(in1, lOW); 
    digitalWrite(in2, LOW);
    delay(2000);
   
}

So my problem is or we can say my question is i just want only 50% of the speed consisting

how to add function or code for this

analog value of speed
top = 255 , 100%
lower = 0 , 0%

but i just want only 50%

So normally you pwm the enable pin. If you want the motor at half speed you would use
a 50% duty cycle so middle= 127 = 50%.

1 Like

but sir how can you help in coding part

Normally you would use

analogWrite (enA, 255)

for full speed and

analogWrite (enA, 0)

to stop the motor, so to get 50% speed you would use

analogWrite (enA, 127)

Note that the characteristics of the motor may not be linear so that a value of 127 may not be exactly 50% speed

Another consideration is that the ESP32 output voltage is 3V3 rather than 5V which may cause problems if the motor driver expects a 5V input

How is the motor powered ?

1 Like

analogWrite(enA, 127);

1 Like

but i got error

exit status 1
'analogWrite' was not declared in this scope

int enA = 23;
int in1 = 18;
int in2 = 19;

void setup()
{
  Serial.begin(9600);

  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite (enA, 255)

}

void loop()
{ 
    // move forword 
    digitalWrite(in1, HIGH); 
    digitalWrite(in2, LOW);
    delay(2000);
    
    // STOP
    digitalWrite(in1, lOW); 
    digitalWrite(in2, LOW);
    delay(2000);
   
 // move forword 
    digitalWrite(in1, LOW); 
    digitalWrite(in2, HIGH);
    delay(2000);
    
    // STOP
    digitalWrite(in1, lOW); 
    digitalWrite(in2, LOW);
    delay(2000);
   
}

can you help me in coding part too

analogWrite() was only recently added to the functions available for the ESP32

You need to update your ESP32 board definition to the latest. This is what I have

I forgot about the esp32 not having that function in the past. Glad they updated it.

Arduino: 1.8.12 (Windows 10), Board: "ESP32 Wrover Module, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), QIO, 80MHz, 921600, None"

C:\Users\jamsa\OneDrive\Desktop\sketch_jan28a\sketch_jan28a.ino: In function 'void setup()':

sketch_jan28a:16:24: error: 'analogWrite' was not declared in this scope

analogWrite (enA, 255);

                    ^

exit status 1
'analogWrite' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Which version of the ESP32 board definitions do you have installed ?

See post #8

1 Like

currently, i am using the 1.0.6 esp32 board version but when i am trying latest version of board i got

Arduino: 1.8.12 (Windows 10), Board: "ESP32 Wrover Module, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), QIO, 80MHz, 921600, None"

C:\Users\jamsa\OneDrive\Desktop\sketch_jan28a\sketch_jan28a.ino: In function 'void setup()':

sketch_jan28a:16:24: error: 'analogWrite' was not declared in this scope

analogWrite (enA, 255);

                    ^

exit status 1
'analogWrite' was not declared in this scope

Index error: could not find referenced tool name=avrdude version=6.3.0-arduino17or18 packager=arduino
Index error: could not find referenced tool name=avrdude version=6.3.0-arduino17or18 packager=arduino

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

If you do not want to update your esp-arduino core then look at the example code
for the LEDcSoftwareFade example. It shows how to control analog output (pwm).

Play around with that and that should get you going.

Which version did you try ?

as you said 2.02

Would it be possible to use software PWM on an older ESP32? Like this lib:

2.0.2

As already stated, PWM in ESP32 works different than in Arduino.

You should configure a channel (ledcSetup), then associate this channel to a pin (ledcAttachPin) and then write the "speed" to the channel (ledcWrite).

My board here doesn´t have an Enable pin (or it´s hide and always on) so I write the speed directly into the channel that is receiving energy. For example:

ledcSetup(15, 5000, 10); //Configures channel 15 with 5000 Hz frequency and 10 bits resolution

At 10 bit resolution, you can set "speed" from 0 to 1024. So 50% would be 512. However, it may be not enough to your motor. I suggest to begin with higher values (like 900) and then decrease untill you see the limit where it still moves.

You need to detach the pin from the channel each time you change directions. So, the code parts should be like this:

//setup
ledcSetup(15, 5000, 10);

//loop

//Move forward
  ledcDetachPin(in2);
  digitalWrite(in2, LOW);
  ledcAttachPin(in1, 15);    //Attach PWM signal (channel 15) to pin in1
  ledcWrite(15,800);           //Sends a signal to channel 15
  delay(1000);


//Move backward
  ledcDetachPin(in1);
  digitalWrite(in1, LOW);
  ledcAttachPin(in2, 15);
  ledcWrite(15,800);
  delay(1000);

//STOP
  ledcDetachPin(in1);
  ledcDetachPin(in2);
  digitalWrite(ET, LOW);
  digitalWrite(DF, LOW);
  delay(1000);
}


1 Like

sure I will try this and get back too you sir