1

I'm just beginning to understand Arduino. Please bear with me. I have a collision avoidance car running on Arduino Uno, WB291111 L298N motor driver, SG90 servo, Arduino Shield v5, and HC-SR04 ultrasonic sensor. Also, I have the entire setup running on 8.4v batteries. If I just use HIGH/LOW on the motors, it will run without any problems. But if I use the PWM to run the motors on different speeds, it will NOT run at all. The sensor and servo works, but not the motors. See my codes below. Let me know if you need further details. Any help is greatly appreciated.

#include <Servo.h>
#include <NewPing.h>

//our L298N control pins
const int enA = 10;
const int in1 = 6;
const int in2 = 7;

const int enB = 9;
const int in3 = 4;
const int in4 = 5;

//sensor pins
#define trig_pin A2 
#define echo_pin A1 

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

NewPing sonar(trig_pin, echo_pin, maximum_distance);
unsigned int uS;

#define SERVO_PIN 11
Servo servo_motor; 


void setup(){
  Serial.begin(115200);
  pinMode(enA, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);


  servo_motor.attach(SERVO_PIN); 

  servo_motor.write(115);
  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(200);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(200);
    distanceLeft = lookLeft();
    delay(200);

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

int lookRight(){  
  Serial.print("Look Right");
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  Serial.print("Look Left");
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

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


}

void moveStop(){
  Serial.print("Stop");

  digitalWrite(in3, LOW);
  digitalWrite(in1, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(in2, LOW);
}

void moveForward(){

  //if(!goesForward){

    //goesForward=true;

    Serial.print("Forward");

    digitalWrite(in1, HIGH);
    digitalWrite(in3, HIGH);

    digitalWrite(in2, LOW);
    digitalWrite(in4, LOW); 

    analogWrite(enA, 250);
    analogWrite(enB, 250);

  //}
}

void moveBackward(){

  //goesForward=false;

  Serial.print("Backward");

  digitalWrite(in2, HIGH);
  digitalWrite(in4, HIGH);

  digitalWrite(in1, LOW);
  digitalWrite(in3, LOW);

}

void turnRight(){
  Serial.print("Right");

  digitalWrite(in1, HIGH);
  digitalWrite(in4, HIGH);

  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);

  delay(200);

  digitalWrite(in1, HIGH);
  digitalWrite(in3, HIGH);

  digitalWrite(in2, LOW);
  digitalWrite(in4, LOW);



}

void turnLeft(){
  Serial.print("Left");

  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);

  digitalWrite(in1, LOW);
  digitalWrite(in4, LOW);

  delay(200);

  digitalWrite(in1, HIGH);
  digitalWrite(in3, HIGH);

  digitalWrite(in2, LOW);
  digitalWrite(in4, LOW);
}

1 Answer 1

1

According to the Servo reference page it's not possible to use Servo library and PWM outputs on the pins 9 and 10.

On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

2
  • Thank you so much for your input. I will try to use other PWM pins on the arduino uno board. I'll update if it works. Feb 26, 2018 at 14:49
  • @JayZapanta You can only hope that NewPing library doesn't affect Timer2, as Servo library affects Timer1
    – KIIV
    Feb 26, 2018 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.