Automatics braking system using dc motor, arduino uno, ultrasonic sensor and L298n

hello everyone please help me, I've been trying to make a project (auto-braking system) using arduino uno, L298n motor driver , ultrasonic sensor and dc motor.
I have uploaded the code in the arduino but the motor is not stopping when the obstacle is detected..
here is the code I'm using.....

#include <Ultrasonic.h>
#include <L298N.h>

//Define trig and echo pins for the ultrasonic sensor
#define trigPin 8
#define echoPin 9

//Define pin numbers for the L298N library
#define enA 5
#define in1 6
#define in2 7

//Create an ultrasonic sensor object
Ultrasonic ultrasonic(trigPin, echoPin);

//Create a L298N object for the motor
L298N motor(enA, in1, in2);

bool objectFlag = false;

byte objectSeen = 20;
byte objectGone = 30;

//^****
void setup()
{
//Set the motor's speed
motor.setSpeed(1000);

} //END of setup()

//^****
void loop()
{
//Measure the distance to the obstacle
long distance = ultrasonic.read();

//*******************************************
//If the distance is less than objectSeen CM
if (objectFlag == false && distance < objectSeen)
{
//Stop the car
motor.stop();

//we see an object
objectFlag = true;

}

//*******************************************
//If the distance is equal to or more than objectGone CM
if (objectFlag == true && distance >= objectGone)
{
//object is gone
objectFlag = false;

//start the car
motor.forward();

}

} //END of loop()

First you need to post your code so we can read and copy it.
In the IDE, click on Edit, then Copy For Forum, that will copy you code. Then just come here and do a paste.

A wiring diagram of how you have things connected would also help.

1 Like
#include <Ultrasonic.h>
#include <L298N.h>

//Define trig and echo pins for the ultrasonic sensor
#define trigPin 8
#define echoPin 9

//Define pin numbers for the L298N library
#define enA 5
#define in1 6
#define in2 7

//Create an ultrasonic sensor object
Ultrasonic ultrasonic(trigPin, echoPin);

//Create a L298N object for the motor
L298N motor(enA, in1, in2);

bool objectFlag = false;

byte objectSeen = 20;           //change as needed
byte objectGone = 30;           //change as needed


//********************************************^************************************************
void setup()
{
  //Set the motor's speed
  motor.setSpeed(1000);

} //END of   setup()


//********************************************^************************************************
void loop()
{
  //Measure the distance to the obstacle
  long distance = ultrasonic.read();
  
  //*******************************************
  //If the distance is less than objectSeen CM
  if (objectFlag == false && distance < objectSeen)   
  {
    //Stop the train
    motor.stop();

    //we see an object
    objectFlag = true;
  }

  //*******************************************
  //If the distance is equal to or more than objectGone CM
  if (objectFlag == true && distance >= objectGone)
  {
    //object is gone
    objectFlag = false;

    //start the train
    motor.forward();
  }

} //END of   loop()


This the wiring am using

Hi, @chamo-luke

Where is the gnd connection between the UNO and the motor driver?

Tom.. :smiley: :+1: :coffee: :australia:

Your code only starts the motor once it's been stopped. If it never stops then you must have a wiring error somewhere. If your diagram is correct then there is no ground connection to the L298 driver.

Turn the builtin LED ON/OFF when you check the distance so you can see if that is working.

Byte is from 0 to 255, but sensor read from 0 to 400.

Try use int instead byte.

I moved your topic to an appropriate forum category @chamo-luke .

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

Is the 9 volt a PP3 block battery?

Tom... :smiley: :+1: :coffee: :australia:

If you want the motor to brake to a stop, set in1 and in2 both LOW and keep enA HIGH. Page 8 in datasheet.

https://www.st.com/resource/en/datasheet/l298.pdf

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