How to control motor speed

Hello, I am a new user from Arduino and I am currently working on my Arduino line tracing project, and lately I have been getting problems as I couldn't control the speed of my motors. My power supply goes directly to my motor driver which is L298N (If I remember correctly), where both motors are connected, which is also where my Arduino uno gets power supply via motor driver through a buck converter. I need guidance please :>

Using "PWM" to change the duty cycle of the signal going to the ENA/ENB of the L298N you can "skid steer" the motors.

Here is a link to an introduction to PWM: https://docs.arduino.cc/learn/microcontrollers/analog-output/

Here is an example of "skid steering" https://wokwi.com/projects/422003442145826817

If we can see your code and a wiring diagram we may be able to help diagnose the problem

Do you actually want to control the motor speed or do you only want to vary the motor speed? There is a big difference in the two. To control the motor speed you need to measure the motor speed and automatically make power changes to maintain the desired speed.

Try a simple load on your PWM pins, like an LED.
A couple of LEDs with resistors will run off the Arduino board using the USB supply.
Also have a look at the IDE examples like Fading.
An Arduino board like the Uno has several PWM pins, 3, 5, 6, 9, 10, 11.
If there's space on the board, the pins have the tilde (squiggle) symbol.
If that works, you can rule out board problems.

Does this say that it was working correctly earlier on?
If so: wiggle bread board connections. One might be loose or badly connected...

Welcome Back!
Something is not correct, but at this point I can only guess, and that
guess would probably be wrong.

To help, we need to know exactly what you have. From your description
there are many possibilities.

Please post an annotated schematic showing exactly how everything is
wired. Be sure to show all power sources and their ratings.

A link to the technical data for each hardware device would also help.
There are millions of different motors available, and they do not all
behave the same way.

If the problem is intermittent, post a clear picture of your setup.

The L298 is a poor choice for a motor driver. It is an old and obsolete
part. It runs hot because it wastes a lot of energy as heat. It can drop
about 3V when driving a load, so a 5V motor supply may leave only about
2V at the motor.

1. Make the following setup (Fig-1):


Figure-1:

2. Upload te following sketch:
Sample Code: (tested few years ago)

void setup()
{
      pinMode(4, OUTPUT);
      pinMode(5, OUTPUT);
      digitalWrite(4, HIGH);
      digitalWrite(5, LOW);
}

void loop()
{
   int y = analogRead(A0);
   byte dC = map(y, 1023, 0, 255);
   analogwrite(9, dC);
   delay(1000);
}

3. Slowly rotate the Pot.
4. Check that M2 is turning at variable speed.

I want to regulate?

That would also require knowing the speed, don't you think?

  • Maybe revisit your schematic so it can be followed better ? :thinking:

Yes!

There are relative merits and demerits of both diagrams. Now, it is up to OP to decide which one convenient to him.

:upside_down_face:

  • I get dizzy when I stand on my head.

:face_with_spiral_eyes:

  • Correction, I cannot stand on my head any more.

1. On L298 Motor assembly, there OUT1, OUT2; OUT3, OUT4; there is nothing such as OUTx_Pos/Neg.

2. ENB pin is a logic pin (D6) and not a pwm pin (~PD6).

3. MotorPowerPos+; where is MotorPowerNeg-?

4. I have said Pot for potentionmeter, and you have said R1 for resistor 1 which is actually a Pot.

5. You have said `Header' -- actually, it is a Terminal.

6. You have shown RESET/ (bar above); it actually, RESET (no bar).

7. You have shown VIN; it is actually Vin.

Is the reference the L298N or the code? "ENB" on the L298N is the PWM input for channel two.

L298

As per Fig-1 of post #8, ENB is a logic pin.

Post #8 code does not use (arduino) D6. (arduino) D6 is just wired to ENB.

Right!

  • Oh my, it seems you might be touchy today.
    My point was, which surely you know, your schematic/WiringDiagram is upside down.

    • “there is nothing such as OUTx_Pos/Neg.”
      There is Pos/Neg as I labeled it so.
      If you look at the motor, there is a + and - terminal.
      When the motor is connected as in the my schematic, the user will get consistent motor direction CW/CCW every time they rewire it from scratch.
      Without Identifying the output terminals with a designator (as I have used, Pos/Neg) the user might wonder why the motor turns CW today then CCW tomorrow when they reconnect the motor backward.
  • ENB pin is a logic pin (D6) and not a pwm pin (~PD6).
    ?

  • “where is MotorPowerNeg?”
    GND, but you knew that.
    The user is surely capable of asking questions.

  • “ you have said R1 for resistor 1”
    R is a designator for components such as fixed and variable resistors. But you know that.

  • “You have shown RESET/“


    The bar above the RESET means when LOW the Arduino is put in the reset state. You knew this.

  • “You have shown VIN; it is actually Vin .”
    It is standard practise to use capital letters as it makes it easier for small printed schematics to be read.
    You might be unaware of this though.

  • Not sure where the vitriol is coming from. :thinking: