Motors not working with Ultrasonic Sensor

So, I tried making an Arduino Project(Controlling Motors by Remote and Ultrasonic Sensor would activate Servo when the distance is less than 50cm)
I am trying to build a project, A remote controlled car, which will pick up objects when it encouters an object within 50cm. The remote controlled car works fine, but, to pick up objects i am trying to make it work with ultrasonic sensor, but to no avail

First when I just build the circuit for Remote Controled Motors the circuit worked fine, but after adding an Ultrasonic Sensor and Servo the circuit is not working)

This is the code without Ultrasonic and Servo:

#define Next_button 16615543  // code received from next button
#define Prev_button 16619623  // code received from previous button
#define left_button 16591063  // code received from left button
#define right_button 16607383 // code received from right button
#define Stop_button 16580863   // code received from stop button

int receiver_pin = 12;      //output pin of IR receiver to pin 2 of arduino
//initializing the pins for leds
int left_motor1 = 2;      //pin 6 of arduino to pin 7 of l293d
int left_motor2 = 3;      //pin 7 of arduino to pin 2 of l293d
int right_motor1  =4;      //pin 5 of arduino to pin 10 of l293d
int right_motor2 = 5;      //pin 4 of arduino to pin 15 of l293d

IRrecv receiver(receiver_pin); //Arduino will take output of IR receiver from pin 2
decode_results output;

void setup() {
  Serial.begin(9600);
  receiver.enableIRIn(); // Start to take the output from IR receiver
  //initializing all the pins where we have connected the led's as output pins
  pinMode(left_motor1, OUTPUT);
  pinMode(left_motor2, OUTPUT);
  pinMode(right_motor1, OUTPUT);
  pinMode(right_motor2, OUTPUT);
}

void loop() {
  if (receiver.decode(&output)) {
    unsigned int value = output.value;
    switch(value) {
      case Next_button:
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,LOW);
           break;
      case Prev_button:
           digitalWrite(left_motor1,HIGH);
           digitalWrite(left_motor2,LOW);
           digitalWrite(right_motor1,LOW);
           digitalWrite(right_motor2,HIGH);
           break;
      case left_button: 
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,HIGH);
           break;
      case right_button:
           digitalWrite(left_motor1,HIGH);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,LOW);
           break;
      case Stop_button:
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,LOW);
           digitalWrite(right_motor1,LOW);
           digitalWrite(right_motor2,LOW);
           break;
    }
    receiver.resume();
  }
}

This is the code with Ultrasonic and Servo:

#include <IRremote.h>      //including the remote library
#include <Servo.h>

#define Next_button 16615543  // code received from next button
#define Prev_button 16619623  // code received from previous button
#define left_button 16591063  // code received from left button
#define right_button 16607383 // code received from right button
#define Stop_button 16580863   // code received from stop button
#define trigPin 11
#define echoPin 10

int receiver_pin = 12;      //output pin of IR receiver to pin 2 of arduino
//initializing the pins for leds
int left_motor1 = 2;      //pin 6 of arduino to pin 7 of l293d
int left_motor2 = 3;      //pin 7 of arduino to pin 2 of l293d
int right_motor1  =4;      //pin 5 of arduino to pin 10 of l293d
int right_motor2 = 5;      //pin 4 of arduino to pin 15 of l293d
Servo servo;

IRrecv receiver(receiver_pin); //Arduino will take output of IR receiver from pin 2
decode_results output;

void setup() {
  Serial.begin(9600);
  receiver.enableIRIn(); // Start to take the output from IR receiver
  //initializing all the pins where we have connected the led's as output pins
  pinMode(left_motor1, OUTPUT);
  pinMode(left_motor2, OUTPUT);
  pinMode(right_motor1, OUTPUT);
  pinMode(right_motor2, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servo.attach(8);
}

void loop() {
  if (receiver.decode(&output)) {
    unsigned int value = output.value;
    switch(value) {
      case Next_button:
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,LOW);
           break;
      case Prev_button:
           digitalWrite(left_motor1,HIGH);
           digitalWrite(left_motor2,LOW);
           digitalWrite(right_motor1,LOW);
           digitalWrite(right_motor2,HIGH);
           break;
      case left_button: 
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,HIGH);
           break;
      case right_button:
           digitalWrite(left_motor1,HIGH);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,LOW);
           break;
      case Stop_button:
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,LOW);
           digitalWrite(right_motor1,LOW);
           digitalWrite(right_motor2,LOW);
           break;
    }
    receiver.resume();
  }
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance < 50) {
    Serial.println("the distance is less than 5");
    servo.write(90);
  }
  else {
    servo.write(0);
  }
  if (distance > 60 || distance <= 0) {
    Serial.println("The distance is more than 60");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}


The signal from the Ultrasonic module is based on time, and one of the most used ways to measure time is through a timer, it is likely that the use of a timer is interfering.

Your code uses pulseIn() this may be inserting a very long repetition interval of the loop() routine

Mutant101:
First when I just build the circuit for Remote Controled Motors the circuit worked fine, but after adding an Ultrasonic Sensor and Servo the circuit is not working)

What EXACTLY does “is not working” mean? Does it compile? Does it do anything at all when you use the remote or place something in front of the sensor?

What do you see on your Serial.prints, does it get to the bits of code you expect it to?

BTW what’s with the “if distance < 50 then print the distance is less than 5” ?

Steve

Hi,
Have you written some code that JUST uses the Ultrasonics?
If not, then do it and get it working.
Have you written some code that JUST controls the servo?
If not, then do it and get it working.

Then combine the two and get it working.
THEN combine your code with the motor control code, which I hope works on its own with the IR.

Tom… :slight_smile:

What Tom said.
Your topic revealed faulty thinking. Motors are not meant to work with ultrasonic sensors. Motors are meant to work with your Arduino (through a driver). And sensors are meant to work with your Arduino.