4WD Obstacle Avoiding smart car using L298N Motor Driver Module, HC SR-04 Ultrasonic Sensor and SG90 Micro Servo Motor

For below problem, only servo motor (carrying Ultrasonic sensor) is rotating. Wheel motors are not getting actuated. Could you please suggest/guide me to make it work as per title.

Reference I have followed is:

Reference

Pre-requisite checks which I have made and are successful are:

  1. all wheel motors are working fine. Direction of rotation of all the wheels is checked and it is fine.
  2. Before this code, I have checked thorough functioning of L298N Motor Driver Module. It is working fine. I built small codes for the rotations and those are fine.
  3. Ultrasonic sensor is detecting obstacle

Mentioning Connections below:
HC-SR04 Ultrasonic Sensor:
VCC to 5V
Trig to A1 (Analog Input)
Echo to A2 (Analog Input)
GND to GND

SG90 Micro Servo Motor:
Signal (S) to Arduino Pin 11
VCC to 5V
GND to GND

L298 Motor Driver Module:
Input1 to 7
Input2 to 6
Input3 to 9
Input4 to 8

Below mentioned is the code.

#include <Servo.h>    //Servo motor library. This is standard library
#include <NewPing.h>  //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 9;
const int LeftMotorBackward = 8;
const int RightMotorForward = 7;
const int RightMotorBackward = 6;

//sensor pins
#define trig_pin A1  //analog input 1
#define echo_pin A2  //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance);  //sensor function
Servo servo_motor;                                    //our servo name

void setup() {

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);

  servo_motor.attach(11);  //our servo pin

  servo_motor.write(85);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop() {

  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20) {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft) {
      turnRight();
      moveStop();
    } else {
      turnLeft();
      moveStop();
    }
  } else {
    moveForward();
  }
  distance = readPing();
}

int lookRight() {
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(85);
  return distance;
}

int lookLeft() {
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(85);
  return distance;
  delay(100);
}

int readPing() {
  delay(70);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 250;
  }
  return cm;
}

void moveStop() {

  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward() {

  if (!goesForward) {

    goesForward = true;

    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);

    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
  }
}

void moveBackward() {

  goesForward = false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);

  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
}

void turnRight() {

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);

  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);

  delay(500);

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);

  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

void turnLeft() {

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);

  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);

  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}





Pictures of wiring are not useful. Please post a hand drawn wiring diagram, with pins and connections clearly labeled.

Wheel motors are not getting actuated.

Put Serial.print statements at various places in the code to see the values of variables, especially ones that control the motors, and determine whether they meet your expectations.

2 Likes

You are lacking just about everything needed to help solve your problem. Post an annotated schematic, hand drawing is OK. Also we are not interested in the pattern of your table cloth, go to a plain color. While you are at it post links to technical information on each of the hardware items, I am getting lazy and tired of looking things up.

Why do you wait until the end of the loop function to read the value of distance?

Please find the schematic. Kindly let me know if u need more details. Thanks.

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