Need help to l298n

Hi
I have made this code, its works on a bord
Simulated by led as motor, but when I put it together with motor on a l298n bord, I thing happen.

Do I need another code for the motor driver Modul? fed skrift

const int motor1ForwardPin = 2;
const int motor1BackwardPin = 3;
const int motor2Pin = 4;
const int contact1Pin = 5;
const int contact2Pin = 6;

void setup() {
  // put pin-mode for motorer og contact
  pinMode(motor1ForwardPin, OUTPUT);
  pinMode(motor1BackwardPin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(contact1Pin, INPUT_PULLUP);
  pinMode(contact2Pin, INPUT_PULLUP);



  
  // motor 1 forward, until contact 1 hits
  while (digitalRead(contact1Pin) == LOW) {
    digitalWrite(motor1ForwardPin, HIGH);
    digitalWrite(motor1BackwardPin, LOW);
  digitalWrite(motor2Pin, HIGH);

  }



  // motor 1 drivning bag until concact 2 har hit
  while (digitalRead(contact2Pin) == LOW) {
    digitalWrite(motor1ForwardPin, LOW);
    digitalWrite(motor1BackwardPin, HIGH);
  }

  // Stop motor 1 and 2 when contact 2 are push
  
  digitalWrite(motor1ForwardPin, LOW);
  digitalWrite(motor1BackwardPin, LOW);
   digitalWrite(motor2Pin, LOW);
  

}


void loop() {
}

Please show a circuit diagram, including all power sources.

7,2 volt on l298n, ground from uno to l208n, 5 volt from uno to l298n, uno pin 2-3-4 on in1-in-2-in3, contact 1/2 to uno bord

scrap that l298n, it's just junk from an ear long gone. The datasheet says absolute minimum Vs = Vss + 2.5V, so your 7.2V is 0.3v too low to make it work.

Follow this tutorial and it will work

You can use the 7.4V instead of the 12V used in the tutorial

It has run with another code so I know it’s works

Super thank you

I think you are missing a "motor1Pin" I see, only one motor.

Your sketch is good. Move the motor control into the loop() function like this...

const int motor1ForwardPin = 2;
const int motor1BackwardPin = 3;
const int motor2Pin = 4;
const int contact1Pin = 5;
const int contact2Pin = 6;

void setup() {
  // put pin-mode for motorer og contact
  pinMode(motor1ForwardPin, OUTPUT);
  pinMode(motor1BackwardPin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(contact1Pin, INPUT_PULLUP);
  pinMode(contact2Pin, INPUT_PULLUP);

}
void loop() {
  // motor 1 forward, until contact 1 hits
  while (digitalRead(contact1Pin) == LOW) {
    digitalWrite(motor1ForwardPin, HIGH);
    digitalWrite(motor1BackwardPin, LOW);
    digitalWrite(motor2Pin, HIGH);
  }

  // motor 1 drivning bag until concact 2 har hit
  while (digitalRead(contact2Pin) == LOW) {
    digitalWrite(motor1ForwardPin, LOW);
    digitalWrite(motor1BackwardPin, HIGH);
    digitalWrite(motor2Pin, HIGH);
  }

  // Stop motor 1 and 2 when contact 2 are push
  digitalWrite(motor1ForwardPin, LOW);
  digitalWrite(motor1BackwardPin, LOW);
  digitalWrite(motor2Pin, LOW);
}
diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -78,
      "left": 150.6,
      "rotate": 90,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -49.2,
      "left": 150.6,
      "rotate": 90,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": -128.4,
      "left": 119.4,
      "attrs": { "color": "blue", "flip": "1" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -61,
      "left": -28.8,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn2",
      "top": -109,
      "left": -28.8,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo1",
      "top": -67.2,
      "left": 192,
      "attrs": { "text": "FWD" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo2",
      "top": -38.4,
      "left": 192,
      "attrs": { "text": "REV" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo3",
      "top": -96,
      "left": -67.2,
      "attrs": { "text": "FWD" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo4",
      "top": -48,
      "left": -67.2,
      "attrs": { "text": "REV" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo5",
      "top": -144,
      "left": 124.8,
      "attrs": { "text": "MOT" }
    }
  ],
  "connections": [
    [ "nano:2", "led1:A", "green", [ "v0" ] ],
    [ "nano:GND.1", "led1:C", "black", [ "v0" ] ],
    [ "nano:GND.1", "led3:C", "black", [ "v0" ] ],
    [ "nano:4", "led3:A", "green", [ "v0" ] ],
    [ "nano:3", "led2:A", "green", [ "v0" ] ],
    [ "nano:GND.1", "led2:C", "black", [ "v0" ] ],
    [ "nano:GND.2", "btn1:2.r", "black", [ "v0" ] ],
    [ "nano:6", "btn1:1.r", "green", [ "v0" ] ],
    [ "nano:GND.2", "btn2:2.r", "black", [ "v0" ] ],
    [ "nano:5", "btn2:1.r", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}

motors

thank you, but if I put it in loop then it will kept going, I only need it to go 1 time and then stop.
if I put it in loop then when it hits contact 2 then I start over.

on your simulator its not they I want it to be, I need it to start motor 1 and 2 when the power is on and then it hit contact 1 then motor 1 drive back while motor 2 run's the same way and when it hits concact 2 bouth motor stop and that's it.

i was thinking about if it is the wiring that is wrong, since its not working with the l298n but working with out l298n just uno with led and power board, so I take the 5 volt from uno to the power board, ground to power board were the 2 contacts are plugs in and the 3 led one simulate motor 1 forward 1 for motor 1 bag and motor 3 allows on.

but if I take out the in pins and put on l298n on en1 en2 en3, the power 5 volt from uno to l298n and splits from l298n 5 volt til power board for the concact's and 7,2 v in on l298n its doesn't work.

any idea?

If it did not work when you used the code and diagram from the tutorial, then I suspect that you have a bad connection or your batteries are dead, or the L298 is bad.

it work with the code from the tutorial, but not my code with l298n, it work with out so I youse led as motors simulation.
can't youse that much from tutorial

That's because your code is wrong and the code in the tutorial is correct.
Exactly what are you trying to do?

i have a item that I want to go forward until it hit a concact"wall" then go back until it hits another concact"wall" there it need to stop motor 1 and 2. there is 2 motor - 1 motor driv the item forward and back while the other motor drive the same way bouth forward and back so always the same way

  1. Add a "start" button to sketch in post #8 (the pin, the pinMode).
  2. In setup, read the start button so you have some control
  3. In setup, start the correct motor in the correct direction.
  4. Move the rest of the code from loop() to to setup()
// https://forum.arduino.cc/t/need-help-to-l298n/1265112/13

const int motor1ForwardPin = 2;
const int motor1BackwardPin = 3;
const int motor2Pin = 4;
const int contact1Pin = 5;
const int contact2Pin = 6;
const int startButton = 7;
const int offLED = 8;

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

  pinMode(motor1ForwardPin, OUTPUT);
  pinMode(motor1BackwardPin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(contact1Pin, INPUT_PULLUP);
  pinMode(contact2Pin, INPUT_PULLUP);
  pinMode(startButton, INPUT_PULLUP);
  pinMode(offLED, OUTPUT);

  digitalWrite(offLED, HIGH); // show OFF state
  while (digitalRead(startButton)) {} // wait for BUTTON 7 (start)
  digitalWrite(offLED, LOW); // stop OFF state

  // MOTOR 1 FORWARD
  while (digitalRead(contact1Pin) == HIGH) { // wait for BUTTON 5 (limit 1)
    digitalWrite(motor1ForwardPin, HIGH);
    digitalWrite(motor1BackwardPin, LOW);
    digitalWrite(motor2Pin, HIGH);
  }

  // MOTOR 1 REVERSE
  while (digitalRead(contact2Pin) == HIGH) { // wait for BUTTON 6 (limit 2)
    digitalWrite(motor1ForwardPin, LOW);
    digitalWrite(motor1BackwardPin, HIGH);
    digitalWrite(motor2Pin, HIGH);
  }

  // STOP MOTOR 1
  digitalWrite(motor1ForwardPin, LOW);
  digitalWrite(motor1BackwardPin, LOW);
  digitalWrite(motor2Pin, LOW); // disable motor

  digitalWrite(offLED, HIGH); // show OFF state
}

void loop() {
  // nothing
}

diagram.json

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -78,
      "left": 150.6,
      "rotate": 90,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -49.2,
      "left": 150.6,
      "rotate": 90,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": -145.2,
      "left": 150.6,
      "rotate": 90,
      "attrs": { "color": "blue", "flip": "" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -61,
      "left": -28.8,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn2",
      "top": -109,
      "left": -28.8,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo1",
      "top": -67.2,
      "left": 192,
      "attrs": { "text": "FORWARD" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo2",
      "top": -38.4,
      "left": 192,
      "attrs": { "text": "REVERSE" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo3",
      "top": -96,
      "left": -86.4,
      "attrs": { "text": "LIMIT_1" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo4",
      "top": -48,
      "left": -86.4,
      "attrs": { "text": "LIMIT_2" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo5",
      "top": -134.4,
      "left": 192,
      "attrs": { "text": "ON" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn3",
      "top": -157,
      "left": -28.8,
      "attrs": { "color": "blue" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo6",
      "top": -144,
      "left": -76.8,
      "attrs": { "text": "START" }
    },
    {
      "type": "wokwi-led",
      "id": "led4",
      "top": -164.4,
      "left": 150.6,
      "rotate": 90,
      "attrs": { "color": "red", "flip": "" }
    },
    {
      "type": "wokwi-text",
      "id": "legendservo7",
      "top": -153.6,
      "left": 192,
      "attrs": { "text": "OFF" }
    }
  ],
  "connections": [
    [ "nano:2", "led1:A", "green", [ "v0" ] ],
    [ "nano:GND.1", "led1:C", "black", [ "v0" ] ],
    [ "nano:GND.1", "led3:C", "black", [ "v0" ] ],
    [ "nano:4", "led3:A", "green", [ "v0" ] ],
    [ "nano:3", "led2:A", "green", [ "v0" ] ],
    [ "nano:GND.1", "led2:C", "black", [ "v0" ] ],
    [ "nano:GND.2", "btn1:2.r", "black", [ "v0" ] ],
    [ "nano:6", "btn1:1.r", "green", [ "v0" ] ],
    [ "nano:GND.2", "btn2:2.r", "black", [ "v0" ] ],
    [ "nano:5", "btn2:1.r", "green", [ "v0" ] ],
    [ "nano:GND.2", "btn3:2.r", "black", [ "v0" ] ],
    [ "nano:7", "btn3:1.r", "green", [ "v0" ] ],
    [ "nano:GND.1", "led4:C", "black", [ "v0" ] ],
    [ "nano:8", "led4:A", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}

Connect everything as shown in the tutorial and start with this code

#define Dir_Forward 0
#define Dir_Back 1

byte direction_X = 0;

// Motor A connections
const int  enA = 9;
const byte in1 = 8;
const byte in2 = 7;
// Motor B connections
const int  enB = 3;
const byte in3 = 5;
const byte in4 = 4;

// Contact Switches
const byte contact1pin = 2;
const byte contact2pin = 10;

void setup()
{
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
  analogWrite(enA, 255);
  analogWrite(enB, 255);

  // Set switches as Input with pull-ups
  pinMode (contact1pin, INPUT_PULLUP);
  pinMode (contact2pin, INPUT_PULLUP);

  // Start by Moving Forward
  Forward();
}

void loop()
{
  int contact1 = digitalRead(contact1pin);
  int contact2 = digitalRead(contact2pin);


}

void Stop()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}
void Forward()
{
  direction_X = Dir_Forward;
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void Back()
{
  direction_X = Dir_Back;
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

thank you, it make the motors running, but the stop concact doesn't work.

Post your code and we will see what's wrong.

It was the code you sent #22

I did not write the entire code for you. The code I gave you has all the basic functions you need to do what you want. You will have to figure out how to do the switching part yourself.

Hint:
When you call Forward() the direction_X varaible will equal Dir_Forward
When you call Back() the direction_X varaible will equal Dir_Back

Use the direction_X varaible and the contact1,2 variable to figure out what to do next