Controlling PWM

Does anyone have ever used or know the ZK-5AD Motor Module which is also known as the L298N 5A? If so, how can I control the motor speed using it, cause it looks like there is no dedicated PWM pinout.
If you can send a example code also, it would be much appreciated!

Product can be seen here:

I'm not sure what the problem is. The table in your link clearly shows how speed control works. You just apply the PWM signal (i.e. analogWrite) to either the forward D0/D2 or reverse D1/D3 connection for the motor in question. Have you tried anything like that?

Steve

So I don't need to connect anything else other than the D0, D1, D2, and D3? And should I connect these four to a digital or analog pin?

I only tried putting the four (D0-D3) in the digital pins and turning them on without adding analogWrite for controlling the speed.

If you could also perhaps put an example code it would be really appreciated.

I haven't used an Arduino for months now and I seem to forgot alot about it, my apologies.

WHICH Arduino?

JCA34F:
WHICH Arduino?

What do you mean by that?

Uno?

MEGA?

Due?

D!Mini?

ESP8266?

ESP32?

STM32 Bluepill?

Roll your own?

etc.

You need to connect to PWM pins and use analogWrite(). PWM pins are the digital pins with a ~ next to them and they're not the same ones on all the different Arduinos.

If you have already got the motor running at full speed and off then post the code you used for that and we'll have something to help with.

Steve

Idahowalker:
Uno?

MEGA?

Due?

D!Mini?

ESP8266?

ESP32?

STM32 Bluepill?

Roll your own?

etc.

I'm using an Arduino Mega 2560.

slipstick:
You need to connect to PWM pins and use analogWrite(). PWM pins are the digital pins with a ~ next to them and they're not the same ones on all the different Arduinos.

If you have already got the motor running at full speed and off then post the code you used for that and we'll have something to help with.

Steve

I have the D0, D1, D2, and D3 pins from the motor module connected to the PWM pins of the Arduino.
Here's the code that I'm using: (I'm using it with an ultrasonic sensor)

int trigPin = 9;      // trig pin of HC-SR04
int echoPin = 10;     // Echo pin of HC-SR04

long duration, distance;

void setup() {
  
  delay(random(500,2000));   // delay for random time
  Serial.begin(9600);
  pinMode(4, OUTPUT);      // set Motor pins as output
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  
  pinMode(trigPin, OUTPUT);         // set trig pin as output
  pinMode(echoPin, INPUT);          //set echo pin as input to capture reflected waves
}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);   
  digitalWrite(trigPin, HIGH);     // send waves for 10 us
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH); // receive reflected waves
  distance = duration / 58.2;   // convert to distance
  delay(10);
    // If you dont get proper movements of your robot then alter the pin numbers
  if (distance > 16)            
  {
    digitalWrite(4, LOW);  // move forward
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);                                
    digitalWrite(7, HIGH);                                                       
  }

  if (distance < 15)
  {
    digitalWrite(4, LOW);  //Stop                
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);                                
    digitalWrite(7, LOW);
    delay(1500);
    digitalWrite(4, HIGH); //movebackword         
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);                                
    digitalWrite(7, LOW);
    delay(300);
    digitalWrite(4, LOW);  //Stop                
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);                                
    digitalWrite(7, LOW);  
    delay(1500);  
    digitalWrite(4, LOW);  //move the other direction       
    digitalWrite(5, HIGH);   
    digitalWrite(6, LOW);                                 
    digitalWrite(7, LOW);  
    delay(300);
    digitalWrite(4, LOW);  //Stop                
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);                                
    digitalWrite(7, LOW);  
    delay(1500); 
  }

}

The change should be easy then. Wherever you have a digitalWrite(somepin, HIGH) change it to analogWrite(somepin, speed) where speed is the value 0-255 that sets the speed. Note 0 is equivalent to digitalWrite LOW and 255 is equivalent to HIGH.

It's up to you how you control the speed if you need anything other than fixed speeds.

Steve

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.