Motor driver not driving the wheel.

I have build up a line follower but a problem has risen up where the l298n dc motor driver is not driving the wheel.It is a single dual shaft tt motor attached to in3, in4 and enaB.
All other components are working including the IR sensor and servo to control the direction except the wheel is not rotating.
I would appreciate your comments and directions ...if any error is done.

#include <Servo.h>

#define leftIr 2
#define rightIr 3

#define MotorSpeed 8
#define MotorP 4
#define MotorN 5
int potValue;
#define leftultrasonicTrigger 11
#define leftultrasonicEcho 12
#define rightultrasonicTrigger 6
#define rightultrasonicEcho 7
Servo myservo;

// motors speed (0 - 255)
const float FORWARD_SPEED = 100;

void setup() {
  Serial.begin(115200);
  myservo.attach(13);
  pinMode(leftIr, INPUT);
  pinMode(rightIr, INPUT);
  pinMode(MotorSpeed, OUTPUT);
  pinMode(MotorP, OUTPUT);
  pinMode(MotorN, OUTPUT);
  pinMode(leftultrasonicTrigger, OUTPUT);
  pinMode(leftultrasonicEcho, INPUT);
  pinMode(rightultrasonicTrigger, OUTPUT);
  pinMode(rightultrasonicEcho, INPUT);
}

void loop() {
  long leftduration, leftdistance;
  digitalWrite(leftultrasonicTrigger, HIGH);
  delayMicroseconds(1000);
  digitalWrite(leftultrasonicTrigger, LOW);
  leftduration = pulseIn(leftultrasonicEcho, HIGH);
  leftdistance = (leftduration / 2) / 29.1;
  Serial.print(leftdistance);
  Serial.println("CM");
  delay(10);
  potValue = leftdistance;
  potValue = map(potValue, 0, 1023, 0, 180);
  long rightduration, rightdistance;
  digitalWrite(rightultrasonicTrigger, HIGH);
  delayMicroseconds(1000);
  digitalWrite(rightultrasonicTrigger, LOW);
  rightduration = pulseIn(rightultrasonicEcho, HIGH);
  rightdistance = (rightduration / 2) / 29.1;
  Serial.print(rightdistance);
  Serial.println("CM");
  delay(10);
  potValue = rightdistance;
  potValue = map(potValue, 0, 1023, 0, 180);
  
  drive();
  moveForward();
  moveLeft();
  moveRight();
  stopAircraft();
  
 
  if ((leftdistance <= 50) && (rightdistance>=50))
  {
    stopAircraft();
    myservo.write(132);
    delay(5000);
    myservo.write(87);
    delay(10000);
    myservo.write(42);
    delay(5000);
    myservo.write(87);
    delay(5000);
    drive();
    Serial.println('leftdistance');
  }

  else if ((rightdistance <= 50) && (leftdistance>=50))
  {
    stopAircraft();
    myservo.write(42);
    delay(5000);
    myservo.write(87);
    delay(10000);
    myservo.write(132);
    delay(5000);
    myservo.write(87);
    delay(5000);
    drive();
    Serial.println('rightdistance');
  }
   if ((leftdistance<=50)  && (rightdistance<= 50))
  {
    stopAircraft();
    myservo.write(87);
  }
 
}
void drive() {
  int leftIrValue = digitalRead(leftIr) ;
  int rightIrValue = digitalRead(rightIr) ;

  if (leftIrValue  && rightIrValue )
    moveForward();
  else if (leftIrValue && !(rightIrValue ))
    moveLeft();
  else if (!(leftIrValue) && rightIrValue )
    moveRight();
  else if (!(leftIrValue) && !(rightIrValue))
    stopAircraft();

  // Runs an action during just 125ms to make sure it doesn't cross the line
  delay(125);
  stopAircraft();
  delay(75);
}


void moveForward() {
  // PWM
 analogWrite(MotorSpeed, FORWARD_SPEED);

  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(87);
  
}

void moveLeft() {
  // PWM
  analogWrite(MotorSpeed,FORWARD_SPEED);
   // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(132);

}

void moveRight() {
  // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(42);

}

void stopAircraft() {
  // PWM
  analogWrite(MotorSpeed, 0);
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(87);

}

Did the motor work with a simple program for only the motor and controller?

Paul

What Arduino are you using? Pin 8 isn't a PWM pin on most of them.

Steve

Paul_KD7HB:
Did the motor work with a simple program for only the motor and controller?

Paul

no

slipstick:
What Arduino are you using? Pin 8 isn't a PWM pin on most of them.

Steve

Arduino Mega 2560

What is the rated voltage and stall current of the motor? What is the motor power source (voltage and current capability)?

voltage and stall current of the motor?

3VDC up to 6VDC

What is the motor power source (voltage and current capability)

7.4 volts - voltage
1100 mAh - current capacity

Hi,
Can I suggest you post the simple motor drive code you attempted and a circuit diagram of how you connected your Arduino, motor controller, motor and power supply please?
PLEASE do not post a FRITZY picture.

You need to go back to JUST getting the motor controlled before adding any other code.

Thanks.. Tom... :slight_smile:

alexmunala:
I have build up a line follower but a problem has risen up where the l298n dc motor driver is not driving the wheel.It is a single dual shaft tt motor attached to in3, in4 and enaB.

You don't attach the motor to in3 and in4. You attach two data pins to in3 and in4. The motor goes to out3 and out4.