Using a L298n to run a motor for X ammount of time

Hello, I'm trying to make a lock for my sand blasting cabinet, and already have an old linear gear rack with a DC motor attached, and would prefer that cool old mech vs using a servo.

The needs are modest.

press a button, motor runs one direction for X amount of time, press it again, it runs the other direction for X amount of time.

The diagram below is 90% what I need, My hang-up is adding the on time code to get the desired effect. Don't have much, but I can offer a few dollars for anyone that wants to help.

/*  Arduino DC Motor Control - PWM | H-Bridge | L298N  -  Example 01

    by Dejan Nedelkovski, www.HowToMechatronics.com
*/

#define enA 9
#define in1 6
#define in2 7
#define button 4

int rotDirection = 0;
int pressed = false;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(button, INPUT);
  // Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
}

void loop() {
  int potValue = analogRead(A0); // Read potentiometer value
  int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
  analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin

  // Read button - Debounce
  if (digitalRead(button) == true) {
    pressed = !pressed;
  }
  while (digitalRead(button) == true);
  delay(20);

  // If button is pressed - change rotation direction
  if (pressed == true  & rotDirection == 0) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    rotDirection = 1;
    delay(20);
  }
  // If button is pressed - change rotation direction
  if (pressed == false & rotDirection == 1) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    rotDirection = 0;
    delay(20);
  }
}

It may be too simple for that. What is the issue, you have no idea how to code it?

I could code that in about 5 minutes but I'm afraid I might get scooped by one of the eager beavers here...

pseudocode

repeat
{
if button press detected
  {
  turn motor on
  wait
  turn motor off
  reverse motor
  }
}

If you really want to contract it out, ask a mod to move this request to the pay for work section.

There needs to be connection between the 298N GND and the Arduino GND.

1 Like

Oh, I did not realize there was a coder for hire section.

Correct, I can kludge around a bit, But im better with vacuum tubes.
Since you were kind enough to reply swiftly, Id like to hire you (if you are willing) I have a few other projects I will likely need some work on.

Thanks Larry, Not my pic, but my test circuit will share grounds.

Are you sure that the ancient L298 motor driver will safely power your motor. The L298 output drops 2V to over 4V of the motor power supply and dissipates that power as heat. So the more current the less voltage gets to the motor. The L298 can supply up to 2A (they say), is the motor stall current less tban 2A?

Hey Fungus, The voltage drop is what im counting on, its a very small 180 size motor rated at 9 volts constant with a 80% duty cycle.

X will vary with motor speed, slower motor requires longer x.

Word of caution.

if (pressed == true & rotDirection == 0) {

  • Be aware what this actually does.

if (pressed == true & rotDirection == 0) {
versus
if (pressed == true && rotDirection == 0) {

These are not the same thing in all instances.

Yes sir, Ill be adjusting the potentiometer and time delay in said code to fine tune the movement.

As an example, try changing this to

  // If button is pressed - change rotation direction
  if (pressed == true  && rotDirection == 0) 
  {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    rotDirection = 1;
    // Run motor for 2 seconds
    delay(2000);
    // Turn off
    digitalWrite(in1, LOW;
  }

Do the same for both directions and that should do the trick!

1 Like

Might be a stupid question, but why don't you use a NE555 for this?

Not OP, but that code took me approximately 1 minute to write. It would take a lot more than that to find an NE555, calculate the required resistor/capacitor values, see if I have them and order if not and finally solder everything together.

Then we have to figure out the direction changing logic. Probably need an NE556 or another 555.

Or use a $2 arduino nano clone and do all that in less than 5 minutes. We're building qty 1, not 10,000.

1 Like

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