2

I'm using an L298N to control the two motors of a robot car, and I also have a servo controlling the HC-SR04. The motors and sensor work fine together, and the servo and sensor work fine together. But when I put them in the code together, only the servo works. Code is

#include <Servo.h>

// motor one
int enA = 9;    //enable PWM for right motor
int rightFwd = 7;    //right forward
int rightBack = 6;    //right backward
// motor two
int enB = 10;   //enable PWM for left motor
int leftFwd = 5;    //left forward
int leftBack = 4;    //left backward

//ultrasonic sensor
int trig = 2;
int echo = 3;

//servo for looking left and right
//int servoPin = 8;

int runSpeed = 255;
int distance;
int leftDistance;
int rightDistance;
//int servoCenter = 100;
//int lookAngle = 60;

//Servo servoMotor;

void setup()
{
  Serial.begin(9600);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(rightFwd, OUTPUT);
  pinMode(rightBack, OUTPUT);
  pinMode(leftFwd, OUTPUT);
  pinMode(leftBack, OUTPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
//  pinMode(servoPin, OUTPUT);

//  servoMotor.attach(servoPin);
//  servoMotor.write(servoCenter);
}

void loop()
{
  test();
}

void goForward()
{
  Serial.println("Going forward");
  analogWrite(enA, runSpeed*3/4);
  analogWrite(enB, runSpeed*3/4);
  digitalWrite(leftBack,LOW);
  digitalWrite(leftFwd,HIGH);
  digitalWrite(rightBack,LOW);
  digitalWrite(rightFwd,HIGH);
}

void goBackward()
{
  Serial.println("Going backward");
  analogWrite(enA, runSpeed*3/4);
  analogWrite(enB, runSpeed*3/4);
  digitalWrite(leftBack,HIGH);
  digitalWrite(leftFwd,LOW);
  digitalWrite(rightBack,HIGH);
  digitalWrite(rightFwd,LOW);
}

void turnLeft()
{
  Serial.println("Turning left");
  analogWrite(enA, runSpeed*3/4);
  analogWrite(enB, runSpeed*3/4);
  digitalWrite(leftBack,HIGH);
  digitalWrite(leftFwd,LOW);
  digitalWrite(rightBack,LOW);
  digitalWrite(rightFwd,HIGH);
}

void turnRight()
{
  Serial.println("Turning right");
  analogWrite(enA, runSpeed*3/4);
  analogWrite(enB, runSpeed*3/4);
  digitalWrite(leftBack,LOW);
  digitalWrite(leftFwd,HIGH);
  digitalWrite(rightBack,HIGH);
  digitalWrite(rightFwd,LOW);
}

void allStop()
{
  Serial.println("Stopping");
  digitalWrite(leftBack,LOW);
  digitalWrite(leftFwd,LOW);
  digitalWrite(rightBack,LOW);
  digitalWrite(rightFwd,LOW);
}

int getDistance()
{
  //  Getting the distance from the sensor
  long duration, distance;
  digitalWrite(trig,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig, LOW);
  duration=pulseIn(echo, HIGH);
  distance =(duration/2)/29.1;
  Serial.print(distance);
  Serial.println("CM");
  delay(10);
  return distance;
}

//int lookRight()
//{
//  Serial.println("Looking right\n");  
//  servoMotor.write(50);
//  delay(500);
//  int distance = getDistance();
//  delay(100);
//  servoMotor.write(servoCenter - lookAngle);
//  delay(100);
//  servoMotor.write(servoCenter);
//  return distance;
//}
//
//int lookLeft()
//{
//  Serial.println("Looking left\n");
//  servoMotor.write(servoCenter + lookAngle);
//  delay(500);
//  int distance = getDistance();
//  delay(100);
//  servoMotor.write(servoCenter);
//  return distance;
//  delay(100);
//}

void test()
{
  getDistance();
  goForward();
  delay(1000);
  allStop();
  delay(1000);
  goBackward();
  delay(1000);
  allStop();
  delay(1000);
  turnLeft();
  delay(1000);
  allStop();
  delay(1000);
  turnRight();
  delay(1000);
  allStop();
  delay(1000);
//  lookLeft();
//  lookRight();
}

I've commented out the servo parts; as it is, the motors and the sensor perform the test perfectly. Uncommenting the servo parts makes the servo and sensor work, but the motors stop.

I see from here that

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.

I'm using 9 and 10 as the enA and enB pins respectively for the PWM of the motors, and the servo is on pin 8.

I built up the code from scratch, testing each function as I went, and it's all good apart from this problem. How can I fix this?

1 Answer 1

2

The comment you have found which is also on the Servo library page https://www.arduino.cc/en/Reference/servo and is the core of your problem.

On an Uno, you will have to change the PWM pins to 3,5,6 or 11 as the timer which provides the PWM for pins 9 and 10 is now being used for the Servo library.

This is probably the easiest option, especially if you are using an L298 module, as you should be able to just swap the leads.

Alternatively, there may be an alternative Servo library available which does not use that timer (a quick Google showed some candidates, but I have not tested).

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.